Litero
Extra light router and dispatcher for small web applications.
Small footprint and no overhead., (*1)
Router class in this package maps HTTP-requests by URL pattern to callbacks., (*2)
Note: It's component not support PSR-7 HTTP Message Interface out of the box., (*3)
Installing
Create project based on Litero in current directory:, (*4)
composer create-project bit55/litero .
And add your application code to autoloading section in composer.json
., (*5)
Or include Litero in your existing project:, (*6)
composer require bit55/litero
Or download this repository and include file Router.php
in your project., (*7)
Your webserver must point to the index.php file for any URI entered. See .htaccess
for Apache and example.nginx.conf
for Nginx configuration example., (*8)
Usage
Create Instance of Router class., (*9)
$router = Bit55\Litero\Router::fromGlobals();
Add routing rules. Routes may contents exact or wildcard rules., (*10)
Wildcards example:
* /page/:seg
- any characters in one segment like /page/qwerty
or /page/123
;
* /page/:num
- digits only like /page/123
;
* /page/:any
- any characters like /page/qwerty
or /page/qwerty/123
;, (*11)
Route handler may be any callable (function name, closure) or string with controller class name and action method. Router instantiate controller and execute action method automatically., (*12)
Wildcard parameters will be passed as function params in handler., (*13)
Note if you using Composer, add your controller classes to autoloading., (*14)
Add single rule with Closure handler:, (*15)
$router->add('/', function () {
echo 'Hello from Litero!';
});
Or add array of routes., (*16)
$router->add([
'/first' => 'Bit55\Litero\ExampleController@firstAction',
'/second/:any' => 'Bit55\Litero\ExampleController@secondAction',
]);
Start route processing., (*17)
if ($router->isFound()) {
$router->executeHandler(
$router->getRequestHandler(),
$router->getParams()
);
}
else {
// Simple "Not found" handler
$router->executeHandler(function () {
http_response_code(404);
echo '404 Not found';
});
}
Usage example code see in index.php
file., (*18)