Simple Routes

Erstellt am: Sonntag 16 September 2018  |  Letzte Änderung am: Donnerstag 20 September 2018

This is a simple class that allows you to manage arbitrary routes within your ItemManager applications. Routes class can help to implement a URL Routing functionality in your web application. URL Routing is a mechanism used to map URLs to the categories/items that gets processed or displayed only when a certain request is received at the server.

The Routes class is very simple to use, but you'll need to create additional methods to implement the routing and processing of URL requests functionality into your application:

<?php
/**
 * Simple Routes Class
 *
 * This class should help to implement a URL Routing mechanism that
 * match request URLs with specific routes and map corresponding
 * categories.
 *
 * In order to use the class, a category must be created, with
 * a hidden field named 'routes'.
 */
class Routes
{
	private $imanager;

	private $mapper;

	private $id;

	private $data = array();

	public function __construct(int $category_id) {
		$this->imanager = imanager();
		$this->mapper = $this->imanager->getItemMapper();
		$this->id = $category_id;
		$this->init();
	}

	public function init() {
		$this->mapper->alloc($this->id);
		if(isset($this->mapper->simpleItems[1])) {
			$routes = unserialize(base64_decode(@$this->mapper->simpleItems[1]->routes));
			foreach($routes as $name => $value) {
				$this->data[$name] = $value;
			}
		}
	}

	public function set($name, $value) { $this->data[$name] = $value; }

	public function get($name) { return isset($this->data[(string) $name]) ? $this->data[(string) $name] : null; }

	public function save() {
		$this->mapper->limitedInit($this->id, 1);
		$routes = @$this->mapper->items[1];
		if(!$routes) {
			$routes = new Item($this->id);
			$routes->name = 'Routes';
		}
		$routes->setFieldValue('routes', base64_encode(serialize($this->data)));
		if($routes->save()) {
			$this->mapper->simplify($routes);
			$this->mapper->save();
		}
	}
}

Preparation

Before you can use this class, the Routes IM category with a hidden field named routes must already be created in IM admin:

Create new pages Catalog and another subpage with the name Categories in GS admin. Next, create three categories for testing: Fruits, Vegetables, Protein and add a set of products to this categories.

Usage

Let's create a new route with the name categories (normally this should be done automatically when creating/updating your categories, but the implementation of this functionality would exceed the scope of this tutorial by far. An idea of how to automatically create routes can be found in this article: Routes Manager). In order to use the Routes, you must include the class in your application:

include 'routes.php';

Next, create an instance of the Routes Class by passing the ID of your Routes category:

include 'routes.php';

$routes = new Routes(4);

Now, create the route with identifier categories, which contains all the IDs of associated product categories, and save it (By the way, it is up to you how you want to design your route, the route can contain any value structure you like.):

include 'routes.php';

$routes = new Routes(4);
// Create and save your route
$route = array(
    'name' => 'categories',
    'type' => 'Product Categories',
    // Add here the IDs of your categories: Fruits, Vegetables, Protein
    'value' => array(1, 2, 5)
);
$routes->set('categories', $route);
$routes->save();

Well, the route is created, let's test it. Use the code below in a template at the part where you want to show the list of products in your categories:

$imanager = imanager();
$itemMapper = $imanager->getItemMapper();
$categoryMapper = $imanager->getCategoryMapper();

include 'routes.php';

$routes = new Routes(4);
// Get the current page slug
$slug = get_page_slug(false);
// Check if the route exists and if it's a categories route
$route = $routes->get($slug);
if($route && $route['name'] == $slug) {
    // List related products:
    foreach($route['value'] as $cat_id) {
        $itemMapper->alloc($cat_id);
        $products = $itemMapper->getSimpleItems('active=1');
        if($products) {
            echo "<h3>{$categoryMapper->getCategory($cat_id)->name}</h3>";
            foreach($products as $product) {
                echo "<p>Name: $product->name</p>";
            }
        }
    }
}

You can display the product list by accessing the page URL: your-website.com/catalog/categories/.

Also read this article: Routes Manager - A GetSimple plugin based on ItemManager, that automatically creates new routes.

Autor: Bigin  |  Tags:  FrameworkPHPGetSimpleDevelopmentItemManager