1

我一直在拼命地与 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();
}
4

1 回答 1

2

可以使用正则表达式来完成。在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%',
    ),
),
于 2013-02-26T17:09:16.967 回答