Zend Framework 2 Wildcard Route

2.5k views Asked by At

I have the following route in my module.config.php:

'routes' => array(
    'admin' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/admin[/:controller[/:action]][/]',
            'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
            ),
            'defaults' => array(
                '__NAMESPACE__' => 'Admin\Controller',
                'module' => 'Admin',
                'controller' => 'Index',
                'action' => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'wildcard' => array(
                'type' => 'Wildcard',
            )
        ),
        'priority' => 1000
    ),
),

The reason for the [/] in the end of the route is in the question: Zend Framework 2 Segment Route matching 'test' but not 'test/'

I want this route to be like in ZF1. I want to pass $_GET parameters in it (like /id/1/test/2/).

The problem it this route is actually matching /admin/customer/edit//id/20 but not matching /admin/customer/edit/id/20

Any ideas?

3

There are 3 answers

2
Mohammad Mehdi Habibi On

You can add route parameter for id :

'route' => '/admin[/:controller[/:action]][/:id][/]',
    'constraints' => array(
         'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
         'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
         'id' => '[0-9]+',
),

this route matchs admin/customer/edit/20/, so you can get id in controller:

$this->params()->fromRoute('id');

If you want to have admin/customer/edit/id/20/ ,try:

'route' => '/admin[/:controller[/:action]][id/:id][/]',
0
Achim On

You are on the right track! Use "Wilcard" as a type of child-route to your admin-route.

There are two options available: key_value_delimiter and param_delimiter. Both's default values are '/' which is equal to the ZF1 default behaviour of route parameters.

'router' => array(
    'routes' => array(
        'admin' => array(
            'type' => 'Segment',
            'options' => array(                
                'route' => '/admin[/:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
                ),
            ),
            'defaults' => array(
                '__NAMESPACE__' => 'Admin\Controller',
                'module' => 'Admin',
                'controller' => 'Index',
                'action' => 'index',
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'wildcard' => array(
                    'type' => 'Wildcard',
                    'options' => array(
                       'key_value_delimiter' => '/',
                       'param_delimiter' => '/'
                    )
                )
            )
        )
    )
)

If you want to address the wildcard route with the help of the url view-helper, you can use it like that:

$this->url('admin/wildcard', array('id' => 1234, 'foo' => 'bar'));
0
lawrenceshen On

If I am understanding correctly, you are trying to get multiple parameters from the URL, right?

e.g.

Traditional GET: www.domain.com/ctrl/action?key1=abc&key2=efg& ... & keyN=xyz

ZF2 route: www.domain.com/ctrl/action/key1/abc/key2/efg/.../keyN/xyz

If so, this is one way of doing it:

            'adminPage' => array(
            'type' => 'regex',
            'options' => array(
                'regex' => '/admin/customer/edit[/](?<keyValuePairs>.*)',
                'defaults' => array(
                    'controller' => 'YourProject\Controller\YourController',
                    'action' => 'yourAction',
                ),
                'spec' => '/admin/customer/edit/%keyValuePairs%',
            )

With this, every character after 'admin/customer/edit/' will be stored in 'keyValuePairs' parameter. In Controller::yourAction, get the 'keyValuePairs' parameter, then split the string back into a meaningful key-value data structure.