mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Development
- Optimized the way parameters are parsed as a result, simple-router now supports routes like `/{param1}-{param2}.json`.
- Replaced reg-ex for parameter-matching with `\w` which means that default parameter matching on routes now include `_` (underscore) per default.
- Simplified `MiddlewareTest` class.
This commit is contained in:
@@ -131,6 +131,14 @@ 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);
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ abstract class RouterEntry {
|
|||||||
return new $name();
|
return new $name();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseParameters($route, $url, $parameterRegex = '[a-z0-9]+') {
|
protected function parseParameters($route, $url, $parameterRegex = '[\w]+') {
|
||||||
$parameterNames = array();
|
$parameterNames = array();
|
||||||
$regex = '';
|
$regex = '';
|
||||||
$lastCharacter = '';
|
$lastCharacter = '';
|
||||||
@@ -288,7 +288,7 @@ abstract class RouterEntry {
|
|||||||
$regex .= '(?:\\/?(?P<'.$parameter.'>[^\/]+)?\\/?)';
|
$regex .= '(?:\\/?(?P<'.$parameter.'>[^\/]+)?\\/?)';
|
||||||
$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 +307,7 @@ 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);
|
||||||
|
|||||||
@@ -20,17 +20,16 @@ class MiddlewareTest extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||||
|
|
||||||
|
$found = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||||
}catch(Exception $e) {
|
}catch(Exception $e) {
|
||||||
$this->assertTrue(($e instanceof MiddlewareLoadedException));
|
$found = ($e instanceof MiddlewareLoadedException);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Exception('Middleware not loaded');
|
$this->assertTrue($found);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user