Ben
Ben

Reputation: 261

Zend Framework 2 Child Regex Routing

I have desperately been battling with ZF2, I am trying to create a route tree, so that:

I've used the official docs and several other websites but all I've been able to do is trigger:

The other two dont reach the controller at all....

        'manual' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/manual',
                'defaults' => array(
                    'controller' => 'Applicaton\Controller\Manual',
                    'action' => 'index'
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // Segment route for viewing one blog post
                'manufacturer' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/[:manufacturer]',
                        'constraints' => array(
                            'manufacturer' => '[a-zA-Z0-9_-]+'
                        ),
                        'defaults' => array(
                            'action' => 'manufacturer'
                        )
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'category' => array(
                            'type' => 'segment',
                            'options' => array(
                                'route' => '/[:category]',
                                'constraints' => array(
                                    'category' => '[a-zA-Z0-9_-]+'
                                ),
                                'defaults' => array(
                                    'action' => 'category'
                                )
                            ),
                            'may_terminate' => true,
                            'child_routes' => array(
                                'model' => array(
                                    'type' => 'segment',
                                    'options' => array(
                                        'route' => '/[:model]',
                                        'constraints' => array(
                                            'model' => '[a-zA-Z0-9_-]+'
                                        ),
                                        'defaults' => array(
                                            'action' => 'model'
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        ),

Thanks for your help in advance, any help would be greatly appreciated!

Update:

Here is my controller action:

public function manufacturerAction() {
    echo 'I am in the manufacturer action!';
    return new ViewModel();
}

Upvotes: 1

Views: 4993

Answers (1)

Finny Abraham
Finny Abraham

Reputation: 880

It can be done using regular expressions. Modify your routes in module.config.php as follows

'manual' => array(
    'type' => 'Literal',
    'options' => array(
        'route' => '/manual',
        'defaults' => array(
            'controller' => 'Application\Controller\Manual',
            'action' => 'index',
        ),
    ),
),
'manufacturer' => array(
    'type' => 'Zend\Mvc\Router\Http\Regex',
    'options' => array(
        'regex' => '/manual/(?<manufacturer>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'Application\Controller\Manual',
            'action' => 'manufacturer',
        ),
        'spec' => '/manual/%manufacturer%',
    ),
),
'category' => array(
    'type' => 'Zend\Mvc\Router\Http\Regex',
    'options' => array(
        'regex' => '/manual/(?<manufacturer>[a-zA-Z0-9_-]+)/(?<category>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'Application\Controller\Manual',
            'action' => 'category',
        ),
        'spec' => '/manual/%manufacturer%/%category%',
    ),
),
'model' => array(
    'type' => 'Zend\Mvc\Router\Http\Regex',
    'options' => array(
        'regex' => '/manual/(?<manufacturer>[a-zA-Z0-9_-]+)/(?<category>[a-zA-Z0-9_-]+)/(?<model>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'Application\Controller\Manual',
            'action' => 'model',
        ),
        'spec' => '/manual/%manufacturer%/%category%/%model%',
    ),
),

Upvotes: 2

Related Questions