diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f60b995..9ddbbad 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,9 +5,10 @@ - - + + + diff --git a/README.md b/README.md index 82adb72..ab5dc2a 100644 --- a/README.md +++ b/README.md @@ -522,10 +522,12 @@ SimpleRouter::all('/ajax/abc/123', function($param1, $param2) { ### Custom regex for matching parameters -By default simple-php-router uses the `\w` regular expression when matching parameters. +By default simple-php-router uses the `[\w\-]+` regular expression. It will match `A-Z`, `a-z`, `0-9`, `-` and `_` characters in 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. +However, sometimes it can be necessary to add a custom regular expression to match more advanced characters like foreign letters `æ ø å` etc. + +You can test your custom regular expression by using on the site [Regex101.com](https://www.regex101.com). 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. @@ -533,16 +535,16 @@ Instead of adding a custom regular expression to all your parameters, you can si #### Example -This example will ensure that all parameters use the `[\w\-]+` regular expression when parsing. +This example will ensure that all parameters use the `[\w\-\æ\ø\å]+` (`a-z`, `A-Z`, `-`, `_`, `0-9`, `æ`, `ø`, `å`) regular expression when parsing. ```php -SimpleRouter::get('/path/{parameter}', 'VideoController@home', ['defaultParameterRegex' => '[\w\-]+']); +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::group(['defaultParameterRegex' => '[\w\-\æ\ø\å]+'], function() { SimpleRouter::get('/path/{parameter}', 'VideoController@home'); diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index a955ff8..de3ba74 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -10,7 +10,7 @@ use Pecee\SimpleRouter\Router; abstract class Route implements IRoute { protected const PARAMETERS_REGEX_FORMAT = '%s([\w]+)(\%s?)%s'; - protected const PARAMETERS_DEFAULT_REGEX = '[\w]+'; + protected const PARAMETERS_DEFAULT_REGEX = '[\w\-]+'; public const REQUEST_TYPE_GET = 'get'; public const REQUEST_TYPE_POST = 'post'; diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index 0571799..35b88af 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -150,6 +150,21 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase $this->assertTrue(true); } + public function testParametersWithDashes() + { + + $defaultVariable = null; + + TestRouter::get('/my/{path}', function ($path = 'working') use (&$defaultVariable) { + $defaultVariable = $path; + }); + + TestRouter::debug('/my/hello-motto-man'); + + $this->assertEquals('hello-motto-man', $defaultVariable); + + } + public function testParameterDefaultValue() {