tailored-tunes / php-router
A lightweight router solution
Requires (Dev)
- phpmd/phpmd: ^2.2
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: ^2.3
This package is not auto-updated.
Last update: 2021-11-13 01:25:14 UTC
README
A small routing library for php that decouples controllers from routing. The routing table only acts as a translation table between paths and intentions.
Installation
Install via composer. Installation help and versions at Packagist
Usage
Set up routes
use TailoredTunes\Router; $router = new Router(); $router->addRoutes( [ ["/" => "Home#index"], ["/login" => "Home#login"], ["/logout" => "Home#logout"], ["/privacy" => "Home#privacy"], ["/magic/:var1/:var2" => "Home#magic"] ] );
Variables in paths
You can match path variables by adding :var
notions to the route table, as above.
Those parameters will translate to the respective variable name in the parameters array of the handler.
Handle routes
The reason behind not handling the request internally is that you might want to put a controller factory between the routing and the serving. This way the router only tells you what the intention of the route was, then it's up to you to map it to a controller.
// $uri = the request uri // $method = the request method // $params = parameters for the request. $requestParamBuilder = new RequestParamBuilder(); $requestParamBuilder->withEnv($_ENV) ->withRequest($_REQUEST) ->withCookies($_COOKIE) ->withFiles($_FILES) ->withServer($_SERVER) ->withSession($_SESSION); $params = new RequestParams($requestParamBuilder); $handler = $router->handle($uri, $method, $params); // serve request call_user_func_array(array($handler->controller(), $handler->action()), $handler->parameters());