如何查询路由zf2?

时间:2012-11-06 10:18:54

标签: routes zend-framework2

我为死记硬背配置:

  'admin' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/admin[/[:controller[/:action]]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*/?',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*/?',
                ),
                'defaults' => array(
                    'controller' => 'index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'query' => array(
                    'type' => 'Query',
                ),
            ),
        )

为什么我试图去管理/控制器/ some_action?id = 234234234,我有一个错误:

A 404 error occurred
Page not found.
The requested URL could not be matched by routing.

我的配置有什么问题?

2 个答案:

答案 0 :(得分:2)

如果使用以下参数定义路径配置:

//...
'defaults' => array(
                    'controller' => 'index',
                    'action' => 'Index',
                   ),
//...

您需要确保在控制器配置级别将控制器类与此键关联:

'controllers' => array(
        'invokables' => array(
            'index' => 'Your\Controller\Namespace\YourController',
            //...
        ),
    ),

但是,它建议定义更多“结构化”键(假设您在不同模块的级别上有不同的索引控制器......) 这可以通过添加与控制器名称空间对应的键来轻松实现:

'defaults' => array(
                    'controller' => 'Index',
                    'action' => 'Index',
                    '__NAMESPACE__'=>'Your\Controller\Namespace'
                   ),

//...

//Controllers configuration

'controllers' => array(
        'invokables' => array(
            'Your\Controller\Namespace\Index' => 'Your\Controller\Namespace\YourController',
            //...
        ),
    ),

[编辑]

尝试使用此(结构化)配置:

'admin' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                    'child_routes' => array(
                        'query'=>array(
                            'type'=>'Query',
                        )
                    )
                ),
            ),
        ),

答案 1 :(得分:0)

您可能已经定义了正则表达式:

'controller' => '[a-zA-Z][a-zA-Z0-9_-]*/?',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*/?',

这迫使你匹配一个尾部斜杠,试试这个:

'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',