【问题标题】:Zend Framework 2 Child Regex RoutingZend Framework 2 子正则表达式路由
【发布时间】:2013-02-25 21:44:30
【问题描述】:

我一直在拼命和ZF2战斗,我正在尝试创建一个路由树,以便:

  • /manual - 转到手动控制器,索引操作
  • /manual/[something] - 转到手动控制器,制造商操作
  • /manual/[something]/[else] - 转到手动控制器,类别操作
  • /manual/[something]/[else]/[foo] - 转到手动控制器,模型动作

我使用过官方文档和其他几个网站,但我所能做的就是触发:

  • /manual - 转到手动控制器,索引操作
  • /manual/[something] - 转到手动控制器构造函数,但不是动作...

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

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

提前感谢您的帮助,任何帮助将不胜感激!

更新:

这是我的控制器操作:

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

【问题讨论】:

    标签: php regex routing tree zend-framework2


    【解决方案1】:

    可以使用正则表达式来完成。如下修改 module.config.php 中的路由

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

    【讨论】:

    • 嗨 Finny,除了“制造商”路线不起作用外,这几乎完美无缺,它可以到达控制器,但无法发送请求。我在上面添加了控制器动作,但它非常标准......
    • 嗨,Ben,你能提供一些关于你遇到的问题的细节吗?喜欢您尝试访问的网址和更新后的路线?
    • 嗨 Finny,我更新的路线与您上面的完全一样。我的 URL 将是 v2.mydomain/manual/nikon ,不管我在 URL 的最后部分输入什么,它确实到达了“制造商操作”...
    • @Ben 您必须捕获路线并采取适当的措施。在manufacturerAction() 中使用以下获取路由参数; $manufacturer = $this->params()->fromRoute('manufacturer'); 根据给出的 url; $manufacturer 将填充为 nikon
    猜你喜欢
    • 2014-05-02
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多