2014-05-14 12:18:59 -04:00
|
|
|
R3
|
2014-05-11 18:52:36 -04:00
|
|
|
================
|
|
|
|
|
2014-05-14 12:18:59 -04:00
|
|
|
R3 is an URI router library. It compiles your route paths into a radix tree.
|
2014-05-13 04:13:20 -04:00
|
|
|
By using the constructed radix tree in the start-up time, you may dispatch your
|
|
|
|
routes efficiently.
|
2014-05-11 18:52:36 -04:00
|
|
|
|
2014-05-11 20:24:22 -04:00
|
|
|
Pattern Syntax
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
/blog/post/{id} use [^/]+ regular expression by default.
|
|
|
|
|
|
|
|
/blog/post/{id:\d+} use `\d+` regular expression instead of default.
|
|
|
|
|
2014-05-11 18:52:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
Use case in PHP
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
```php
|
|
|
|
// Here is the paths data structure
|
|
|
|
$paths = [
|
2014-05-13 04:13:20 -04:00
|
|
|
'/blog/post/{id}' => [ 'controller' => 'PostController' , 'action' => 'item' , 'method' => 'GET' ] ,
|
|
|
|
'/blog/post' => [ 'controller' => 'PostController' , 'action' => 'list' , 'method' => 'GET' ] ,
|
|
|
|
'/blog/post' => [ 'controller' => 'PostController' , 'action' => 'create' , 'method' => 'POST' ] ,
|
|
|
|
'/blog' => [ 'controller' => 'BlogController' , 'action' => 'list' , 'method' => 'GET' ] ,
|
2014-05-11 18:52:36 -04:00
|
|
|
];
|
2014-05-13 04:13:20 -04:00
|
|
|
$rs = r2_compile_radix($paths, 'persisten-table-id');
|
|
|
|
$ret = r2_dispatch_radix($rs, '/blog/post/3' );
|
2014-05-11 18:52:36 -04:00
|
|
|
list($complete, $route, $variables) = $ret;
|
|
|
|
|
|
|
|
list($error, $message) = r2_validate($route); // validate route conditions
|
|
|
|
if ( $error ) {
|
|
|
|
echo $message; // "Method not allowed", "...";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|