mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
@@ -72,12 +72,14 @@ class RouterBase {
|
|||||||
array_push($newPrefixes, trim($route->getPrefix(), '/'));
|
array_push($newPrefixes, trim($route->getPrefix(), '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @var $group RouterGroup */
|
||||||
|
$group = null;
|
||||||
|
|
||||||
if(!($route instanceof RouterGroup)) {
|
if(!($route instanceof RouterGroup)) {
|
||||||
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
|
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
|
||||||
$route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() );
|
$route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() );
|
||||||
}
|
}
|
||||||
|
|
||||||
$group = null;
|
|
||||||
$this->controllerUrlMap[] = $route;
|
$this->controllerUrlMap[] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,11 +88,11 @@ class RouterBase {
|
|||||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||||
$group = $route;
|
$group = $route;
|
||||||
|
|
||||||
$route->renderRoute($this->request);
|
$group->renderRoute($this->request);
|
||||||
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
|
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||||
|
|
||||||
// Add ExceptionHandler
|
// Add ExceptionHandler
|
||||||
if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) {
|
if($group->matchRoute($this->request) && $group->getExceptionHandler() !== null) {
|
||||||
$this->exceptionHandlers[] = $route;
|
$this->exceptionHandlers[] = $route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,14 +133,6 @@ class RouterBase {
|
|||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
$this->processRoutes($this->routes);
|
$this->processRoutes($this->routes);
|
||||||
|
|
||||||
// Make sure routes with longer urls are rendered first
|
|
||||||
usort($this->controllerUrlMap, function($a, $b) {
|
|
||||||
if(strlen($a->getUrl()) < strlen($b->getUrl())) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
$routeNotAllowed = false;
|
$routeNotAllowed = false;
|
||||||
|
|
||||||
$max = count($this->controllerUrlMap);
|
$max = count($this->controllerUrlMap);
|
||||||
@@ -186,7 +180,7 @@ class RouterBase {
|
|||||||
|
|
||||||
protected function handleException(\Exception $e) {
|
protected function handleException(\Exception $e) {
|
||||||
|
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterGroup */
|
||||||
foreach ($this->exceptionHandlers as $route) {
|
foreach ($this->exceptionHandlers as $route) {
|
||||||
$route->loadMiddleware($this->request);
|
$route->loadMiddleware($this->request);
|
||||||
$handler = $route->getExceptionHandler();
|
$handler = $route->getExceptionHandler();
|
||||||
@@ -300,9 +294,7 @@ class RouterBase {
|
|||||||
if(is_array($getParams)) {
|
if(is_array($getParams)) {
|
||||||
if ($includeEmpty === false) {
|
if ($includeEmpty === false) {
|
||||||
$getParams = array_filter($getParams, function ($item) {
|
$getParams = array_filter($getParams, function ($item) {
|
||||||
if (!empty($item)) {
|
return (!empty($item));
|
||||||
return $item;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ abstract class RouterEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamicially set settings value
|
* Dynamically set settings value
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed|null $value
|
* @param mixed|null $value
|
||||||
@@ -261,12 +261,6 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
$character = $route[$i];
|
$character = $route[$i];
|
||||||
|
|
||||||
// Skip "/" if we are at the end of a parameter
|
|
||||||
if($lastCharacter === '}' && $character === '/') {
|
|
||||||
$lastCharacter = $character;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($character === '{') {
|
if($character === '{') {
|
||||||
// Remove "/" and "\" from regex
|
// Remove "/" and "\" from regex
|
||||||
if(substr($regex, strlen($regex)-1) === '/') {
|
if(substr($regex, strlen($regex)-1) === '/') {
|
||||||
@@ -285,10 +279,10 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
if($lastCharacter === '?') {
|
if($lastCharacter === '?') {
|
||||||
$parameter = substr($parameter, 0, strlen($parameter)-1);
|
$parameter = substr($parameter, 0, strlen($parameter)-1);
|
||||||
$regex .= '(?:\\/?(?P<'.$parameter.'>[^\/]+)?\\/?)';
|
$regex .= '(?:\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?)?';
|
||||||
$required = false;
|
$required = false;
|
||||||
} else {
|
} else {
|
||||||
$regex .= '.*?(?P<' . $parameter . '>'. $parameterRegex .').*?';
|
$regex .= '\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?';
|
||||||
}
|
}
|
||||||
$parameterNames[] = array('name' => $parameter, 'required' => $required);
|
$parameterNames[] = array('name' => $parameter, 'required' => $required);
|
||||||
$parameter = '';
|
$parameter = '';
|
||||||
@@ -307,7 +301,8 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
$parameterValues = array();
|
$parameterValues = array();
|
||||||
|
|
||||||
if(preg_match('/^'.$regex.'.?$/is', $url, $parameterValues)) {
|
if(preg_match('/^'.$regex.'$/is', $url, $parameterValues)) {
|
||||||
|
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
|
||||||
$max = count($parameterNames);
|
$max = count($parameterNames);
|
||||||
@@ -399,7 +394,7 @@ abstract class RouterEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get allowed requeset methods
|
* Get allowed request methods
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class RouterRoute extends RouterEntry {
|
|||||||
} else {
|
} else {
|
||||||
return strtolower($this->getAlias()) === strtolower($name);
|
return strtolower($this->getAlias()) === strtolower($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,9 @@ class DummyController {
|
|||||||
echo static::class . '@' .'start() OK';
|
echo static::class . '@' .'start() OK';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function param($params = null) {
|
||||||
|
$params = func_get_args();
|
||||||
|
echo 'Params: ' . join(', ', $params);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -66,4 +66,40 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSimpleParam() {
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\RouterBase::reset();
|
||||||
|
|
||||||
|
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||||
|
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}', 'DummyController@param');
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMultiParam() {
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\RouterBase::reset();
|
||||||
|
|
||||||
|
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||||
|
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPathParam() {
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\RouterBase::reset();
|
||||||
|
|
||||||
|
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||||
|
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
|
||||||
|
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{param}', 'DummyController@param');
|
||||||
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user