I have a problem that I have not been able to solve, I have a route with a child, like this:
'apps' => array(
'type' => 'Segment',
'options' => array(
'route' => '/apps[/[:action[/:id]]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Apps\Controller\Index',
'action' => 'index'
),
),
'may_terminate' => true,
'child_routes' => array(
'ximages' => array(
'type' => 'Segment',
'options' => array(
'route' => '/ximages[/[:action[/:id_ximage]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Apps\Controller\xImages',
'action' => 'index'
)
),
),
),
),
Now in the file .phtml
I want to access the path http://miserver/apps/view/1/ximages/images/1
and what I do is the following:
echo $this->url('apps/ximages', array(
'action' => 'view',
'id' => 1,
'id_ximage' => 2
));
But that code only prints the route http://miserver/apps/view/1/ximages/index/1
and I don't know how the name action
of the child route can be passed to it.
In theory, the array that you are going to pass to
$this->url(...)
should not have twokey
equal ones, I think that could cause errors when mounting the url, and even more so when it comes to the part that resolvescontroller
andaction
, which should not produce ambiguities.The route you are mounting should have all these parameters:
Which should not be possible due to the aforementioned. Not being able to resolve the second action, it takes the one with the default
route
which is theindex
.Something that occurs to me would be to pass the parameters of the parent route to the child route by modifying the
route
.Calling it like this:
Again, in theory, having the parent 's
action
e -parameters set to may or may not be present in the , should resolve to the path you're looking for.id
route
url
I haven't tested it, but because of how zf2 resolves paths it should be an alternative to your question.
If not, and when mounting the url it asks you that the parameters of the parent route are missing, you should change the route construction approach and mount the classic one.
'route' => ':controller/:action/:id1/:id2/...'
It depends on how the routes are planned, they can be one more element to work with, or they can cause you real headaches.
EDIT
I have set up a test module and the problem is what it was saying from the beginning. Two equal keys in el
route
generates problems, since it resolvesaction
del firstroute
apps
and defaults toaction
delroute
apps/ximages
.action
With the approach that I carry out, despite being optional parameters, the e parameters must necessarily be passedid
if they are variables and a default value is not passed.So if you want to maintain this strategy
routes
, with variable parameters, (especially theaction
), you would have to find a way to inject the parameters of theroute
apps
and then mount theViewHelper
$this->url(...)
. Solution that is going to be more tedious to build and maintain, and I don't know if it would work correctly.In particular, I would solve it by putting the
route
parallel toapps
instead of as a child.and ride the
ViewHelper