aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2014-02-24 15:52:17 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2014-02-24 15:52:17 +0100
commite81b020f3ba334ed863d562750d6719efbae0b4d (patch)
treead7dfcb14d219827ceec76fef6177612dfcebe95
parent66ec5bc0c06196fe55459c2ae9622cfd652fd645 (diff)
downloadRouter-e81b020f3ba334ed863d562750d6719efbae0b4d.zip
Router-e81b020f3ba334ed863d562750d6719efbae0b4d.tar.gz
Router-e81b020f3ba334ed863d562750d6719efbae0b4d.tar.bz2
Add syntax highlighting.
-rw-r--r--README.md106
1 files changed, 60 insertions, 46 deletions
diff --git a/README.md b/README.md
index b0456af..b7dac10 100644
--- a/README.md
+++ b/README.md
@@ -27,83 +27,97 @@ We consider the following routes:
There are different ways to declare routes but the more usual is as follows:
- $router = new Hoa\Router\Http();
- $router
- ->get('u', '/hello', function ( ) {
+```php
+$router = new Hoa\Router\Http();
+$router
+ ->get('u', '/hello', function ( ) {
- echo 'world!', "\n";
- })
- ->post('v', '/hello', function ( Array $_request ) {
+ echo 'world!', "\n";
+ })
+ ->post('v', '/hello', function ( Array $_request ) {
- echo $_request['a'] + $_request['b'], "\n";
- })
- ->get('w', '/bye', function ( ) {
+ echo $_request['a'] + $_request['b'], "\n";
+ })
+ ->get('w', '/bye', function ( ) {
- echo 'ohh :-(', "\n";
- })
- ->get('x', '/hello_(?<nick>\w+)', function ( $nick ) {
+ echo 'ohh :-(', "\n";
+ })
+ ->get('x', '/hello_(?<nick>\w+)', function ( $nick ) {
- echo 'Welcome ', ucfirst($nick), '!', "\n";
- });
+ echo 'Welcome ', ucfirst($nick), '!', "\n";
+ });
+```
We can use a basic dispatcher to call automatically the associated callable of
the appropriated rule:
- $dispatcher = new Hoa\Dispatcher\Basic();
- $dispatcher->dispatch($router);
+```php
+$dispatcher = new Hoa\Dispatcher\Basic();
+$dispatcher->dispatch($router);
+```
Now, we will use [cURL](http://curl.haxx.se/) to test our program that listens
on `127.0.0.1:8888`:
- $ curl 127.0.0.1:8888/hello
- world!
- $ curl -X POST -d a=3\&b=39 127.0.0.1:8888/hello
- 42
- $ curl 127.0.0.1:8888/bye
- ohh :-(
- $ curl -X POST 127.0.0.1:8888/bye
- // error
- $ curl 127.0.0.1:8888/hello_gordon
- Welcome Gordon!
- $ curl 127.0.0.1:8888/hello_alyx
- Welcome Alyx!
+```sh
+$ curl 127.0.0.1:8888/hello
+world!
+$ curl -X POST -d a=3\&b=39 127.0.0.1:8888/hello
+42
+$ curl 127.0.0.1:8888/bye
+ohh :-(
+$ curl -X POST 127.0.0.1:8888/bye
+// error
+$ curl 127.0.0.1:8888/hello_gordon
+Welcome Gordon!
+$ curl 127.0.0.1:8888/hello_alyx
+Welcome Alyx!
+```
This simple API hides a modular mechanism that can be foreseen by typing
`print_r($router->getTheRule())`.
To unroute, i.e. make the opposite operation, we can do this:
- var_dump($router->unroute('x', array('nick' => 'gordon')));
- // string(13) "/hello_gordon"
+```php
+var_dump($router->unroute('x', array('nick' => 'gordon')));
+// string(13) "/hello_gordon"
+```
### CLI
We would like to recognize the following route `[<group>:]?<subcommand>
<options>` in the `Router.php` file:
- $router = new Hoa\Router\Cli();
- $router->get(
- 'g',
- '(?<group>\w+):(?<subcommand>\w+)(?<options>.*?)'
- function ( $group, $subcommand, $options ) {
+```php
+$router = new Hoa\Router\Cli();
+$router->get(
+ 'g',
+ '(?<group>\w+):(?<subcommand>\w+)(?<options>.*?)'
+ function ( $group, $subcommand, $options ) {
- echo 'Group : ', $group, "\n",
- 'Subcommand: ', $subcommand, "\n",
- 'Options : ', trim($options), "\n";
- }
- );
+ echo 'Group : ', $group, "\n",
+ 'Subcommand: ', $subcommand, "\n",
+ 'Options : ', trim($options), "\n";
+ }
+);
+```
We can use a basic dispatcher to call automatically the associated callable:
- $dispatcher = new Hoa\Dispatcher\Basic();
- $dispatcher->dispatch($router);
+```php
+$dispatcher = new Hoa\Dispatcher\Basic();
+$dispatcher->dispatch($router);
+```
And now, testing time:
- $ php Router.php foo:bar --some options
- Group : foo
- Subcommand: bar
- Options : --some options
+```sh
+$ php Router.php foo:bar --some options
+Group : foo
+Subcommand: bar
+Options : --some options
+```
The use of `Hoa\Console` would be a good idea to interprete the options and
getting some confortable services for the terminal.