Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morphable/simplerouting
Simple routing component
https://github.com/morphable/simplerouting
component php routing simple
Last synced: 7 days ago
JSON representation
Simple routing component
- Host: GitHub
- URL: https://github.com/morphable/simplerouting
- Owner: Morphable
- License: mit
- Created: 2018-11-16T08:21:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-26T14:59:14.000Z (over 5 years ago)
- Last Synced: 2024-11-17T10:08:59.736Z (about 2 months ago)
- Topics: component, php, routing, simple
- Language: PHP
- Homepage: https://packagist.org/packages/morphable/simple-routing
- Size: 26.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleRouting
A simple component that takes care of routing.
## Installing
```terminal
$ composer require morphable/simple-routing
```## Usage
```php
sendResponse('UserId = ' . $req->getParam('user_id'), 200);
});// A POST route with middleware
$route2 = new Route('POST', '/user/:user_id/update', function ($req, $res) {
return $res->sendResponse('Welcome user 2!');
}, [
// middlewares
function ($req, $res) {
if ($req->getParam('user_id') != 2) {
return $res->sendResponse('Forbidden', 403);
}
}
]);SimpleRouting::add('user_detail', $route);
SimpleRouting::add('user_update', $route2);try {
SimpleRouting::execute();
} catch (\Morphable\SimpleRouting\Exception\RouteNotFound $e) {
// catch 404
die('404');
}```
## Builders and callbacks
Better callbacks and builders!Callback can now be anything that works with ```call_user_func()```
```php
use \Morphable\SimpleRouting\Builder;
$route = (new Builder())
->setMethod('GET')
->setRoute('/user/:userId')
->setCallback($callback)
->setMiddleware([$callback])
->build();$route = Builder::fromArray([
'method' => 'GET',
'route' => '/user/:userId',
'callback' => $callback,
'middleware' => $callback
]);```
## Contributing
- Follow PSR-2 and the .editorconfig
- Start namespaces with \Morphable\SimpleRouting
- Make tests