Skip to content

Instantly share code, notes, and snippets.

@bigin
Last active December 2, 2017 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bigin/0395fb4f3cc4ac73967c085cf8ba62b4 to your computer and use it in GitHub Desktop.
Save bigin/0395fb4f3cc4ac73967c085cf8ba62b4 to your computer and use it in GitHub Desktop.
<?php
/**
* GS 'Routing' demonstration module
*
* Demonstrates how to use "data_index" to modify the page data
* and how to use url segments to control process flows in your
* scripts.
*
* Required:
* --------------------------------------------------------------
* RewriteEngine On
* RewriteCond %{REQUEST_FILENAME} !-f
* RewriteCond %{REQUEST_FILENAME} !-d
* RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
*/
register_plugin(
'router',
'Catalog Router',
'0.1',
'J.Ehret',
'https://ehret-studio.com',
'A simple router plugin',
'',
''
);
/**
* This is our gateway function, GetSimple calls this when "data_index" fired.
* This is a good place to attach your code.
*/
add_filter('data_index',
function($data)
{
$input = new \Input();
/**
* Break routing here if the page isn't a part of the catalog.
* Just please remember to make the "last" segment as a slug,
* so that GS would be able to use it in the native way:
*/
if($input->urlSegments->get(0) != 'catalog') {
global $pagesArray;
$lastSegment = $input->urlSegments->getLast();
if(isset($pagesArray[$lastSegment])) {
$data = getXml(GSDATAPAGESPATH . $lastSegment . '.xml');
}
return $data;
}
/**
* From this point downwards all pages belongs to your catalog
* and you can now use URL segments to control your process flow.
*
* URL segments can be accessed from the $input->urlSegments variable:
*
* $input->urlSegments->get($n) // Retrieve the $n'th URL segment (1-...)
*
* $input->urlSegments->getLast() // Retrieve the last segment
*
* $input->urlSegments->total // The total number of segments
*
* For example the URL could be:
* 1) /catalog/
* 2) /catalog/category/
* 3) /catalog/category/subcategory/
* 3) /catalog/category/subcategory/item/
* 4) etc
*
* Lets assume that the path /catalog/food/fruits/bananas/
*
* $input->urlSegments->get(0) // catalog
* $input->urlSegments->get(1) // food
* $input->urlSegments->get(2) // fruits
* $input->urlSegments->get(3) // bananas
*
*/
// Set default values
$pageTitle = null;
$metaTags = null;
$metaDescr = null;
$menuLabel = null;
$template = 'template.php';
$url = 'slug-you-want-to-set';
$content = 'This is the default content, could be 404 page';
// ...
// /catalog/category/subcategory/item/
if($input->urlSegments->get(3))
{
$pageTitle = 'My Item Page Title';
$metaTags = 'my, item, tags, ...';
$metaDescr = 'Meta item decr...';
$menuLabel = 'Your Item Menu Label';
$url = $input->urlSegments->get(3);
//$template = 'your-item-template-file.php';
$content = 'This is the Item page content '.$input->urlSegments->get(3);
// Or function call: render_item_content($input->urlSegments->get(3));
// /catalog/category/subcategory/
} elseif($input->urlSegments->get(2))
{
$pageTitle = 'My Subcat Page Title';
$metaTags = 'my, subcat, tags, ...';
$metaDescr = 'Meta subcat decr...';
$menuLabel = 'Your Subcategory Menu Label';
//$template = 'your-subcategory-template-file.php';
$content = 'This is the Subcategory page content';
// Or function call: render_subcategory_content();
// /catalog/category/
} elseif($input->urlSegments->get(1))
{
$pageTitle = 'My Cat Page Title';
$metaTags = 'my, cat, tags, ...';
$metaDescr = 'Meta cat decr...';
$menuLabel = 'Your Subcategory Menu Label';
//$template = 'your-category-template-file.php';
$content = 'This is the Category page content';
// Or function call: render_category_content();
// /catalog/
} elseif($input->urlSegments->get(0) == 'catalog')
{
$pageTitle = 'My Cat Page Title';
$metaTags = 'my, cat, tags, ...';
$metaDescr = 'Meta cat decr...';
$menuLabel = 'Your Subcategory Menu Label';
//$template = 'your-category-template-file.php';
$content = 'This is the Catalog page content';
// Or function call: render_category_content();
}
$sxe = new \SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><item></item>');
$sxe->addChild('pubDate', date('r'));
$sxe->addChild('title', $pageTitle);
$sxe->addChild('url', $url);
$sxe->addChild('meta', $metaTags);
$sxe->addChild('metad', $metaDescr);
$sxe->addChild('menu', $menuLabel);
$sxe->addChild('template', $template);
$sxe->addChild('content', $content);
// ...
return $sxe;
}
);
class Input
{
public $urlSegments;
public function __construct() {
$this->urlSegments = new \UrlSegments();
$this->parseUrl();
}
private function parseUrl() {
$currentPath = isset($_GET['id']) ? $_GET['id'] : 'index';
$this->buildSegments($currentPath);
}
private function buildSegments($url) {
$parseUrl = parse_url(trim($url));
if(isset($parseUrl['path'])) {
foreach(array_values(array_filter(array_map('trim', explode('/', $parseUrl['path'])))) as $key => $value) {
$this->urlSegments->set($key, basename($value));
$this->urlSegments->total++;
}
}
}
}
class UrlSegments
{
public $total = 0;
public function set($id, $value) {
$this->segment{$id} = $value;
}
public function get($id) {
return isset($this->segment{$id}) ? $this->segment{$id} : null;
}
public function getLast() {
return isset($this->segment{($this->total - 1)}) ? $this->segment{($this->total - 1)} : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment