Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/razonyang/php-router
A fast and high scalable HTTP router for PHP
https://github.com/razonyang/php-router
Last synced: about 1 month ago
JSON representation
A fast and high scalable HTTP router for PHP
- Host: GitHub
- URL: https://github.com/razonyang/php-router
- Owner: razonyang
- License: mit
- Created: 2017-09-01T07:11:48.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-17T07:10:08.000Z (over 1 year ago)
- Last Synced: 2024-04-14T02:23:41.004Z (7 months ago)
- Language: PHP
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Router
A fast and high scalable HTTP router for PHP.
## Requirements
- PHP >= 5.4
## Install
```
composer require razonyang/php-router:dev-master
```Please add the following repository into `repositories` when `composer` complains about
that `Could not find package razonyang/php-router ...`.```
{
"type": "git",
"url": "https://github.com/razonyang/php-router.git"
}
```## Features
- **Grouping router**
- Friendly to **RESTful API**
- No third-party library dependencies
- High scalable## Usage
```
use RazonYang\Router\Router;$router = new Router();
$router->handle('GET', '', 'homepage');
// RESTful API
$router->get('users', 'users list'); // GET "users" matched
$router->post('users', 'create user account'); // POST "users" matched
$router->get('users/', 'users profile'); // GET "users/foo" matched
$router->delete('users/', 'delete user'); // DELETE "users/bar" matched
$router->get('posts/', 'post content'); // GET "posts/1" matched, GET "posts/nan" unmatched// Grouping
$router->group('admin', function (Router $group) {
$group->get('', 'admin dashboard');
});// Dispatch request
$requestMethod = 'GET';
$requestPath = 'users/foo';// If matched, $route contains handler, params and settings, otherwise $route is null.
// In this case:
// $route = [
// 'handler', // first elements
// 'params', // second elements
// 'settings', // third elements
// ];
$route = $router->dispatch($requestMethod, $requestPath);
```