This commit is contained in:
Simon Sessingø
2016-11-15 06:38:39 +01:00
parent 669d318a12
commit 9c413a3c53
5 changed files with 33 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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