Routing - PHP Library
, (*1)
Router class for PSR-7 http, (*2)
Installation
Install using composer:, (*3)
$ composer require assouan/routing
Usage
$router = new Routing\Router();
$router->map($regex1, $callable1);
$router->map($regex2, $callable2);
$router->map($regex3, $callable3);
$response = $router->match($request);
echo $reponse;
Example
$router = new Routing\Router();
$router->map('/', 'sample_func');
$router->map('/special', 'SampleController::special')->setOption('method', 'post')->setOption('scheme', 'https');
$router->map('/hello/(?<name>\w+)', 'SampleController::hello');
$response = $router->match($request); // $request = Psr\Http\Message\ServerRequestInterface
echo $response;
function sample_func()
{
return 'Hello world!';
}
class SampleController
{
public function special()
{
return 'Page only in HTTPS POST';
}
public static function hello($request, $name)
{
// with function parameter
$username = $name;
// with request attribute
$username = $request->getAttribute('name');
return 'Hello '.$username;
}
}