[FEATURE] Added option to change route and get information about current

route through the Request object.

- Updated documentation to relfect new changes.
This commit is contained in:
Simon Sessingø
2016-01-15 11:06:13 +01:00
parent 765204f552
commit aeacda1812
3 changed files with 42 additions and 0 deletions

View File

@@ -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

View File

@@ -1,6 +1,8 @@
<?php
namespace Pecee\Http;
use Pecee\SimpleRouter\RouterBase;
class Request {
protected static $instance;
@@ -10,6 +12,7 @@ class Request {
protected $host;
protected $method;
protected $headers;
protected $loadedRoute;
/**
* Return new instance
@@ -140,4 +143,12 @@ class Request {
return isset($this->data[$name]) ? $this->data[$name] : null;
}
/**
* Get the currently loaded route.
* @return \Pecee\SimpleRouter\RouterEntry
*/
public function getLoadedRoute() {
return $this->loadedRoute;
}
}

View File

@@ -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