33
44[ ![ Build Status] ( https://travis-ci.org/stein189/Simple-PHP-Router.svg?branch=master )] ( https://travis-ci.org/stein189/Simple-PHP-Router )
55
6- Note: This package is still in development, to use it add "minimum-stability": "dev" to your composer.json.
7-
86<h2 >Getting started</h2 >
97
108<b >Step 1 - .htaccess file</b >
@@ -28,7 +26,7 @@ create an .htaccess file in the root of your project and fill it with the code b
2826````
2927
3028<b >Step 2 - require szenis/routing</b ><br />
31- In your terminal execute: `` composer require szenis/routing 0.* ``
29+ In your terminal execute: `` composer require szenis/routing ``
3230
3331<b >Step 3 - create index.php</b ><br />
3432Create the file index.php in the root of your project
@@ -44,7 +42,7 @@ use Szenis\RouteResolver;
4442````
4543to your index.php
4644
47- <b >Step 6 *optional </b ><br />
45+ <b >Optional </b ><br />
4846For debuging purpose add the following to your index.php
4947```php
5048error_reporting(E_ALL);
@@ -60,42 +58,55 @@ For the sake of simplicity consider this code to be inside index.php
6058$router = new Router();
6159
6260/**
63- * Add a route to the homepage
64- * The first argument is the route that we want to look for
65- * The second argument are the accepted methods, methods have to be seperated by a |
66- * The third and last argument is the full path to the action that has to be executed,
67- * this could also be an closure
61+ * Route matches the url '/' for the GET method
6862 */
69- $router->add('/', 'GET|PUT', 'App\Controllers\PageController::index');
63+ $router->add('/', 'GET', function() {
64+ // the closure will be executed when route is triggerd and will return 'hello world'
65+ return 'hello world!';
66+ });
7067
7168/**
7269 * It is posible to add one or multiple wildcards in one route
7370 */
74- $router->add('/user/{id}', 'GET', 'App\Controllers\UserController::show');
71+ $router->add('/user/{id}', 'GET', function($id) {
72+ return $id;
73+ });
7574
7675/**
77- * Closure route example
76+ * It is also posible to allow mulitple methods for one route (methods should be separated with a '|')
7877 */
7978$router->add('/user/{id}/edit', 'GET|POST', function($id) {
80- echo $id;
81-
82- return;
79+ return 'user id: '.$id;
8380});
8481
82+ /**
83+ * Or when u are using controllers in a namespace you could give the full path to a controller (controller::action)
84+ */
85+ $router->add('/user/{id}/delete', 'DELETE', 'App\Controllers\UserController::delete');
86+
87+ /**
88+ * When all the controller are in the same namespace you could set the default namespace like so
89+ */
90+ $router->setNamespace('App\\Controllers\\');
91+
92+ /**
93+ * The route now uses the default namespace + the given namespace
94+ */
95+ $router->add('/user/{id}/update', 'PUT', 'UserController::update');
96+
97+ /**
98+ * After all the routes are created the resolver must be initialized
99+ */
85100$resolver = new RouteResolver($router);
86101
87102/**
88- * resolve the route
89- * the resolve function will search for an matching route
90- * when a matching route is found the given function will be triggerd.
91- * lets asume we have triggerd the route: /user/10
92- * the function `show` from the class `UserController` will be called
93- * the wildcard which is the number `10` will be passed on to the `show` function
103+ * resolve the route and receive the response
94104 */
95- $resolver->resolve([
105+ $response = $ resolver->resolve([
96106 'uri' => $_SERVER['REQUEST_URI'],
97107 'method' => $_SERVER['REQUEST_METHOD'],
98108]);
109+
99110````
100111
101112<b >When a route is not found an RouteNotFoundException will be thrown</b >
@@ -133,10 +144,10 @@ The following options exist
133144</p >
134145
135146```php
136- // In this case the id may be a number
147+ // The id must be a number
137148$router->add('/user/{n:id}', 'GET', 'App\Controllers\UserController::show');
138149
139- // In this case the id may only contain alfabetic chars or numbers (or both)
150+ // The id must contain either alfabetic chars or numbers
140151$router->add('/user/{an:id}', 'GET', 'App\Controllers\UserController::show');
141152
142153// Now we want everything behind docs/ in the page variable
@@ -154,6 +165,10 @@ $router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname =
154165
155166<h2 >Changelog</h2 >
156167
168+ <b >v1.0.0</b >
169+ - Updated readme
170+ - Posible to add default namespace
171+
157172<b >v0.9.0</b >
158173- 100% test coverage
159174- minimum php version reduced from 5.4 to 5.3
0 commit comments