From 1474bdcd3adc5075cefb49b3adf486cbd1c0788d Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 12 May 2014 06:52:36 +0800 Subject: [PATCH] Add README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0ff996 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +R2 +================ + +libr2 is a DFA URI router library. It compiles your route paths into DFA state +table, and you may dispatch the URL based on the transition table in C. + + + + + +Use case in PHP +----------------------- + +```php +// Here is the paths data structure +$paths = [ + '/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' ], +]; +$rs = r2_compile($paths, 'persisten-table-id'); +$ret = r2_dispatch($rs, '/blog/post/3' ); +list($complete, $route, $variables) = $ret; + +list($error, $message) = r2_validate($route); // validate route conditions +if ( $error ) { + echo $message; // "Method not allowed", "..."; +} +``` +