https://github.com/sasa-b/router
Lightweight router inspired by Laravel's and Phalcon's router.
https://github.com/sasa-b/router
dispatcher library php router
Last synced: over 1 year ago
JSON representation
Lightweight router inspired by Laravel's and Phalcon's router.
- Host: GitHub
- URL: https://github.com/sasa-b/router
- Owner: sasa-b
- License: mit
- Created: 2017-10-04T20:20:31.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-09T18:55:44.000Z (over 8 years ago)
- Last Synced: 2025-01-19T19:20:06.184Z (over 1 year ago)
- Topics: dispatcher, library, php, router
- Language: PHP
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Router
Lightweight router inspired by Laravel's and Phalcon's router.
*********
## Usage examples
```php
$router = new \Foundation\Routing\Router();
$r->get('/foo', function () {
echo 'Hello foo!';
});
// here we are utilising cache for performance, if the cache file was not found
// routes will be registered and the cache file recreated
$router->cache(function (\Foundation\Routing\Router $r) {
// if you want to collect routes from a file
// you can set the path to the routes file as a paramater to collectRoutes() method
// or via setRoutesPath() method
$r->collectRoutes();
// you can both collect routes and add them one by one, they will be merged
$r->get('/foo/{bar}', [
'controller' => 'FooController',
'action' => 'index',
]);
$r->post('/foo/{bar}', 'FooController::store');
});
try {
// Adding event listeners
$router->addEventListener('before_match', function(\Foundation\Routing\Router $router) {
echo "before match";
});
$router->addEventListener('after_match', function(\Foundation\Routing\Router $router) {
echo "after match";
});
$dispatcher = $router->catch();
$dispatcher->dispatch();
} catch (\Foundation\Routing\Exceptions\NotFoundException $e) {
echo $e->getCode() . " - Page not found";
} catch (\Foundation\Routing\Exceptions\BadHttpMethodException $e) {
echo $e->getCode() . " - Bad Http Method";
}
```