1. Go to this page and download the library: Download phossa2/route library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
if ($dispatcher->match('GET', '/user/view/1234')) {
$result = $dispatcher->getResult();
switch($result->getStatus()) {
case 200:
// ...
break;
case 404:
// ...
break;
default:
// ...
break;
}
} else {
// no match found
// ...
}
use Phossa2\Route\Route;
use Phossa2\Route\Status;
$route = new Route(
'GET',
'/user/{action:xd}/{id:d}',
function($result) { // handler for Status::OK
// ...
}
);
use Phossa2\Route\Status;
$dispatcher->addHandler(
function($result) {
echo "method " . $result->getMethod() . " not allowed";
},
Status::METHOD_NOT_ALLOWED
);
use Phossa2\Route\Status;
$dispatcher->addHandler(
function($result) {
echo "no other handler found";
},
0 // <-- match all other status
);
use Phossa2\Route\Collector\Collector;
use Phossa2\Route\Resolver\ResolverSimple;
// dispatcher with default resolver
$dispatcher = new Route\Dispatcher(
new Collector(),
new ResolverSimple() // the default resolver anyway
);
use Phossa2\Route\Status;
use Phossa2\Route\Dispatcher;
use Phossa2\Route\Extensions\RedirectToHttps;
// create dispatcher
$dispatcher = new Dispatcher();
// direct any HTTP request to HTTPS port before any routing
$dispatcher
->addExtension(new RedirectToHttps())
->addHandler(function() {
echo "redirect to https";
}, Status::MOVED_PERMANENTLY)
->dispatch('GET', '/user/view/123');
use Phossa2\Route\Status;
use Phossa2\Route\Dispatcher;
use Phossa2\Route\Extensions\UserAuth;
$dispatcher = new Dispatcher();
$dispatcher
// add handler for unauthorized routing
->addHandler(
function() {
echo "need auth";
}, Status::UNAUTHORIZED)
// add a route
->addGet('/user/view/{id:d}', function() {
echo "AUTHED!";
})
// add extension to force auth routes under '/user/'
->addExt(function($event) {
$result = $event->getParam('result');
$path = $result->getPath();
if (!isset($_SESSION['authed']) && '/user/' === substr($path, 0, 6)) {
$result->setStatus(Status::UNAUTHORIZED);
return false;
}
return true;
}, Dispatcher::EVENT_BEFORE_MATCH);
// try a not authed route
$dispatcher->dispatch('GET', '/user/view/123');
// try a authed route
$_SESSION['authed'] = 1;
$dispatcher->dispatch('GET', '/user/view/123');
use Phossa2\Route\Status;
use Phossa2\Route\Dispatcher;
use Phossa2\Route\Extensions\IdValidation;
$dispatcher = new Dispatcher();
// add extension to a route
$route = (new Route('GET', '/user/{id:d}', null))
->addExtension(new IdValidation());
// will fail
$dispatcher->addRoute($route)->dispatch('GET', '/user/1000');
$dispatcher->enableDebug()->setDebugger($logger);
// created with default RER collector
$dispatcher = (new Dispatcher())
->addCollector(new Collector()) // regex based routing first
->addCollector(new CollectorQPR()); // support for legacy QPR
use Phossa2\Route\Dispatcher;
use Phossa2\Route\Parser\ParserStd;
use Phossa2\Route\Collector\Collector;
// use standard algorithm
$dispatcher = new Dispatcher(new Collector(new ParserStd));