44[ ![ Total Downloads] ( https://poser.pugx.org/szenis/routing/downloads )] ( https://packagist.org/packages/szenis/routing )
55[ ![ Build Status] ( https://travis-ci.org/stein189/Simple-PHP-Router.svg?branch=master )] ( https://travis-ci.org/stein189/Simple-PHP-Router )
66
7+ ** Updating from version 0.x or 1.x will break your code! read the documentation before upgrading!**
8+
79<h2 >Getting started</h2 >
810
911<b >Step 1 - .htaccess file</b >
@@ -34,14 +36,36 @@ Create the file index.php in the root of your project
3436
3537<b >Step 4 - require autoload.php and use the Router</b ><br />
3638
37- Add
39+ The following snippet shows how the router can be used.
40+
3841``` php
3942<?php
4043
4144require './vendor/autoload.php';
4245
43- use Szenis\Router;
44- use Szenis\RouteResolver;
46+ use Szenis\Routing\Router;
47+
48+ $router = new Router();
49+ $router->get('/{n:date}-{w:item}', function($date, $item) {
50+ return 'hello world';
51+ });
52+
53+ $response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
54+
55+ switch ($response['code']) {
56+ case \Szenis\Routing\Route::STATUS_NOT_FOUND:
57+ // render your 404 page here...
58+ break;
59+
60+ case \Szenis\Routing\Route::STATUS_FOUND:
61+ // the router only resolves the route, here is an example how to execute the route.
62+ if ($response['handler'] instanceof \Closure) {
63+ echo call_user_func_array($response['handler'], $response['arguments']);
64+ }
65+
66+ break;
67+ }
68+
4569
4670```
4771to your index.php
@@ -54,7 +78,6 @@ ini_set('display_errors', 1);
5478```
5579
5680<h2 >Usage</h2 >
57- For the sake of simplicity consider this code to be inside index.php
5881
5982``` php
6083
@@ -87,20 +110,11 @@ $router->add('/user/{id}/edit', 'GET|POST', function($id) {
87110
88111/**
89112 * Or when u are using controllers in a namespace you could give the full path to a controller (controller::action)
113+ *
114+ * Since version 2.0 executing the handler is up to you.
90115 */
91116$router->add('/user/{id}/delete', 'DELETE', 'App\Controllers\UserController::delete');
92117
93- /**
94- * When all the controller are in the same namespace you could set the default namespace like so
95- */
96- $router->setNamespace('App\\Controllers\\');
97-
98- /**
99- * The route now uses the default namespace + the given namespace
100- */
101- $router->add('/user/{id}/update', 'PUT', 'UserController::update');
102-
103-
104118/**
105119 * Since version 1.1 there are shortcut methods for get, post, put, patch, delete and any.
106120 * You can use them as follow
@@ -113,37 +127,39 @@ $router->delete('/example/delete', function() {}); // Will match DELETE request
113127$router->any('/example/any', function() {}); // Will match GET, POST, PUT, PATCH, DELETE requests
114128
115129/**
116- * After all the routes are created the resolver must be initialized
130+ * resolve the route and receive the response
131+ *
132+ * The response is an array with the following keys
133+ * - code (contains 200 if the route is found, else 404)
134+ * - handler (contains the handler, often a \Closure or full path to your controller action)
135+ * - arguments (contains the route arguments if any)
117136 */
118- $resolver = new RouteResolver( $router);
137+ $response = $router->resolve($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'] );
119138
120139/**
121- * resolve the route and receive the response
140+ * To execute your route you have to do the following
122141 */
123- $response = $resolver->resolve([
124- 'uri' => $_SERVER['REQUEST_URI'],
125- 'method' => $_SERVER['REQUEST_METHOD'],
126- ]);
127- ```
128142
129- <b >When a route is not found an RouteNotFoundException will be thrown</b >
130- <p >Its posible to catch this exception and display a good looking 404 page, the try catch block will look something like this</p >
131-
132- ``` php
133- try {
134- // You have to resolve the route inside the try block
135- $resolver->resolve([
136- 'uri' => $_SERVER['REQUEST_URI'],
137- 'method' => $_SERVER['REQUEST_METHOD'],
138- ]);
139- } catch (Szenis\Exceptions\RouteNotFoundException $e) {
140- // route not found, add a nice 404 page here if you like
141- die($e->getMessage());
142- } catch (Szenis\Exceptions\InvalidArgumentException $e) {
143- // when an arguments of a route is missing an InvalidArgumentException will be thrown
144- // it is not necessary to catch this exception as this exception should never occur in production
145- die($e->getMessage());
143+ switch ($response['code']) {
144+ case \Szenis\Routing\Route::STATUS_NOT_FOUND:
145+ // render your 404 page here...
146+ break;
147+
148+ case \Szenis\Routing\Route::STATUS_FOUND:
149+ // the router only resolves the route, here is an example how to execute the route.
150+ if ($response['handler'] instanceof \Closure) {
151+ echo call_user_func_array($response['handler'], $response['arguments']);
152+ }
153+
154+ // if your handler is a full path to your function you could execute it with this:
155+ $className = substr($response['handler'], 0, strpos($response['handler'], '::'));
156+ $functionName = substr($response['handler'], strpos($response['handler'], '::') + 2);
157+
158+ echo call_user_func_array(array((new $className), $functionName), $response['arguments']);
159+
160+ break;
146161}
162+
147163```
148164
149165<h2 >Wildcard options</h2 >
@@ -180,8 +196,21 @@ $router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname =
180196})
181197```
182198
199+ <h2 >Upgrading from v0.x/v1.x to v2.x</h2 >
200+ $router->setNamespace() has been removed!
201+
202+ In version 2 the router does not execute the callable anymore. From now one it is your responsibility to execute the handler.
203+
204+ At the bottom of the section 'Usage' there is an example how to execute the handler.
205+
183206<h2 >Changelog</h2 >
184207
208+ <b >v2.0.0</b >
209+ - Removed 'default' namespace
210+ - Router does not execute the callable itself, this gives you more control over parameter injection
211+ - RouteResolver is callable trough the router
212+ - Bugfix: it is now possible to have more then one parameter in one segment (/{parameter1}-{parameter2}/)
213+
185214<b >v1.1.0</b >
186215- Shortcut functions for get, post, put, patch, delete and any
187216
@@ -217,7 +246,3 @@ $router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname =
217246
218247<b >v0.2.0</b >
219248- RouteResolver uses regex to match routes quicker
220-
221- <hr />
222-
223- Click <a href =" https://github.com/stein189/SimpleRoutingExample/tree/master " >here</a > to see the working example.
0 commit comments