2

我有我的死记硬背的配置:

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

以及为什么我试图去 admin/controller/some_action?id=234234234,我有一个错误:

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

我的配置有什么问题?

4

2 回答 2

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',
                        )
                    )
                ),
            ),
        ),
于 2012-11-06T10:59:21.630 回答
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_-]*',
于 2012-11-06T14:17:35.847 回答