本文介绍了Zend Framework 2子正则表达式路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命与ZF2作战,我试图创建一个路由树,以便:

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


  • / manual-转到手动控制器,索引操作

  • / manual / [某物]-转到手动控制器,制造商操作

  • / manual / [something] / [else]-转到手动控制器,类别操作

  • / manual / [something] / [else] / [foo]-转到手动控制器,模型操作

  • /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-转到手动控制器,索引操作

  • / manual / [something]-转到 Manual Controller构造器,但是没有动作...

另外两个根本没有到达控制器...。

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!

这是我的控制器操作:

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


推荐答案

可以使用常规表达式。修改 module.config.php 中的路由,如下所示

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

这篇关于Zend Framework 2子正则表达式路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:15