Merge pull request #179 from skipperbent/v2

V2
This commit is contained in:
Simon Sessingø
2016-11-24 14:45:06 +02:00
committed by GitHub
+33 -33
View File
@@ -52,12 +52,12 @@ use \Pecee\SimpleRouter\SimpleRouter;
// Load external routes file // Load external routes file
require_once 'routes.php'; require_once 'routes.php';
/* /*
* The default namespace for route-callbacks, so we don't have to specify it each time. * The default namespace for route-callbacks, so we don't have to specify it each time.
* Can be overwritten by using the namespace config option on your routes. * Can be overwritten by using the namespace config option on your routes.
*/ */
SimpleRouter::setDefaultNamespace('MyWebsite'); SimpleRouter::setDefaultNamespace('Demo\Controllers');
// Start the routing // Start the routing
SimpleRouter::start(); SimpleRouter::start();
@@ -79,9 +79,9 @@ use Pecee\SimpleRouter\SimpleRouter;
/* /*
* This route will match the url /v1/services/answers/1/ * This route will match the url /v1/services/answers/1/
* The middleware is just a class that renders before the * The middleware is just a class that renders before the
* controller or callback is loaded. * controller or callback is loaded.
* *
* This is useful for stopping the request, for * This is useful for stopping the request, for
* instance if a user is not authenticated etc. * instance if a user is not authenticated etc.
*/ */
@@ -100,24 +100,24 @@ SimpleRouter::group(['middleware' => 'Middlewares\Site', 'exceptionHandler' => '
*/ */
SimpleRouter::get('/answers/{id?}', 'ControllerAnswers@show'); SimpleRouter::get('/answers/{id?}', 'ControllerAnswers@show');
/** /**
* This example will route url when matching the regular expression to the method. * This example will route url when matching the regular expression to the method.
* For example route: domain.com/ajax/music/world -> ControllerAjax@process (parameter: music/world) * For example route: domain.com/ajax/music/world -> ControllerAjax@process (parameter: music/world)
*/ */
SimpleRouter::all('/ajax', 'ControllerAjax@process')->setMatch('.*?\\/ajax\\/([A-Za-z0-9\\/]+)'); SimpleRouter::all('/ajax', 'ControllerAjax@process')->setMatch('.*?\\/ajax\\/([A-Za-z0-9\\/]+)');
/** /**
* Restful resource (see IRestController interface for available methods) * Restful resource (see IRestController interface for available methods)
*/ */
SimpleRouter::resource('/rest', 'ControllerRessource'); SimpleRouter::resource('/rest', 'ControllerRessource');
/** /**
* Load the entire controller (where url matches method names - getIndex(), postIndex(), putIndex()). * Load the entire controller (where url matches method names - getIndex(), postIndex(), putIndex()).
* The url paths will determine which method to render. * The url paths will determine which method to render.
* *
* For example: * For example:
@@ -128,18 +128,18 @@ SimpleRouter::group(['middleware' => 'Middlewares\Site', 'exceptionHandler' => '
* *
* etc. * etc.
*/ */
SimpleRouter::controller('/animals', 'ControllerAnimals'); SimpleRouter::controller('/animals', 'ControllerAnimals');
/** /**
* Example of providing callback instead of Controller * Example of providing callback instead of Controller
*/ */
SimpleRouter::post('/something', function() { SimpleRouter::post('/something', function() {
die('Callback example'); die('Callback example');
}); });
}); });
@@ -192,7 +192,7 @@ class CustomExceptionHandler implements IExceptionHandler
} }
throw $error; throw $error;
} }
} }
@@ -208,7 +208,7 @@ Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) { Route::get('user/{id}', function ($account, $id) {
// Do stuff... // Do stuff...
}); });
}); });
``` ```
@@ -229,11 +229,11 @@ $router = Router::getInstance();
$route = new RouteUrl('/answer/1', function() { $route = new RouteUrl('/answer/1', function() {
die('this callback will match /answer/1'); die('this callback will match /answer/1');
}); });
$route->setMiddleware('\Demo\Middlewares\AuthMiddleware'); $route->setMiddleware('\Demo\Middlewares\AuthMiddleware');
$route->setNamespace('MyWebsite'); $route->setNamespace('\Demo\Controllers');
$route->setPrefix('v1'); $route->setPrefix('v1');
/* Add the route to the router */ /* Add the route to the router */
@@ -257,7 +257,7 @@ class Router extends SimpleRouter {
// change this to whatever makes sense in your project // change this to whatever makes sense in your project
require_once 'routes.php'; require_once 'routes.php';
// change default namespace for all routes // change default namespace for all routes
parent::setDefaultNamespace('\Demo'); parent::setDefaultNamespace('\Demo');
@@ -419,9 +419,9 @@ Querystrings are ignored.
use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\Http\Middleware\BaseCsrfVerifier;
class CsrfVerifier extends BaseCsrfVerifier { class CsrfVerifier extends BaseCsrfVerifier {
protected $except = [ protected $except = [
'/companies/*', '/companies/*',
'/api', '/api',
]; ];
@@ -478,9 +478,9 @@ By doing this the route will now load the url ```/article/view/1``` instead of `
The last thing we need to do, is to add our custom boot-manager to the ```routes.php``` file. You can create as many bootmanagers as you like and easily add them in your ```routes.php``` file. The last thing we need to do, is to add our custom boot-manager to the ```routes.php``` file. You can create as many bootmanagers as you like and easily add them in your ```routes.php``` file.
## Easily overwrite route about to be loaded ## Easily overwrite route about to be loaded
Sometimes it can be useful to manipulate the route about to be loaded. Sometimes it can be useful to manipulate the route about to be loaded.
simple-php-router allows you to easily change the route about to be executed. simple-php-router allows you to easily change the route about to be executed.
All information about the current route is stored in the ```\Pecee\SimpleRouter\Router``` instance's `loadedRoute` property. All information about the current route is stored in the ```\Pecee\SimpleRouter\Router``` instance's `loadedRoute` property.
For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`. For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`.
@@ -500,7 +500,7 @@ $route->setMethod('hello');
### Examples ### Examples
It's only possible to change the route BEFORE the route has initially been loaded. If you want to redirect to another route, we highly recommend that you It's only possible to change the route BEFORE the route has initially been loaded. If you want to redirect to another route, we highly recommend that you
modify the `IRoute` object from a `Middleware` or `ExceptionHandler`, like the examples below. modify the `IRoute` object from a `Middleware` or `ExceptionHandler`, like the examples below.
#### Rewriting to new route #### Rewriting to new route
@@ -520,11 +520,11 @@ use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomMiddleware implements Middleware { class CustomMiddleware implements Middleware {
public function handle(Request $request, ILoadableRoute &$route) { public function handle(Request $request, ILoadableRoute &$route) {
$request->setUri(url('home')); $request->setUri(url('home'));
} }
} }
``` ```
@@ -547,12 +547,12 @@ use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomMiddleware implements Middleware { class CustomMiddleware implements Middleware {
public function handle(Request $request, ILoadableRoute &$route) { public function handle(Request $request, ILoadableRoute &$route) {
$route->callback('DefaultController@home'); $route->callback('DefaultController@home');
} }
} }
``` ```