Parameters are by default now using regex [\w\-]+ (supports dashes) to avoid confusion.

This commit is contained in:
Simon Sessingø
2021-03-18 03:24:47 +01:00
parent fb726c3613
commit 19b1a14dec
4 changed files with 27 additions and 9 deletions
+4 -3
View File
@@ -5,9 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="a7058529-bdc4-40b4-a50d-c50564dc83f0" name="Default" comment="">
<change beforePath="$PROJECT_DIR$/.idea/simple-php-router.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/simple-php-router.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/Http/Security/CookieTokenProvider.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/Http/Security/CookieTokenProvider.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" beforeDir="false" afterPath="$PROJECT_DIR$/src/Pecee/SimpleRouter/Route/Route.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterRouteTest.php" beforeDir="false" afterPath="$PROJECT_DIR$/tests/Pecee/SimpleRouter/RouterRouteTest.php" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -423,7 +424,7 @@
<workItem from="1535806837271" duration="204000" />
<workItem from="1543101575756" duration="1207000" />
<workItem from="1616029119335" duration="207000" />
<workItem from="1616030812009" duration="1360000" />
<workItem from="1616030812009" duration="3298000" />
</task>
<servers />
</component>
+7 -5
View File
@@ -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');
+1 -1
View File
@@ -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';
@@ -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()
{