From 3a905783517b49e06502c9870af680b3a1bf73c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 3 Sep 2017 16:37:20 +0100 Subject: [PATCH] Added parameter support for Group routes. --- src/Pecee/SimpleRouter/Route/IRoute.php | 2 +- src/Pecee/SimpleRouter/Route/Route.php | 3 +- src/Pecee/SimpleRouter/Route/RouteGroup.php | 18 +++++-- .../SimpleRouter/Route/RoutePartialGroup.php | 34 +++++++++++++ src/Pecee/SimpleRouter/Router.php | 51 +++++++++---------- 5 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 src/Pecee/SimpleRouter/Route/RoutePartialGroup.php diff --git a/src/Pecee/SimpleRouter/Route/IRoute.php b/src/Pecee/SimpleRouter/Route/IRoute.php index 3c7d51c..e77946e 100644 --- a/src/Pecee/SimpleRouter/Route/IRoute.php +++ b/src/Pecee/SimpleRouter/Route/IRoute.php @@ -21,7 +21,7 @@ interface IRoute * * @param Request $request * @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException - * @return void + * @return string */ public function renderRoute(Request $request); diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 87ffca0..9545257 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -42,6 +42,7 @@ abstract class Route implements IRoute protected $defaultParameterRegex; protected $paramModifiers = '{}'; protected $paramOptionalSymbol = '?'; + protected $urlRegex = '/^%s\/?$/u'; protected $group; protected $parent; protected $callback; @@ -154,7 +155,7 @@ abstract class Route implements IRoute $urlRegex = preg_quote($route, '/'); } - if (preg_match('/^' . $urlRegex . '\/?$/u', $url, $matches) > 0) { + if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) > 0) { $values = []; diff --git a/src/Pecee/SimpleRouter/Route/RouteGroup.php b/src/Pecee/SimpleRouter/Route/RouteGroup.php index e6397b1..d15c343 100644 --- a/src/Pecee/SimpleRouter/Route/RouteGroup.php +++ b/src/Pecee/SimpleRouter/Route/RouteGroup.php @@ -48,9 +48,17 @@ class RouteGroup extends Route implements IGroupRoute */ public function matchRoute($url, Request $request) { - /* Skip if prefix doesn't match */ - if ($this->prefix !== null && stripos($url, $this->prefix) === false) { - return false; + if($this->prefix !== null) { + /* Parse parameters from current route */ + $parameters = $this->parseParameters($this->prefix, $url); + + /* If no custom regular expression or parameters was found on this route, we stop */ + if ($parameters === null) { + return false; + } + + /* Set the parameters */ + $this->setParameters((array)$parameters); } return $this->matchDomain($request); @@ -150,11 +158,11 @@ class RouteGroup extends Route implements IGroupRoute $this->setPrefix($values['prefix'] . $this->prefix); } - if (isset($values['exceptionHandler'])) { + if ($merge === false && isset($values['exceptionHandler'])) { $this->setExceptionHandlers((array)$values['exceptionHandler']); } - if (isset($values['domain'])) { + if ($merge === false &&isset($values['domain'])) { $this->setDomains((array)$values['domain']); } diff --git a/src/Pecee/SimpleRouter/Route/RoutePartialGroup.php b/src/Pecee/SimpleRouter/Route/RoutePartialGroup.php new file mode 100644 index 0000000..12613c4 --- /dev/null +++ b/src/Pecee/SimpleRouter/Route/RoutePartialGroup.php @@ -0,0 +1,34 @@ +prefix !== null) { + /* Parse parameters from current route */ + $parameters = $this->parseParameters($this->prefix, $url); + + /* If no custom regular expression or parameters was found on this route, we stop */ + if ($parameters === null) { + return false; + } + + /* Set the parameters */ + $this->setParameters((array)$parameters); + } + + return $this->matchDomain($request); + } + +} \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index ebe7672..5fd5824 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -129,15 +129,27 @@ class Router $route = $routes[$i]; + if ($parent !== null) { + + /* Add the parent route */ + $route->setParent($parent); + + /* Add/merge parent settings with child */ + $route->setSettings($parent->toArray(), true); + + } + + if ($group !== null) { + + /* Add the parent group */ + $route->setGroup($group); + } + /* @var $route IGroupRoute */ if ($route instanceof IGroupRoute) { $group = $route; - $this->processingRoute = true; - $route->renderRoute($this->request); - $this->processingRoute = false; - if ($route->matchRoute($url, $this->request) === true) { /* Add exception handlers */ @@ -147,22 +159,10 @@ class Router } } - } - - if ($group !== null) { - - /* Add the parent group */ - $route->setGroup($group); - } - - if ($parent !== null) { - - /* Add the parent route */ - $route->setParent($parent); - - /* Add/merge parent settings with child */ - $route->setSettings($parent->toArray(), true); + $this->processingRoute = true; + $route->renderRoute($this->request); + $this->processingRoute = false; } if ($route instanceof ILoadableRoute) { @@ -258,9 +258,7 @@ class Router if ($rewriteUrl !== null && $rewriteUrl !== $url) { unset($this->processedRoutes[$i]); $this->processedRoutes = array_values($this->processedRoutes); - $this->routeRequest(true); - - return; + return $this->routeRequest(true); } /* Render route */ @@ -268,8 +266,6 @@ class Router $this->request->setLoadedRoute($route); return $route->renderRoute($this->request); - - break; } } @@ -293,12 +289,15 @@ class Router $this->handleException(new NotFoundHttpException($message, 404)); } + + return null; } /** * @param \Exception $e * @throws HttpException * @throws \Exception + * @return string */ protected function handleException(\Exception $e) { @@ -335,9 +334,7 @@ class Router if ($rewriteUrl !== null && $rewriteUrl !== $url) { unset($this->exceptionHandlers[$i]); $this->exceptionHandlers = array_values($this->exceptionHandlers); - $this->routeRequest(true); - - return; + return $this->routeRequest(true); } } }