https://github.com/eser/scabbia2-router
Scabbia2 Router Component
https://github.com/eser/scabbia2-router
Last synced: 6 months ago
JSON representation
Scabbia2 Router Component
- Host: GitHub
- URL: https://github.com/eser/scabbia2-router
- Owner: eser
- License: other
- Archived: true
- Created: 2015-09-12T03:39:25.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-01-03T00:15:50.000Z (over 10 years ago)
- Last Synced: 2025-11-27T14:56:37.934Z (8 months ago)
- Language: PHP
- Size: 18.6 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-Apache
Awesome Lists containing this project
README
# Scabbia2 Router Component
[This component](https://github.com/eserozvataf/scabbia2-router) is a simple routing dispatcher which resolves and dispatchs the routes to callbacks or controllers.
[](https://travis-ci.org/eserozvataf/scabbia2-router)
[](https://scrutinizer-ci.com/g/eserozvataf/scabbia2-router/?branch=master)
[](https://packagist.org/packages/eserozvataf/scabbia2-router)
[](https://packagist.org/packages/eserozvataf/scabbia2-router)
[](https://packagist.org/packages/eserozvataf/scabbia2-router)
[](https://readthedocs.org/projects/scabbia2-documentation)
## Usage
### Creating route definitions
```php
use Scabbia\Router\RouteCollection;
$routes = new RouteCollection();
// adding a static route
$routes->addRoute('GET', '/about', 'AboutController::IndexAction');
// adding a static route with multiple http methods
$routes->addRoute(['GET', 'POST'], '/about', 'AboutController::IndexAction');
// adding a dynamic route
$routes->addRoute('GET', '/users/profile/{id:[a-z]+}', 'UsersController::ProfileAction');
// adding a dynamic route with a routing name
$routes->addRoute('GET', '/users/posts/{id:[a-z]+}', 'UsersController::PostsAction', 'user/posts');
```
### Saving route definitions
```php
file_put_contents('routes.json', json_encode($routes->save()));
```
### Loading route definitions back
```php
$routes = json_decode(file_get_contents('routes.json'));
```
### Dispatching a route
```php
use Scabbia\Router\Router;
$router = new Router($routes); // initialize a new router with route definitions
$route = $router->dispatch('GET', '/about');
if ($route['status'] === Router::FOUND) {
call_user_func($route['callback'], ...$route['parameters']);
}
```
### Reverse Routing with names
```php
use Scabbia\Router\Router;
$router = new Router($routes); // initialize a new router with route definitions
echo $router->path('users/posts', [ 'id' => 'eser' ]);
```
## Links
- [List of All Scabbia2 Components](https://github.com/eserozvataf/scabbia2)
- [Documentation](https://readthedocs.org/projects/scabbia2-documentation)
- [Twitter](https://twitter.com/eserozvataf)
- [Contributor List](contributors.md)
- License Information [I](LICENSE-Apache) [II](LICENSE-Flask)
## Contributing
It is publicly open for any contribution. Bugfixes, new features and extra modules are welcome. All contributions should be filed on the [eserozvataf/scabbia2-router](https://github.com/eserozvataf/scabbia2-router) repository.
* To contribute to code: Fork the repo, push your changes to your fork, and submit a pull request.
* To report a bug: If something does not work, please report it using GitHub issues.
* To support: [](https://gratipay.com/eserozvataf/)