mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
@@ -33,7 +33,7 @@ abstract class Route implements IRoute
|
|||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $filterEmptyParams = false;
|
protected $filterEmptyParams = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default regular expression used for parsing parameters.
|
* Default regular expression used for parsing parameters.
|
||||||
@@ -86,12 +86,20 @@ abstract class Route implements IRoute
|
|||||||
return null;
|
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 */
|
/* Render callback function */
|
||||||
if (is_callable($callback) === true) {
|
if (is_callable($callback) === true) {
|
||||||
|
|
||||||
/* When the callback is a function */
|
/* 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 */
|
/* 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);
|
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);
|
return call_user_func_array([$class, $method], $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -545,6 +545,16 @@ class Router
|
|||||||
$this->bootManagers[] = $bootManager;
|
$this->bootManagers[] = $bootManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get routes that has been processed.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getProcessedRoutes()
|
||||||
|
{
|
||||||
|
return $this->processedRoutes;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -125,6 +125,20 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
|
|||||||
TestRouter::debug('/my/custom-path', 'get');
|
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()
|
public function testDefaultParameterRegex()
|
||||||
{
|
{
|
||||||
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);
|
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);
|
||||||
|
|||||||
Reference in New Issue
Block a user