mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 15:02:18 +00:00
Bugfixes
This commit is contained in:
@@ -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
|
## Easily overwrite route about to be loaded
|
||||||
Sometimes it can be useful to manipulate the 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.
|
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()`.
|
For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`.
|
||||||
|
|
||||||
@@ -376,12 +376,19 @@ $route->setMethod('hello');
|
|||||||
|
|
||||||
### Examples
|
### 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
|
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
|
```php
|
||||||
namespace demo\Middlewares;
|
namespace demo\Middlewares;
|
||||||
@@ -393,7 +400,7 @@ use Pecee\SimpleRouter\RouterEntry;
|
|||||||
class CustomMiddleware implements Middleware {
|
class CustomMiddleware implements Middleware {
|
||||||
|
|
||||||
public function handle(Request $request, RouterEntry &$route = null) {
|
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.
|
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.
|
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
|
```php
|
||||||
namespace demo\Middlewares;
|
namespace demo\Middlewares;
|
||||||
|
|
||||||
|
|||||||
@@ -77,6 +77,10 @@ class RouterBase {
|
|||||||
*/
|
*/
|
||||||
protected $loadedRoute;
|
protected $loadedRoute;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List over route changes (to avoid looping)
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
protected $routeChanges;
|
protected $routeChanges;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
@@ -233,6 +237,7 @@ class RouterBase {
|
|||||||
$routeNotAllowed = false;
|
$routeNotAllowed = false;
|
||||||
|
|
||||||
$this->loadedRoute = $route;
|
$this->loadedRoute = $route;
|
||||||
|
|
||||||
$request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute);
|
$request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute);
|
||||||
$request = ($request === null) ? $this->request : $request;
|
$request = ($request === null) ? $this->request : $request;
|
||||||
|
|
||||||
@@ -278,13 +283,15 @@ class RouterBase {
|
|||||||
$request = ($request === null) ? $this->request : $request;
|
$request = ($request === null) ? $this->request : $request;
|
||||||
|
|
||||||
if(!in_array($request->getUri(), $this->routeChanges)) {
|
if(!in_array($request->getUri(), $this->routeChanges)) {
|
||||||
|
|
||||||
$this->routeChanges[] = $request->getUri();
|
$this->routeChanges[] = $request->getUri();
|
||||||
|
|
||||||
if($request->getUri() !== $this->request->getUri()) {
|
if($request->getUri() !== $this->request->getUri()) {
|
||||||
$this->routeRequest($request);
|
$this->routeRequest($request);
|
||||||
} else {
|
} else {
|
||||||
$this->routeChanges[] = $request->getUri();
|
|
||||||
$this->loadedRoute->renderRoute($request);
|
$this->loadedRoute->renderRoute($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,24 +26,11 @@ abstract class RouterEntry {
|
|||||||
'requestMethods' => array(),
|
'requestMethods' => array(),
|
||||||
'where' => array(),
|
'where' => array(),
|
||||||
'parameters' => array(),
|
'parameters' => array(),
|
||||||
|
'middleware' => array(),
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $callback;
|
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
|
* @param string $callback
|
||||||
* @return static
|
* @return static
|
||||||
@@ -86,21 +73,12 @@ abstract class RouterEntry {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $prefix
|
|
||||||
* @return static
|
|
||||||
*
|
|
||||||
public function setPrefix($prefix) {
|
|
||||||
$this->settings['prefix'] = '/' . ltrim($prefix, '/');
|
|
||||||
return $this;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $middleware
|
* @param string $middleware
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setMiddleware($middleware) {
|
public function setMiddleware($middleware) {
|
||||||
$this->settings['middleware'] = $middleware;
|
$this->settings['middleware'][] = $middleware;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class RouterGroup extends RouterEntry {
|
class RouterGroup extends RouterEntry {
|
||||||
|
|
||||||
protected $loadableRoute = false;
|
|
||||||
|
|
||||||
public function matchDomain(Request $request) {
|
public function matchDomain(Request $request) {
|
||||||
if($this->setting('domain') !== null) {
|
if($this->setting('domain') !== null) {
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,10 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
|
|||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $controller;
|
protected $controller;
|
||||||
protected $postMethod;
|
|
||||||
|
|
||||||
public function __construct($url, $controller) {
|
public function __construct($url, $controller) {
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
$this->postMethod = strtolower(($_SERVER['REQUEST_METHOD']) !== 'get') ? 'post' : 'get';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderRoute(Request $request) {
|
public function renderRoute(Request $request) {
|
||||||
@@ -46,8 +44,8 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function matchRoute(Request $request) {
|
public function matchRoute(Request $request) {
|
||||||
$url = parse_url(urldecode($request->getUri()));
|
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
||||||
$url = rtrim($url['path'], '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
|
|
||||||
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
||||||
|
|
||||||
@@ -63,17 +61,17 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
|
|||||||
unset($parameters['action']);
|
unset($parameters['action']);
|
||||||
|
|
||||||
// Delete
|
// 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);
|
return $this->call('destroy', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// 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);
|
return $this->call('update', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit
|
// 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);
|
return $this->call('edit', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,12 +81,12 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
if($this->postMethod === static::REQUEST_TYPE_POST) {
|
if($request->getMethod() === static::REQUEST_TYPE_POST) {
|
||||||
return $this->call('store', $parameters);
|
return $this->call('store', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show
|
// 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);
|
return $this->call('show', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,8 +109,7 @@ class RouterResource extends RouterEntry implements ILoadableRoute, IControllerR
|
|||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setUrl($url) {
|
public function setUrl($url) {
|
||||||
$url = rtrim($url, '/') . '/';
|
$this->url = rtrim($url, '/') . '/';
|
||||||
$this->url = $url;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user