This commit is contained in:
Simon Sessingø
2016-11-16 15:15:51 +01:00
parent 8478899eb6
commit 75566dc2ba
4 changed files with 37 additions and 30 deletions
+2
View File
@@ -138,6 +138,8 @@ class CustomExceptionHandler implements IExceptionHandler {
// Throw your custom 404-page view // Throw your custom 404-page view
// - or - // - or -
// load another route with our 404 page // load another route with our 404 page
// - or -
// you can return the $request object to ignore the error and continue on rendering the route.
return $request->setUri(url('page.notfound')); return $request->setUri(url('page.notfound'));
} }
+2 -2
View File
@@ -8,9 +8,9 @@ interface IMiddleware {
/** /**
* @param Request $request * @param Request $request
* @param RouterEntry|null $route * @param RouterEntry $route
* @return Request|null * @return Request|null
*/ */
public function handle(Request $request, RouterEntry &$route = null); public function handle(Request $request, RouterEntry &$route);
} }
+20 -19
View File
@@ -129,6 +129,9 @@ class RouterBase {
if($backStack && $group !== null) { if($backStack && $group !== null) {
$route->setGroup($group); $route->setGroup($group);
} else {
$prefixes = [];
$group = null;
} }
if($route->getNamespace() === null && $this->defaultNamespace !== null) { if($route->getNamespace() === null && $this->defaultNamespace !== null) {
@@ -140,11 +143,6 @@ class RouterBase {
$route->setNamespace($namespace); $route->setNamespace($namespace);
} }
if($group !== null && $group->getPrefix() !== null && trim($group->getPrefix(), '/') !== '') {
$prefixes[] = trim($group->getPrefix(), '/');
}
$group = null;
$this->currentRoute = $route; $this->currentRoute = $route;
if($route instanceof ILoadableRoute) { if($route instanceof ILoadableRoute) {
@@ -154,7 +152,14 @@ class RouterBase {
$this->controllerUrlMap[] = $route; $this->controllerUrlMap[] = $route;
} else { } else {
if(is_callable($route->getCallback())) {
if($route instanceof RouterGroup) {
if ($route->getPrefix() !== null && trim($route->getPrefix(), '/') !== '') {
$prefixes[] = trim($route->getPrefix(), '/');
}
if (is_callable($route->getCallback())) {
$route->renderRoute($this->request); $route->renderRoute($this->request);
@@ -173,6 +178,7 @@ class RouterBase {
} }
} }
} }
}
$this->currentRoute = null; $this->currentRoute = null;
@@ -191,16 +197,13 @@ class RouterBase {
$this->loadedRoute = null; $this->loadedRoute = null;
$routeNotAllowed = false; $routeNotAllowed = false;
// Create a fictive request - so it can be changed in the middleware or exceptionhandler later on...
$request = clone $this->request;
try { try {
// Initialize boot-managers // Initialize boot-managers
if(count($this->bootManagers)) { if(count($this->bootManagers)) {
/* @var $manager RouterBootManager */ /* @var $manager RouterBootManager */
foreach($this->bootManagers as $manager) { foreach($this->bootManagers as $manager) {
$request = $manager->boot($request); $this->request = $manager->boot($this->request);
if(!($this->request instanceof Request)) { if(!($this->request instanceof Request)) {
throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.'); throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.');
@@ -220,16 +223,14 @@ class RouterBase {
} }
} }
$request = ($newRequest !== null) ? $newRequest : $request;
/* @var $route RouterEntry */ /* @var $route RouterEntry */
for ($i = 0; $i < count($this->controllerUrlMap); $i++) { for ($i = 0; $i < count($this->controllerUrlMap); $i++) {
$route = $this->controllerUrlMap[$i]; $route = $this->controllerUrlMap[$i];
if ($route->matchRoute($request)) { if ($route->matchRoute($this->request)) {
if (count($route->getRequestMethods()) && !in_array($request->getMethod(), $route->getRequestMethods())) { if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
$routeNotAllowed = true; $routeNotAllowed = true;
continue; continue;
} }
@@ -238,16 +239,16 @@ class RouterBase {
$this->loadedRoute = $route; $this->loadedRoute = $route;
$request = $this->loadedRoute->loadMiddleware($request, $this->loadedRoute); $request = clone $this->request;
$request = ($request === null) ? $this->request : $request; $this->loadedRoute->loadMiddleware($request, $this->loadedRoute);
if($request !== null && $request->getUri() !== $this->request->getUri() && !in_array($request->getUri(), $this->routeChanges)) { if($request->getUri() !== $this->request->getUri() && !in_array($request->getUri(), $this->routeChanges)) {
$this->routeChanges[] = $request->getUri(); $this->routeChanges[] = $request->getUri();
$this->routeRequest($request); $this->routeRequest($request);
return; return;
} }
$this->loadedRoute->renderRoute($request); $this->loadedRoute->renderRoute($this->request);
break; break;
} }
@@ -262,7 +263,7 @@ class RouterBase {
} }
if($this->loadedRoute === null) { if($this->loadedRoute === null) {
$this->handleException(new RouterException(sprintf('Route not found: %s', $request->getUri()), 404)); $this->handleException(new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404));
} }
} }
+4
View File
@@ -64,6 +64,10 @@ class GroupTest extends PHPUnit_Framework_TestCase {
public function testUrls() { public function testUrls() {
\Pecee\SimpleRouter\RouterBase::getInstance()->reset();
\Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/fancy/url/1');
\Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get');
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']); \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']);
\Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2'); \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2');