https://github.com/corpusphp/router
Simple HTTP and CLI Router for PHP
https://github.com/corpusphp/router
httprouter
Last synced: 3 months ago
JSON representation
Simple HTTP and CLI Router for PHP
- Host: GitHub
- URL: https://github.com/corpusphp/router
- Owner: CorpusPHP
- License: mit
- Created: 2014-06-24T17:13:15.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2025-12-02T00:50:54.000Z (8 months ago)
- Last Synced: 2026-01-11T12:34:46.786Z (7 months ago)
- Topics: httprouter
- Language: PHP
- Homepage:
- Size: 95.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Corpus Router
[](https://packagist.org/packages/corpus/router)
[](https://packagist.org/packages/corpus/router)
[](https://github.com/CorpusPHP/Router/actions/workflows/ci.yml)
A Simple Collection of Routers
## Requirements
- **php**: >=8.1
## Installing
Install the latest version with:
```bash
composer require 'corpus/router'
```
## Usage
### HttpRouter
```php
['REQUEST_METHOD' => [ 'method' => 'POST' ]]
$router = new \Corpus\Router\HttpRouter('\\Corpus\\Controllers', $_SERVER);
$route = $router->match('test/controller:action');
// $route =
// [
// 'controller' => '\\Corpus\\Controllers\\test\\controller',
// 'action' => 'action',
// 'options' => [],
// 'request' => [ 'method' => 'POST' ],
// ]
// ----------------
$route = $router->match('test/controller?query=whatwhat');
// $route =
// [
// 'controller' => '\\Corpus\\Controllers\\test\\controller',
// 'action' => NULL,
// 'options' => [ 'query' => 'whatwhat' ],
// 'request' => [ 'method' => 'POST' ],
// ]
// ----------------
$route = $router->match($_SERVER['REQUEST_URI']);
// $route = Current Request
// ----------------
$url = $router->generate('myNamespace\\admin', 'index');
// $url = '/myNamespace/admin:index'
// ----------------
$url = $router->generate('\\Corpus\\Controllers\\myNamespace\\admin', 'index');
// $url = '/myNamespace/admin:index'
// ----------------
try {
$url = $router->generate('\\Invalid\\Absolute\\Controller', 'index');
}catch (\Corpus\Router\Exceptions\NonRoutableException $e) {
$url = 'fail';
}
// $url = 'fail'
```
## Documentation
### Class: \Corpus\Router\HttpRouter
```php
__construct
```php
function __construct(string $rootNamespace [, array $server = []])
```
##### Parameters:
- ***array*** `$server` - The $_SERVER array - optional
---
#### Method: HttpRouter->match
```php
function match(string $path) : ?array
```
Match given path to a route array.
A non-null route is not guaranteed to _exist_ - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists
The returned route array the the a shape of
```php
[
// The controller action. Definition varies by router.
RouterInterface:ACTION => 'action',
// An expected class name based on given rules. Not guaranteed to exist.
RouterInterface:CONTROLLER => '\Controller\www\index',
// Router specific but akin to $_GET - may contain additional options
RouterInterface:OPTIONS => ['key' => 'value'],
]
```
Match given path to a route array.
A non-null route is not guaranteed to _exist_ - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists
The returned route array the the a shape of
```php
[
// The controller action. Definition varies by router.
RouterInterface:ACTION => 'action',
// An expected class name based on given rules. Not guaranteed to exist.
RouterInterface:CONTROLLER => '\Controller\www\index',
// Router specific but akin to $_GET - may contain additional options
RouterInterface:OPTIONS => ['key' => 'value'],
]
```
##### Parameters:
- ***string*** `$path` - The path to match against including query string ala `foo/bar.html?param=woo`
##### Returns:
- ***array*** | ***null*** - route array or null on failure to route
---
#### Method: HttpRouter->generate
```php
function generate($controller [, ?string $action = null [, array $options = []]]) : string
```
Generate a URL for the given controller, action and options
##### Parameters:
- ***object*** | ***string*** `$controller` - Instance or Relative 'admin\index' or absolute '\Controllers\www\admin\index'
**Throws**: `\Corpus\Router\Exceptions\NonRoutableException`
---
#### Method: HttpRouter->getNamespace
```php
function getNamespace() : string
```
##### Returns:
- ***string*** - The canonical namespace prefix
### Class: \Corpus\Router\CliRouter
```php
__construct
```php
function __construct($rootNamespace [, array $arguments = []])
```
##### Parameters:
- ***string*** `$rootNamespace` - The namespace prefix the controllers will be under
---
#### Method: CliRouter->match
```php
function match(string $path) : ?array
```
Match given path to a route array.
A non-null route is not guaranteed to _exist_ - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists
The returned route array the the a shape of
```php
[
// The controller action. Definition varies by router.
RouterInterface:ACTION => 'action',
// An expected class name based on given rules. Not guaranteed to exist.
RouterInterface:CONTROLLER => '\Controller\www\index',
// Router specific but akin to $_GET - may contain additional options
RouterInterface:OPTIONS => ['key' => 'value'],
]
```
##### Parameters:
- ***string*** `$path` - The path to match against including query string ala `foo/bar.html?param=woo`
##### Returns:
- ***array*** | ***null*** - route array or null on failure to route
---
#### Method: CliRouter->getNamespace
```php
function getNamespace() : string
```
##### Returns:
- ***string*** - The canonical namespace prefix