Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kusabi/router
A router library for PHP
https://github.com/kusabi/router
library php php7 router routing
Last synced: about 10 hours ago
JSON representation
A router library for PHP
- Host: GitHub
- URL: https://github.com/kusabi/router
- Owner: kusabi
- Created: 2019-02-03T17:42:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-03T16:54:09.000Z (over 5 years ago)
- Last Synced: 2024-05-18T10:59:26.971Z (6 months ago)
- Topics: library, php, php7, router, routing
- Language: PHP
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
This library implements a HTTP router.
It is designed to be extensible.
# Basic usage
Basic usage is very simple.
```php
// Instantiate the router
$router = new \Kusabi\Router\Router();// Read a user
$router->get('users/{id}', function(int $id) {
return "Showing user {$id}";
});// Create a user
$router->post('users', function() {
return "Creating new user";
});// Update a user
$router->put('users/{id}', function(int $id) {
return "Updating user {$id}";
})// Get the results
echo $router->run('get', 'users/123');
```The example above returns `Showing user 123`
### Forcing the data types
You can ensure a certain data type was passed in with either raw regex or one of the preset variable types provided.
```php
// Instantiate the router
$router = new \Kusabi\Router\Router();// Force ID to be integer
$router->get('users/{:int}', function(int $id) {
return "Showing user {$id}";
});// Force ID to be float
$router->get('users/{:float}', function(int $id) {
return "Showing user {$id}";
});// Force ID to be any number format
$router->get('users/{:number}', function(int $id) {
return "Showing user {$id}";
});// Force ID to be any aplha set
$router->get('users/{:alpha}', function(int $id) {
return "Showing user {$id}";
});// Force ID to be any aplha-numeric set
$router->get('users/{:alpha-numeric}', function(int $id) {
return "Showing user {$id}";
});// Wildcard the rest of the route (note that this would match 'users/21/edit/fluff'
$router->get('users/{:any}', function(int $id) {
return "Showing user {$id}";
});// Custom regex (matches ID like '12345-6789-123')
$router->get('users/(\d+-\d+-\d+)', function(int $id) {
return "Showing user {$id}";
});```
# Advanced usage
The key to this library is the ability to create your own plugins.
Don't like the way it parses routes? Change it.
Don't like how it handles responses? Change it.
Not happy with the verbs available? Add more (`// @todo allow adding of more verbs`)
### Adding a controller dispatcher
Create a controller dispatcher.
Please note that this dispatcher may be added in the future, or possibly in an extended library.
```php
action = $action;
}public function dispatch(...$params)
{
list($controller, $method) = explode('@', $this->action);
$c = new $controller();
return $c->$method(...$params);
}
}
```Next extend the factory to include your dispatcher as a possibility
```php
get('users/{id}', 'UserController@show');// Create a user
$router->post('users', 'UserController@create');// Update a user
$router->put('users/{id}', 'UserController@update');// Get the results
echo $router->run('get', 'users/123');
```