ZF2 fromRoute not returning default action name

301 views Asked by At

I am going to create a link like this:

somesite.com/management/users/index/delete/10

in which management is module name,users is controller, index is action name and delete is a parameter which gets id of the record. To create this link I am using this part of code :

        $delUrl = $this->url()->fromRoute('management-users', array(
                'controller' => 'users',
                'action' => 'index',
                'delete' => $siteData['id'],
        ));

My config is like below:

    'router' => array(
    'routes' => array(
        'management-index' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/management[/:controller][/:action][/delete/:delete][/:id]',
                'constraints' => array( 
                    'delete'    => '[0-9]+' ,
                    'id'    => '[0-9]+' 
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Management\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index' 
                ),
            ), 
        ),

    ),
), 

The result is this:

somesite.com/management/users/delete/10

The function omits action name because it is my default action and then gets delete as action name and I finally get 404 error. As you see I have a parameter "id" which does not let me to omit phrase "delete" from link. By omiting "delete" from url, ZF2 gets input as "id" and not "delete". I can not change my default action either. Is there any way to tell ZF2 to input action name in url?

1

There are 1 answers

1
Haver On

Rename the route key from "management-index" to "management-users" Change the controller name from "Index" to "Users" so the route will read like:

'router' => array(
    'routes' => array(
        'management-users' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/management[/:controller][/:action][/delete/:delete][/:id]',
                'constraints' => array( 
                    'delete'    => '[0-9]+' ,
                    'id'    => '[0-9]+' 
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Management\Controller',
                    'controller'    => 'Users',
                    'action'        => 'index' 
                ),
            ), 
        ),

    ),
), 

Don't forget to change your controller definition (invocable or factory) to reflect this change.