eserozvataf / scabbia2-router
Scabbia2 Router Component
Installs: 26
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/eserozvataf/scabbia2-router
Requires
- php: >=5.6.0
Requires (Dev)
This package is auto-updated.
Last update: 2023-09-08 20:01:57 UTC
README
This component is a simple routing dispatcher which resolves and dispatchs the routes to callbacks or controllers.
Usage
Creating route definitions
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
file_put_contents('routes.json', json_encode($routes->save()));
Loading route definitions back
$routes = json_decode(file_get_contents('routes.json'));
Dispatching a route
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
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
- Documentation
- Contributor List
- License Information I II
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 repository.