diff --git a/README.md b/README.md index 867b602..cb773b7 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,24 @@ Register the new class in your ```routes.php```, custom ```Router``` class or wh SimpleRouter::csrfVerifier(new \Demo\Middleware\CsrfVerifier()); ``` +## Easily overwrite route about to be loaded +Sometimes it can be useful to manipulate the route that's about to be loaded, for instance if a user is not authenticated or if an error occurred within your Middleware that requires +some other route to be initialised. 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\Http\Request``` object. + +**Note:** Please note that it's only possible to change the route BEFORE any route has initially been loaded, so doing this in your custom ExceptionHandler or Middleware is highly recommended. + +```php +$route = Request::getInstance()->getLoadedRoute(); + +$route->setCallback('Example\MyCustomClass@hello'); + +// -- or -- + +$route->setClass('Example\MyCustomClass'); +$route->setMethod('hello'); +``` + ## Documentation While I work on a better documentation, please refer to the Laravel 5 routing documentation here: @@ -278,6 +296,9 @@ http://laravel.com/docs/5.1/routing ## Easily extendable The router can be easily extended to customize your needs. +## Ideas and issues +If you want a great new feature or experience any issues what-so-ever, please feel free to leave an issue and i'll look into it whenever possible. + ## The MIT License (MIT) Copyright (c) 2015 Simon Sessingø / simple-php-router diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 7117eac..180b8f1 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -1,6 +1,8 @@ data[$name]) ? $this->data[$name] : null; } + /** + * Get the currently loaded route. + * @return \Pecee\SimpleRouter\RouterEntry + */ + public function getLoadedRoute() { + return $this->loadedRoute; + } + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 90af6dc..5fdd7aa 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -15,7 +15,6 @@ class RouterBase { protected $processedRoutes; protected $controllerUrlMap; protected $backStack; - protected $loadedRoute; protected $defaultNamespace; protected $baseCsrfVerifier; @@ -144,9 +143,10 @@ class RouterBase { $routeNotAllowed = false; - $this->loadedRoute = $route; + $this->request->loadedRoute = $route; $route->loadMiddleware($this->request); - $route->renderRoute($this->request); + + $this->request->loadedRoute->renderRoute($this->request); break; } } @@ -155,7 +155,7 @@ class RouterBase { throw new RouterException('Route or method not allowed', 403); } - if(!$this->loadedRoute) { + if(!$this->request->loadedRoute) { throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404); } } @@ -178,8 +178,8 @@ class RouterBase { * @return RouterEntry */ public function getLoadedRoute() { - if(!($this->loadedRoute instanceof RouterGroup)) { - return $this->loadedRoute; + if(!($this->request->loadedRoute instanceof RouterGroup)) { + return $this->request->loadedRoute; } return null; } @@ -327,8 +327,8 @@ class RouterBase { return $url; } - if($controller === null && $this->loadedRoute !== null) { - return $this->processUrl($this->loadedRoute, $this->loadedRoute->getMethod(), $parameters, $getParams); + if($controller === null && $this->request->loadedRoute !== null) { + return $this->processUrl($this->request->loadedRoute, $this->request->loadedRoute->getMethod(), $parameters, $getParams); } $c = ''; diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index 2f59b7c..4700b59 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -77,6 +77,16 @@ abstract class RouterEntry { return null; } + public function setMethod($method) { + $this->callback = sprintf('%s@%s', $this->getClass(), $method); + return $this; + } + + public function setClass($class) { + $this->callback = sprintf('%s@%s', $class, $this->getMethod()); + return $this; + } + /** * @param string $prefix * @return self