https://github.com/thomiceli/router
https://github.com/thomiceli/router
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/thomiceli/router
- Owner: thomiceli
- Created: 2021-02-01T14:39:58.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-04T15:45:59.000Z (over 4 years ago)
- Last Synced: 2025-01-16T10:32:32.358Z (over 1 year ago)
- Language: PHP
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# thomas-miceli/router
```shell
$ composer require thomas-miceli/router
```
### Dependencies
* Nyholm's [PSR-7 implementation](https://github.com/nyholm/psr7) (to create HTTP OO messages) and [PSR-7 server](https://github.com/nyholm/psr7-server) (for server requests)
* [PHP-DI](https://php-di.org) for dependency injection
* [PSR-15 interfaces](https://www.php-fig.org/psr/psr-15/)
### Working full example
```php
hello = 'hello';
$router->registerInstance($dependency);
$router->middleware(function (Request $request, RequestHandlerInterface $handler) {
$request = $request->withAttribute('global', true);
// code executed before the route
$response = $handler->handle($request);
// code executed after the route
return $response;
});
$router
->get('/hello/{name}', function ($name, Request $request, Response $response, stdClass $myClass) {
if ($request->getAttribute('route') === true) {
$myClass->route_middleware = 'works';
}
if ($request->getAttribute('global') === true) {
$myClass->global_middleware = 'works';
}
$myClass->hello .= " $name";
return $response->json($myClass);
// or
// $response->getBody()->write("$name");
// return $response;
})
->middleware(function (Request $request, RequestHandlerInterface $handler) {
// executed second
$request = $request->withAttribute('route', true);
/** @var Response $response */
$response = $handler->handle($request);
$response->setHeader('after', 'true');
return $response;
})
->middleware(function (Request $request, RequestHandlerInterface $handler) {
// executed first
$request = $request->withAttribute('route', false);
return $handler->handle($request);
});
$router->run();
```