From aeacda1812f26b8a44c5671ec2407fdd8c5ba942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Fri, 15 Jan 2016 11:06:13 +0100 Subject: [PATCH] [FEATURE] Added option to change route and get information about current route through the Request object. - Updated documentation to relfect new changes. --- README.md | 21 +++++++++++++++++++++ src/Pecee/Http/Request.php | 11 +++++++++++ src/Pecee/SimpleRouter/RouterEntry.php | 10 ++++++++++ 3 files changed, 42 insertions(+) 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/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