gacela-project / router
A minimalistic HTTP router.
Fund package maintenance!
chemaclass.com/sponsor
Requires
- php: >=8.1
- gacela-project/container: ^0.6
Requires (Dev)
- ext-mbstring: *
- friendsofphp/php-cs-fixer: ^3.41
- gacela-project/gacela: ^1.7
- infection/infection: ^0.26
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- psalm/plugin-phpunit: ^0.18
- symfony/var-dumper: ^5.4
- vimeo/psalm: ^5.18
Suggests
- gacela-project/gacela: ^1.7
README
A minimalistic HTTP router, ideal for your proof-of-concept projects and decoupled controllers.
Why?
There are many other routers out there. Eg: using Symfony Framework, Laravel, etc... however, these are really rich in features which means they add a lot of accidental complexity and dependencies to your vendor, that you might want to avoid. At least for your proof-of-concept project.
Gacela Router doesn't aim to be the best router that can do everything, but a light router to have the bare minimum code, ideal for your simple ideas to emerge.
For a POC, we value simplicity over a rich-feature library.
Installation
composer require gacela-project/router
Example
# `Bindings` and `Handlers` are optional, and you can place them in any order. $router = new Router(function (Routes $routes, Bindings $bindings, Handlers $handlers) { // Custom redirections $routes->redirect('docs', 'https://gacela-project.com/'); // Matching a route coming from a particular or any custom HTTP methods $routes->get('custom', CustomController::class, '__invoke'); $routes->...('custom', CustomController::class, 'customAction'); $routes->any('custom', CustomController::class); // Matching a route coming from multiple HTTP methods $routes->match(['GET', 'POST'], '/', CustomController::class); // Binding custom dependencies on your controllers $routes->get('custom/{number}', CustomControllerWithDependencies::class, 'customAction'); $bindings->bind(SomeDependencyInterface::class, SomeDependencyConcrete::class) // Handle custom Exceptions with class-string|callable $handlers->handle(NotFound404Exception::class, NotFound404ExceptionHandler::class); }); $router->run();
Working demo
For a working example run composer serve
and check the example/example.php
TIP:
composer serve
is equivalent to:php -S localhost:8081 example/example.php