Search code examples
url-routingzend-framework2zend-framework-mvc

Zend Framework 2 Routing Part of the action name based on route segment


Is there a way to set up a route in ZF2 (Segment or otherwise) so that /staff/list translates to the staffListAction() action in my controller?

Typically /staff-list translates to staffListAction. Is there a way to change that delimiter?

I saw this on Evan Coury's blog, but it talks about a segment, not exactly an action.

Changing segment delimeter


Solution

  • This pretty much would be a route of type literal. Something like

    'staff' => array(
        'type' => 'literal',
        'options' => array(
            'route' => '/staff',
            'defaults' => array(
                'controller' => 'namespace-controller-staff',
                'action' => 'index'
            )
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'list' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/list',
                    'defaults' => array(
                        'action' => 'staffList'
                    )
                )
            )
        )
    ),
    

    Didn't try it, but i assume it works. Personally i run with literal routes as much as possible, as they are the fastest to match.