tgrj / tweb_dispatch
minimalistic router
Requires
- php: ^8.0
This package is auto-updated.
Last update: 2026-05-07 13:32:41 UTC
README
minimalistic router
Background
I used noodlehaus/dispatch for routing, but wanted the feature to have routes be declarable as static class method attributes instead for convenience. Thus I forked the original code and extended all with some kind of "reflection layer".
Usage
Basically with this it should be possible to define routes as attributes on static class methods, as if if were the original route() method (method, uri path and middleware, while the final function would be the static method itself on which the attribute is attached).
Example:
class IndexController {
#[Route('GET', '/')]
public static function index(): callable
{
return response('index page');
}
#[Route('GET', '/item/:id', ['Middleware::auth'])]
public static function index(array $params): callable
{
return response('item-id: ' . ($params['id'] ?? 'fail'));
}
}
In the bootstrap (in my case it's the entry index.php file) you have to register the classes, which hold the attributes and finally use the dispatch() method as if it just were noodlehaus/dispatch:
tweb_add_routes_controllers(IndexController::class, ...);
dispatch();
Licence
noodlehaus/dispatch is under MIT licence, so is this repo as well!