Zend Framework 2路由动作

时间:2014-05-07 11:23:25

标签: zend-framework2

运行时我发现错误" localhost / products / edit" 我做错了什么?当然我有功能编辑 视图中的产品控制器类和edit.html。

        'products' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/products',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller' => 'Products',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/products[/:action]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),


'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Products' => 'Application\Controller\ProductsController'
    )

),

);

1 个答案:

答案 0 :(得分:1)

尝试将此作为您的子根数组:

'child_routes' => array(
                                        'view' => array(
                                                    'type' => 'segment',
                                                    'options' => array(
                                                            'route'    => '/:id',
                                                            'constraints' => array(
                                                                    'id'     => '[0-9]+',
                                                            ),
                                                            'defaults' => array(
                                                                    'action'     => 'view',
                                                            ),
                                                        ),
                                                    'may_terminate' => true,
                                                    'child_routes' => array(

                                                            'actions' => array(
                                                                    'type' => 'segment',
                                                                    'options' => array(
                                                                            'route'    => '/:action',
                                                                            'constraints' => array(
                                                                                    'id'     => '[0-9]+',
                                                                            ),
                                                                            'defaults' => array(
                                                                            'controller' => 'Application\Controller\ProductsController,
                                                                                    'action'     => 'view',                                                                             ),
                                                                    ),
                                                                ), 
                                                    ),
                                        ),
                            ),

它还需要是localhost / products / [:id] /编辑,如localhost / products / 1 /编辑不是localhost / products / edit无论如何我会想象,但我不知道你做什么做得公平。只需删除id参数和约束,它应该可以正常工作

请注意,子根操作没有重复路由,因为它是继承的...您的路由会创建/ products / products / edit