zf2路由无法正常工作

时间:2014-05-16 04:50:25

标签: php zend-framework2 zend-route

我的根网址为http://restaurent.local 我想像这样http://restaurent.local/menuedit/test/1路线。但这不起作用 这是我的代码

     'menuedit' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/menuedit[/:action][/:id]',
                'defaults' => array(
                    'controller' => 'Menu\Controller\Menu',
                    'action'     => 'menuedit',
                ),
            ),
        ),

如果有人知道这件事,请帮助我。

3 个答案:

答案 0 :(得分:0)

您正在尝试使用Literal路由类型。如果要匹配路由参数,则需要Segment路由类型...

 'menuedit' => array(
        'type' => 'Zend\Mvc\Router\Http\Segment',
        'options' => array(
            'route'    => '/menuedit[/:action][/:id]',
            'defaults' => array(
                'controller' => 'Menu\Controller\Menu',
                'action'     => 'menuedit',
            ),
        ),
    ),

请考虑阅读手册以了解差异以及它们的用途 - > http://framework.zend.com/manual/2.3/en/modules/zend.mvc.routing.html#http-route-types

答案 1 :(得分:0)

您可能还想为路线参数设置约束。我还会使用[/]选择尾随反斜杠,否则它将与/route/to/不匹配,只匹配/route/to

'menuedit' => array(
    'type' => 'segment',
    'options' => array(
        'route'    => '/menuedit[/][:action][/:id]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'     => [0-9]*'
         ),
        'defaults' => array(
            'controller' => 'Menu\Controller\Menu',
            'action'     => 'menuedit',
        ),
    ),
),

答案 2 :(得分:0)

I Literal路线无法处理params。

'type' => 'Zend\Mvc\Router\Http\Literal',

您想要编辑具有特定ID的特定菜单,这是一个参数。

对于句柄参数,您必须使用段类型。

不要忘记为您的操作和您的ID添加约束选项。更安全。