Txiki Router
Simple router for PHP, (*1)
, (*2)
, (*3)
Install
Via Composer, (*4)
``` bash
$ composer require txiki/router, (*5)
## Requirements
The following versions of PHP are supported by this version.
* PHP 5.4
* PHP 5.5
* PHP 5.6
* HHVM
## Documentation
Simple example:
``` php
// load composer autoload
require '../vendor/autoload.php';
use Txiki\Router\Route;
$r = new Route();
// add GET route
$r->get('/home', function(){
return "GET Hello world!";
});
//tell router what you want to process, the route and the http method
$route = $r->exec( '/home', 'get');
if($route!==false){
// example process response
echo $route->response;
}else{
// example response 404
echo '404';
}
Add more routes:, (*6)
``` php
// add POST route
$r->post('/home', function(){
return "POST Hello world!";
});, (*7)
// add DELETE route
$r->delete('/home', function(){
return "DELETE Hello world!";
});, (*8)
// add PUT route
$r->put('/home', function(){
return "PUT Hello world!";
});, (*9)
Wildcard and custom routes:
``` php
// add route to any http method
$r->any('/home', function(){
return "Hello world! respond to any http method";
});
// custom http methods for one route
$r->add('/home', function(){
return "Hello world! respond to custom http methods";
}, 'get|post');
Manage custom url params:
``` php
$r->any('/test-{id}/{name}/{n}', function($id, $name , $n){
return 'Test: ' . $id .' '.$name.' '.$n;
})->params([
// add your own regular expression to param or
// 'use Txiki\Router\RouteRegex'
'id' => RouteRegex::INT,
'name' => RouteRegex::ALPHA,
'n' => RouteRegex::INT
]
);, (*10)
Use class/method as callback:
``` php
class myClass{
public function method1( $id, $name){
return 'Hello world ' .$id . ' '. $name;
}
}
$r->get('/user/{id}/{name}', 'myClass::method1');
Helper methods:
``` php
// get all established routes
$table = $r->table();, (*11)
// get all routes for '/home'
$map = $r->getRouteMap('/home');, (*12)
// get only POST route for '/home'
$map = $r->getRouteMap('/home', 'post');, (*13)
## Testing
``` bash
$ vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details., (*14)
Credits
License
The MIT License (MIT). Please see License File for more information., (*15)