mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Merge pull request #350 from skipperbent/v3-development
Version 3.4.10.0
This commit is contained in:
@@ -33,7 +33,7 @@ abstract class Route implements IRoute
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $filterEmptyParams = false;
|
||||
protected $filterEmptyParams = true;
|
||||
|
||||
/**
|
||||
* Default regular expression used for parsing parameters.
|
||||
@@ -86,12 +86,20 @@ abstract class Route implements IRoute
|
||||
return null;
|
||||
}
|
||||
|
||||
$parameters = $this->getParameters();
|
||||
|
||||
/* Filter parameters with null-value */
|
||||
|
||||
if ($this->filterEmptyParams === true) {
|
||||
$parameters = array_filter($parameters, function ($var) {
|
||||
return ($var !== null);
|
||||
});
|
||||
}
|
||||
|
||||
/* Render callback function */
|
||||
if (is_callable($callback) === true) {
|
||||
|
||||
/* When the callback is a function */
|
||||
return call_user_func_array($callback, $this->getParameters());
|
||||
|
||||
return call_user_func_array($callback, $parameters);
|
||||
}
|
||||
|
||||
/* When the callback is a class + method */
|
||||
@@ -108,16 +116,6 @@ abstract class Route implements IRoute
|
||||
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
|
||||
}
|
||||
|
||||
$parameters = $this->getParameters();
|
||||
|
||||
/* Filter parameters with null-value */
|
||||
|
||||
if ($this->filterEmptyParams === true) {
|
||||
$parameters = array_filter($parameters, function ($var) {
|
||||
return ($var !== null);
|
||||
});
|
||||
}
|
||||
|
||||
return call_user_func_array([$class, $method], $parameters);
|
||||
}
|
||||
|
||||
|
||||
@@ -545,6 +545,16 @@ class Router
|
||||
$this->bootManagers[] = $bootManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get routes that has been processed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProcessedRoutes()
|
||||
{
|
||||
return $this->processedRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -125,6 +125,20 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
|
||||
TestRouter::debug('/my/custom-path', 'get');
|
||||
}
|
||||
|
||||
public function testParameterDefaultValue() {
|
||||
|
||||
$defaultVariable = null;
|
||||
|
||||
TestRouter::get('/my/{path?}', function($path = 'working') use(&$defaultVariable) {
|
||||
$defaultVariable = $path;
|
||||
});
|
||||
|
||||
TestRouter::debug('/my/');
|
||||
|
||||
$this->assertEquals('working', $defaultVariable);
|
||||
|
||||
}
|
||||
|
||||
public function testDefaultParameterRegex()
|
||||
{
|
||||
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);
|
||||
|
||||
Reference in New Issue
Block a user