I have two actions for a single route but I do not know how to arm the URL

1

I have a problem that I have not been able to solve, I have a route with son, 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 to the path http://miserver/apps/view/1/ximages/index/1 and I do not know how you can pass the name of action of the child route.

    
asked by albertcito 26.07.2016 в 03:03
source

1 answer

1

In theory, the array that you pass to $this->url(...) should not have two key equal, I think it could cause errors when mounting the url, and even more so in the case that resolves controller and action , that should not produce ambiguities.

The route you are riding should have all these parameters:

echo $this->url('apps/ximages', array(
    'action'    => 'view', // <-- parent
    'id'        => 1, 
    'action'    => 'images', // <-- child
    'id_ximage' => 2
));

Thing that should not be possible because of the aforementioned. Unable to solve the second action, take the one with the default route which is the index .

One thing that occurred to me would be to pass the parameters of the parent route to the child route by changing the route .

'child_routes' => array(
    'ximages' => array(
        'type'    => 'Segment',
        'options' => array(
            'route'       => '/:parent_action/:parent_id/ximages[/[:action[/:id_ximage]]]',
            'constraints' => array(
                'parent_action' => '[a-zA-Z][a-zA-Z0-9_-]+',
                'parent_id'     => '[0-9]+',
                'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
                'id_ximage'     => '[0-9]*',
            ),
            'defaults'    => array(
                'controller' => 'Apps\Controller\xImages',
                'action'     => 'index'
            )
        ),
    ),
)

Calling it like this:

echo $this->url('apps/ximages', array(
    'parent_action' => 'view',
    'parent_id'     => 1,
    'action'        => 'images',
    'id_ximage'     => 2
));

Again, in theory, having the parameters action e id of route parent configured as that may or may not be present in url , you should solve with the route you are looking for.

I have not tried it, but following how it solves the zf2 routes it should be an alternative to your question.

If this is not the case, and when mounting the url, it asks you for missing the parameters of the parent route, you should change the approach to the construction of the routes and mount the classic 'route' => ':controller/:action/:id1/:id2/...'

Depends on how the routes are considered, they can be another element with which to work, or generate real headaches.

EDITO

I have mounted a test module and the problem is what I had been saying since the beginning. Two equal keys in the route generates problems, since it solves first the action of route apps and leaves by default the action of route apps/ximages .

With the approach I carry out, in spite of being optional parameters, we must necessarily pass the parameters action e id if they are variable and a default value is not passed.

So if you want to keep this strategy of routes , with variable parameters, (especially the action ), you would have to find a way to inject the parameters of route apps and then mount the ViewHelper $this->url(...) . Solution that will be more tedious to build and maintain, and I do not know if it would work correctly.

I would particularly solve it by putting the route parallel to apps instead of as a child.

'apps'    => array(
    'type'    => 'Zend\Mvc\Router\Http\Segment',
    'options' => array(
        'route'       => '/apps[/[:action[/:id[/]]]]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'     => '[0-9]*',
        ),
        'defaults'    => array(
            'controller' => 'Application\Controller\Index',
            'action'     => 'index',
        ),
    ),
),

'ximages' => array(
    'type'    => 'Zend\Mvc\Router\Http\Segment',
    'options' => array(
        'route'       => '/apps/:parent_action/:parent_id/ximages[/[:action[/:id_ximage[/]]]]',
        'constraints' => array(
            'parent_action' => '[a-zA-Z][a-zA-Z0-9_-]+',
            'parent_id'     => '[0-9]+',
            'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id_ximage'     => '[0-9]*',
        ),
        'defaults'    => array(
            'controller' => 'Application\Controller\Other',
            'action'     => 'index'
        )
    ),
),

And mount the ViewHelper

$this->url('ximages', array(
    'parent_action' => 'view',
    'parent_id'     => 2,
    'action'        => 'images',
    'id_ximage'     => 9,
));
    
answered by 26.07.2016 / 12:58
source