-
-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Feature Request
| Q | A |
|---|---|
| New Feature | yes |
| RFC | yes |
| BC Break | no |
Summary
With a route defined with optional parameters:
$route = $app->get('/files[/:controller][/:action][/:projects_id][/:file][/:revision]', $middleware, 'files');
$route->setOptions(
[
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'projects_id' => '[0-9]+',
'file' => '[0-9]+',
'revision' => '[a-zA-Z0-9]*',
],
'defaults' => [
'controller' => 'Index',
'action' => 'index',
'projects_id' => '',
'file' => '',
'revision' => '',
],
]
);
the vendor/laminas/laminas-router/src/Http/Segment.php file performs assembly in a way that adds unnecessary slashes.
See the following examples:
$this->url('files', ['projects_id' => 123, 'file' => 456, 'revision' => 789])
produces
/files/Index/index/123/456/789
(correct, both controller and index are substituted from default values).
However, the helper with parameters
$this->url('files', ['projects_id' => 123])
produces
/files/Index/index/123//
(incorrect, as values for file and revision should be empty and therefore skipped along with their slashes).
The expected route assembly should read /files/Index/index/123 as it was done in Laminas and MVC router.
We are trying to migrate from Laminas to Mezzio, and the routing is an important path of our bridge.
The Segment.php file in buildPath function catches the optional parameters correctly, ending up in the following code:
case 'optional':
$skippable = true;
$optionalPart = $this->buildPath($part[1], $mergedParams, true, $hasChild, $options);
if ($optionalPart !== '') {
$path .= $optionalPart;
$skip = false;
}
break;
At the same time, the $optionalPart is never going to be empty, as it holds the / value, therefore producing an incorrect url.
I am aware that ideally we should divide it into separate routes, but given the amount of various usages of url helpers in all views across the app it would be quite a tedious task.
If in Segment.php the if would be changed from
$optionalPart !== ''
to
$optionalPart !== '' && $optionalPart !== '/'
that would result in optional parameters being really skipped, without adding only the slash to the url.
In case this solution is not possible, I would really appreciate suggesting me some alternative solutions on how to migrate Laminas routers to Mezzio ones in the most effective way. Many thanks in advance.