https://github.com/morphable/micro
new routing lib
https://github.com/morphable/micro
Last synced: 2 months ago
JSON representation
new routing lib
- Host: GitHub
- URL: https://github.com/morphable/micro
- Owner: Morphable
- Created: 2019-11-24T18:24:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-21T20:30:08.000Z (about 3 years ago)
- Last Synced: 2024-12-27T05:16:34.048Z (over 1 year ago)
- Language: PHP
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Micro
a micro framework based on the psr-15 and psr-7 standard
### Simple usage
```php
routing();
$router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) {
$id = $args['id'];
/** @link https://www.php-fig.org/psr/psr-7/ */
return $response;
});
try {
$response = $micro->handle($request); // \Psr\Http\Message\ResponseInterface
} catch (\Exception $e) {
// 404
}
```
### Callbacks
```php
add('GET', '/user/:id', ['controller', 'method']);
// __invoke
$router->add('GET', '/user/:id', 'controller');
// static method
$router->add('GET', '/user/:id', [controller::class, 'method']);
function callback(ServerRequestInterface $request, array $args) {
return $response;
}
// function
$router->add('GET', '/user/:id', 'callback');
// annonymous function
$router->add('GET', '/user/:id', function (ServerRequestInterface $request, array $args) {
return $response;
});
```
### Route pattern
```php
add('GET', '/user/:userId/profile', function ($request, $args){
$args['userId'] // second parameter in url
});
$router->add('GET', '/callback/?:channel', function ($request, $args) {
$args['channel'] // second parameter or null
})
```
### Middleware
```php
handle($request);
}
}
$router->add('GET', '/user/:id', ['controller', 'method'])
->middleware('middleware'); // from container
```
### Groups
```php
group('api', function ($router) { // prefix of api
$router->add('GET', '/', ['controller', 'method']); // pattern: /api
$router->group('user', function ($router) {
$router->add('GET', '/:id', ['controller', 'method']); // pattern: /api/user/:id
})->middleware('middleware');
})->middleware('middleware'); // counts for every route inside
```
### After call
```php
add('GET', '/', ['controller', 'method'])
->after(function ($reqest, $args, $response): void {})
->after(['class', 'method'])
->after('function');
```