mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 22:32:14 +00:00
Added parameter support for Group routes.
This commit is contained in:
@@ -21,7 +21,7 @@ interface IRoute
|
|||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
|
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
|
||||||
* @return void
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function renderRoute(Request $request);
|
public function renderRoute(Request $request);
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ abstract class Route implements IRoute
|
|||||||
protected $defaultParameterRegex;
|
protected $defaultParameterRegex;
|
||||||
protected $paramModifiers = '{}';
|
protected $paramModifiers = '{}';
|
||||||
protected $paramOptionalSymbol = '?';
|
protected $paramOptionalSymbol = '?';
|
||||||
|
protected $urlRegex = '/^%s\/?$/u';
|
||||||
protected $group;
|
protected $group;
|
||||||
protected $parent;
|
protected $parent;
|
||||||
protected $callback;
|
protected $callback;
|
||||||
@@ -154,7 +155,7 @@ abstract class Route implements IRoute
|
|||||||
$urlRegex = preg_quote($route, '/');
|
$urlRegex = preg_quote($route, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match('/^' . $urlRegex . '\/?$/u', $url, $matches) > 0) {
|
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) > 0) {
|
||||||
|
|
||||||
$values = [];
|
$values = [];
|
||||||
|
|
||||||
|
|||||||
@@ -48,11 +48,19 @@ class RouteGroup extends Route implements IGroupRoute
|
|||||||
*/
|
*/
|
||||||
public function matchRoute($url, Request $request)
|
public function matchRoute($url, Request $request)
|
||||||
{
|
{
|
||||||
/* Skip if prefix doesn't match */
|
if($this->prefix !== null) {
|
||||||
if ($this->prefix !== null && stripos($url, $this->prefix) === false) {
|
/* 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set the parameters */
|
||||||
|
$this->setParameters((array)$parameters);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->matchDomain($request);
|
return $this->matchDomain($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,11 +158,11 @@ class RouteGroup extends Route implements IGroupRoute
|
|||||||
$this->setPrefix($values['prefix'] . $this->prefix);
|
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['exceptionHandler'])) {
|
if ($merge === false && isset($values['exceptionHandler'])) {
|
||||||
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['domain'])) {
|
if ($merge === false &&isset($values['domain'])) {
|
||||||
$this->setDomains((array)$values['domain']);
|
$this->setDomains((array)$values['domain']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pecee\SimpleRouter\Route;
|
||||||
|
|
||||||
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
|
class PartialGroup extends RouteGroup
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Method called to check if route matches
|
||||||
|
*
|
||||||
|
* @param string $url
|
||||||
|
* @param Request $request
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function matchRoute($url, Request $request)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -129,15 +129,27 @@ class Router
|
|||||||
|
|
||||||
$route = $routes[$i];
|
$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 */
|
/* @var $route IGroupRoute */
|
||||||
if ($route instanceof IGroupRoute) {
|
if ($route instanceof IGroupRoute) {
|
||||||
|
|
||||||
$group = $route;
|
$group = $route;
|
||||||
|
|
||||||
$this->processingRoute = true;
|
|
||||||
$route->renderRoute($this->request);
|
|
||||||
$this->processingRoute = false;
|
|
||||||
|
|
||||||
if ($route->matchRoute($url, $this->request) === true) {
|
if ($route->matchRoute($url, $this->request) === true) {
|
||||||
|
|
||||||
/* Add exception handlers */
|
/* 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) {
|
if ($route instanceof ILoadableRoute) {
|
||||||
@@ -258,9 +258,7 @@ class Router
|
|||||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||||
unset($this->processedRoutes[$i]);
|
unset($this->processedRoutes[$i]);
|
||||||
$this->processedRoutes = array_values($this->processedRoutes);
|
$this->processedRoutes = array_values($this->processedRoutes);
|
||||||
$this->routeRequest(true);
|
return $this->routeRequest(true);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Render route */
|
/* Render route */
|
||||||
@@ -268,8 +266,6 @@ class Router
|
|||||||
$this->request->setLoadedRoute($route);
|
$this->request->setLoadedRoute($route);
|
||||||
|
|
||||||
return $route->renderRoute($this->request);
|
return $route->renderRoute($this->request);
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,12 +289,15 @@ class Router
|
|||||||
|
|
||||||
$this->handleException(new NotFoundHttpException($message, 404));
|
$this->handleException(new NotFoundHttpException($message, 404));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Exception $e
|
* @param \Exception $e
|
||||||
* @throws HttpException
|
* @throws HttpException
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function handleException(\Exception $e)
|
protected function handleException(\Exception $e)
|
||||||
{
|
{
|
||||||
@@ -335,9 +334,7 @@ class Router
|
|||||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||||
unset($this->exceptionHandlers[$i]);
|
unset($this->exceptionHandlers[$i]);
|
||||||
$this->exceptionHandlers = array_values($this->exceptionHandlers);
|
$this->exceptionHandlers = array_values($this->exceptionHandlers);
|
||||||
$this->routeRequest(true);
|
return $this->routeRequest(true);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user