diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index e02a710..db19f0f 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -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); } diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php index 65ec83d..c503b28 100644 --- a/test/RouterRouteTest.php +++ b/test/RouterRouteTest.php @@ -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\-]+']);