diff --git a/README.md b/README.md index 282f7f6..53af7df 100644 --- a/README.md +++ b/README.md @@ -356,7 +356,7 @@ The last thing we need to do, is to add our custom boot-manager to the ```routes ## Easily overwrite 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. -All information about the current route is stored in the ```\Pecee\SimpleRouter\RouterBase``` instance. +All information about the current route is stored in the ```\Pecee\SimpleRouter\RouterBase``` instance's `loadedRoute` property. For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`. @@ -376,12 +376,19 @@ $route->setMethod('hello'); ### Examples -#### Faking new route 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 `RouterRoute` object from a `Middleware` or `ExceptionHandler`, for like the examples below. +modify the `RouterEntry` object from a `Middleware` or `ExceptionHandler`, like the examples below. + +#### Faking new route + +The example below will cause the router to re-route the request with another url. We are using the `url()` helper function to get the uri to another route added in the `routes.php` file. + +This does require the `$request` object to be returned, otherwise the `request` object will be ignored by the router. + +Using the example below will NOT inherit the rules from the other route. This means that IF you are faking a route that is enabled in `post`. + +**NOTE: Use this method if you want to fully load a route (middlewares, request-method etc. will be kept).** -The example below will cause the router to re-route the request with the "fake" uri. This does require the `$request` object to be returned, -otherwise the `request` object will be ignored by the router. ```php namespace demo\Middlewares; @@ -393,7 +400,7 @@ use Pecee\SimpleRouter\RouterEntry; class CustomMiddleware implements Middleware { public function handle(Request $request, RouterEntry &$route = null) { - return $request->setUri('/home'); + return $request->setUri(url('home')); } } @@ -407,6 +414,8 @@ on some criteria's for the request. The callback below will fire immediately after the `Middleware` or `ExceptionHandler` has been loaded, as they are loaded before the route is rendered. If you wish to change the callback from outside, please have this in mind. +**NOTE: Use this method if you want to load another controller. No additional middlewares or rules will be loaded.** + ```php namespace demo\Middlewares; diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index e3f267b..c350c74 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -77,6 +77,10 @@ class RouterBase { */ protected $loadedRoute; + /** + * List over route changes (to avoid looping) + * @var array + */ protected $routeChanges; public function __construct() { @@ -233,6 +237,7 @@ class RouterBase { $routeNotAllowed = false; $this->loadedRoute = $route; + $request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute); $request = ($request === null) ? $this->request : $request; @@ -278,13 +283,15 @@ class RouterBase { $request = ($request === null) ? $this->request : $request; if(!in_array($request->getUri(), $this->routeChanges)) { + $this->routeChanges[] = $request->getUri(); + if($request->getUri() !== $this->request->getUri()) { $this->routeRequest($request); } else { - $this->routeChanges[] = $request->getUri(); $this->loadedRoute->renderRoute($request); } + return; } diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index 3ed29b2..376ed7d 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -26,24 +26,11 @@ abstract class RouterEntry { 'requestMethods' => array(), 'where' => array(), 'parameters' => array(), + 'middleware' => array(), ]; protected $callback; - /** - * Returns callback name/identifier for the current route based on the callback. - * Useful if you need to get a unique identifier for the loaded route, for instance - * when using translations etc. - * - * @return string - */ - public function getIdentifier() { - if(strpos($this->callback, '@') !== false) { - return $this->callback; - } - return 'function_' . md5($this->callback); - } - /** * @param string $callback * @return static @@ -86,21 +73,12 @@ abstract class RouterEntry { return $this; } - /** - * @param string $prefix - * @return static - * - public function setPrefix($prefix) { - $this->settings['prefix'] = '/' . ltrim($prefix, '/'); - return $this; - }*/ - /** * @param string $middleware * @return static */ public function setMiddleware($middleware) { - $this->settings['middleware'] = $middleware; + $this->settings['middleware'][] = $middleware; return $this; } diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index 0c21f1a..8e29572 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -7,8 +7,6 @@ use Pecee\Http\Request; class RouterGroup extends RouterEntry { - protected $loadableRoute = false; - public function matchDomain(Request $request) { if($this->setting('domain') !== null) { diff --git a/src/Pecee/SimpleRouter/RouterResource.php b/src/Pecee/SimpleRouter/RouterResource.php index 9829c43..8fe4a18 100644 --- a/src/Pecee/SimpleRouter/RouterResource.php +++ b/src/Pecee/SimpleRouter/RouterResource.php @@ -8,12 +8,10 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR protected $url; protected $controller; - protected $postMethod; public function __construct($url, $controller) { $this->url = $url; $this->controller = $controller; - $this->postMethod = strtolower(($_SERVER['REQUEST_METHOD']) !== 'get') ? 'post' : 'get'; } public function renderRoute(Request $request) { @@ -46,8 +44,8 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR } public function matchRoute(Request $request) { - $url = parse_url(urldecode($request->getUri())); - $url = rtrim($url['path'], '/') . '/'; + $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); + $url = rtrim($url, '/') . '/'; $route = rtrim($this->url, '/') . '/{id?}/{action?}'; @@ -63,17 +61,17 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR unset($parameters['action']); // Delete - if($request->getMethod() === static::REQUEST_TYPE_DELETE && $this->postMethod === static::REQUEST_TYPE_POST) { + if($request->getMethod() === static::REQUEST_TYPE_DELETE && $request->getMethod() === static::REQUEST_TYPE_POST) { return $this->call('destroy', $parameters); } // Update - if(in_array($request->getMethod(), array(static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT)) && $this->postMethod === static::REQUEST_TYPE_POST) { + if(in_array($request->getMethod(), array(static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT)) && $request->getMethod() === static::REQUEST_TYPE_POST) { return $this->call('update', $parameters); } // Edit - if(isset($action) && strtolower($action) === 'edit' && $this->postMethod === static::REQUEST_TYPE_GET) { + if(isset($action) && strtolower($action) === 'edit' && $request->getMethod() === static::REQUEST_TYPE_GET) { return $this->call('edit', $parameters); } @@ -83,12 +81,12 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR } // Save - if($this->postMethod === static::REQUEST_TYPE_POST) { + if($request->getMethod() === static::REQUEST_TYPE_POST) { return $this->call('store', $parameters); } // Show - if(isset($parameters['id']) && $this->postMethod === static::REQUEST_TYPE_GET) { + if(isset($parameters['id']) && $request->getMethod() === static::REQUEST_TYPE_GET) { return $this->call('show', $parameters); } @@ -111,8 +109,7 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR * @return static */ public function setUrl($url) { - $url = rtrim($url, '/') . '/'; - $this->url = $url; + $this->url = rtrim($url, '/') . '/'; return $this; }