Zend Framework 2 Child Regex Routing

余生长醉 提交于 2019-12-11 06:32:41

问题


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

  • /manual - Goes to the Manual Controller, index action
  • /manual/[something] - Goes to the Manual Controller, manufacturer action
  • /manual/[something]/[else] - Goes to the Manual Controller, category action
  • /manual/[something]/[else]/[foo] - Goes to the Manual Controller, model action

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

  • /manual - Goes to the Manual Controller, index action
  • /manual/[something] - Goes to the Manual Controller Constructor, but not the action...

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();
}

回答1:


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%',
    ),
),


来源:https://stackoverflow.com/questions/15077075/zend-framework-2-child-regex-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!