Router - Gimle/phpFramework GitHub Wiki

How to use the router

<?php

$router = Router::getInstance();

$router->setCanvas('pc');

// Use template called main.php for top level page.
$router->bind('pc', '', function () use ($router) {
	return $router->setTemplate('main');
});

// Match url starting with article, and then have one part after.
// eg: article/my-article
$router->bind('pc', ':template/:id', function () use ($router) {
	return $router->setTemplate('article');
}, ['template' => 'article', 'id' => '[^/]+']);

// Match url starting with article, and then have any number of parts after.
// eg article/2017/my-article
$router->bind('pc', ':template/:id', function () use ($router) {
	return $router->setTemplate('article');
}, ['template' => 'article', 'id' => '.*']);

// Match url starting with article, and then have zero or one part after.
// eg article or article/my-article
$router->bind('pc', ':template(/:id)', function () use ($router) {
	return $router->setTemplate('article');
}, ['template' => 'article', 'id' => '[^/]+']);

$router->dispatch();

Supporting other request methods.

By default the router will route requests of type GET and HEAD. To override this you can specify any other request method like so:

<?php
$router->bind('pc', ':template/:id', function () use ($router) {
	return $router->setTemplate('article');
}, ['template' => 'feedback', 'id' => '[^/]+'], Router::R_POST);