Zend Framework3类似路线冲突

时间:2019-01-29 10:29:09

标签: php zend-framework routes zend-framework3

我正在使用ZF3,在Post模块的 module.config.php 文件中,我有两条路由之一,

'create-post' => [
    'type' => Literal::class,
    'options' => [
        // Change this to something specific to your module
        'route' => '/post/create',
        'defaults' => [
            'controller' => Controller\PostController::class,
            'action' => 'create',
        ]
    ],
    'may_terminate' => true,
    'child_routes' => [
        // You can place additional routes that match under the
        // route defined above here.
    ],
],

'post' => [
    'type' => Segment::class,
    'options' => [
        // Change this to something specific to your module
        'route' => '/post[/:postId]',
        'defaults' => [
            'controller' => Controller\PostController::class,
            'action' => 'show',
        ],
        'constraints' => array(
            'postId' => '\d{4}'
        )
    ],
    'may_terminate' => true,
    'child_routes' => [
        // You can place additional routes that match under the
        // route defined above here.
    ],
]

现在,当我访问 http://localhost:8080/post/create 时,它可以工作,但是当我访问 http://localhost:8080/post/32 时,它不起作用。它说404错误,找不到页面。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

根据@jon Stirling在我的问题上的评论,我更改了发车路线的限制,并且奏效了。

将'postId'=>'\ d {4}'更改为 'postId'=>'\ d {1,4}'

'post' => [
                'type'    => Segment::class,
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/post[/:postId]',
                    'defaults' => [
                        'controller'    => Controller\PostController::class,
                        'action'        => 'show',
                    ],
                    'constraints' => array(
                        'postId' => '\d{1,4}'
                    )
                ],
                'may_terminate' => true,
                'child_routes' => [
                    // You can place additional routes that match under the
                    // route defined above here.
                ],
            ]

答案 1 :(得分:-1)

为什么要在同一路线上执行两个操作?

如果这是REST,则应该在/ post上使用POST方法(用于创建操作),在/ post [/:id]上使用GET方法(用于show操作)。

否则,您可以发布/创建和/ post / show / [/:id]。