diff --git a/README.md b/README.md index 28d5844..ef10ef5 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe - [Optional parameters](#optional-parameters) - [Regular expression constraints](#regular-expression-constraints) - [Regular expression route-match](#regular-expression-route-match) + - [Custom regex for matching parameters](#custom-regex-for-matching-parameters) - [Named routes](#named-routes) - [Generating URLs To Named Routes](#generating-urls-to-named-routes) - [Router groups](#router-groups) @@ -381,7 +382,7 @@ The example below is using the following regular expression: `/ajax/([\w]+)/?([0 **Matches:** `/ajax/abc/`, `/ajax/abc/123/` -**Doesn't match:** `/ajax/` +**Won't match:** `/ajax/` Match groups specified in the regex will be passed on as parameters: @@ -392,6 +393,35 @@ SimpleRouter::all('/ajax/abc/123', function($param1, $param2) { })->setMatch('/\/ajax\/([\w]+)\/?([0-9]+)?\/?/is'); ``` +### Custom regex for matching parameters + +By default simple-php-router uses the `\w` regular expression when matching parameters. +This decision was made with speed and reliability in mind, as this match will match both letters, number and most of the used symbols on the internet. + +However, sometimes it can be necessary to add a custom regular expression to match more advanced characters like `-` etc. + +Instead of adding a custom regular expression to all your parameters, you can simply add a global regular expression which will be used on all the parameters on the route. + +**Note:** If you the regular expression to be available across, we recommend using the global parameter on a group as demonstrated in the examples below. + +#### Example + +This example will ensure that all parameters use the `[\w\-]+` regular expression when parsing. + +```php +SimpleRouter::get('/path/{parameter}', 'VideoController@home', ['defaultParameterRegex' => '[\w\-]+']); +``` + +You can also apply this setting to a group if you need multiple routes to use your custom regular expression when parsing parameters. + +```php +SimpleRouter::group(['defaultParameterRegex' => '[\w\-]+'], function() { + + SimpleRouter::get('/path/{parameter}', 'VideoController@home'); + +}); +``` + ## Named routes Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition: @@ -1102,35 +1132,6 @@ $router->addRoute($route); This section contains advanced tips & tricks on extending the usage for parameters. -### Custom default regex for matching parameters - -By default simple-php-router uses the `\w` regular expression when matching parameters. -This decision was made with speed and reliability in mind, as this match will match both letters, number and most of the used symbols on the internet. - -However, sometimes it can be nessesary to add a custom regular expression to match more advanced characters like `-` etc. - -Instead of adding a custom regular expression to all your parameters, you can simply add a global regular expression which will be used on all the parameters on the route. - -**Note:** If you the regular expression to be available across, we recommend using the global parameter on a group as demonstrated in the examples below. - -#### Route - -This example will ensure that all parameters use the `[\w\-]+` regular expression when parsing. - -```php -SimpleRouter::get('/path/{parameter}', 'VideoController@home', ['defaultParameterRegex' => '[\w\-]+']); -``` - -You can also apply this setting to a group if you need multiple routes to use your custom regular expression when parsing parameters. - -```php -SimpleRouter::group(['defaultParameterRegex' => '[\w\-]+'], function() { - - SimpleRouter::get('/path/{parameter}', 'VideoController@home'); - -}); -``` - ## Extending This is a simple example of an integration into a framework. diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 079a4f9..c9d7ce6 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -135,7 +135,7 @@ abstract class Route implements IRoute } } - $regex = sprintf('\-?\/?(?P<%s>%s)', $name, $regex) . $parameters[2][$key]; + $regex = sprintf('(?:\/|\-)' . $parameters[2][$key] . '(?P<%s>%s)', $name, $regex) . $parameters[2][$key]; } @@ -148,7 +148,9 @@ abstract class Route implements IRoute $urlRegex = preg_quote($route, '/'); } - if (preg_match('/^' . $urlRegex . '(\/?)$/', $url, $matches) > 0) { + echo $urlRegex . '\/? | ' . $url . chr(10); + + if (preg_match('/^' . $urlRegex . '\/?/', $url, $matches) > 0) { $values = []; diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 01f898e..e404538 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -11,6 +11,7 @@ use Pecee\SimpleRouter\Route\IControllerRoute; use Pecee\SimpleRouter\Route\IGroupRoute; use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\IRoute; +use Pecee\SimpleRouter\Route\LoadableRoute; class Router { @@ -124,6 +125,8 @@ class Router $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri(); + + /* @var $route IRoute */ for ($i = $max; $i >= 0; $i--) { diff --git a/test/RouterUrlTest.php b/test/RouterUrlTest.php index 93e6108..6bf3b25 100644 --- a/test/RouterUrlTest.php +++ b/test/RouterUrlTest.php @@ -8,6 +8,33 @@ require_once 'Helpers/TestRouter.php'; class RouterUrlTest extends PHPUnit_Framework_TestCase { + public function testOptionalParameters() + { + + // TestRouter::get('/aviso/legal', 'DummyController@method1'); + TestRouter::get('/aviso/{aviso}', 'DummyController@method1'); + //TestRouter::get('/pagina/{pagina}', 'DummyController@method1'); + TestRouter::get('/{pagina?}', 'DummyController@method1'); + + //TestRouter::debugNoReset('/aviso/optional', 'get'); + //$this->assertEquals('/aviso/{aviso}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + + //TestRouter::debugNoReset('/pagina/optional', 'get'); + //$this->assertEquals('/pagina/{pagina}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + + //TestRouter::debugNoReset('/optional', 'get'); + //$this->assertEquals('/{pagina?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + +// New test lines + //TestRouter::debugNoReset('/avisolegal', 'get'); + //$this->assertNotEquals('/aviso/{aviso}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + + TestRouter::debugNoReset('/avisolegal', 'get'); + $this->assertEquals('/{pagina?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + + TestRouter::router()->reset(); + } + public function testSimilarUrls() { // Match normal route on alias