mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Bugfixes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user