0

I am new to ZF2. I have question for url('route-name', $urlParams, $urlOptions); ?>

What should I construct $urlParams and $urlOptions when multiple controller in module?

I rename Album module as Shop and it has two controllers: indexController and VendorController. In the View>Shop>Vendor>index.phtml, I add:

<p><a href="<?php echo $this->url('shop', array('action'=>'add')); ?>">
         Add new vendor</a></p>

aiming this link would links to localhost/shop/vendor/add. But the page shows the link is: http://host.com/shop while what i want is http://host.com/shop/vendor/add

My understanding is that I should set $urlOPtions field, could anyone give me example? Thanks all Below is module.config.php:

'router' => array(
   'routes' => array(
        'shop' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/shop',
                'defaults' => array(
                    '__NAMESPACE__' => 'Shop\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Shop\Controller\Index' => 'Shop\Controller\IndexController',
        'Shop\Controller\Vendor'=> 'Shop\Controller\VendorController'
    ),
),

1 Answer 1

1

The route you're trying to use to construct the url is actually the default child route. So you should use that instead:

<p><a href="<?php echo $this->url('shop/default', array('action'=>'add')); ?>">
         Add new vendor</a></p>

Note the 'shop/default' instead of shop to target the child route.

Also you should specify the controller as a parameter, so you get something like this:

<p><a href="<?php echo $this->url('shop/default', array('controller' => 'vendor', 'action'=>'add')); ?>">
         Add new vendor</a></p>
Sign up to request clarification or add additional context in comments.

4 Comments

It works, thanks Ruben.. How about if we have arguments.. for example. for example. <a href="<?php echo $this->url('shop/default', array('controller'=>'vendor', 'action'=>'delete', 'id' => $vendor->vendorId));?>">Delete</a> only generates /shop/vendor/delete instead of /shop/vendor/delete/id/34
@shijiexu that's because the param id is not defined in the route
Thanks @Sam. I changed child router 'route' => '/[:controller[/:action][/:id]]' and the url is construct as expected. The question is that if I have multiple urls like shop/vendor/namevalue ; shop/vendor/addressValue ; and more arguments shop/vendor/namevalue1/namevalue2/nabmevalue3.. How should I update my child router ? Should I count all possible URLs pattern and construct child-router for each kind url?
iirc filter-values should be query parameters, anything else is fine for the route itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.