代码之家  ›  专栏  ›  技术社区  ›  Aamir

Zend Framework3类似路由冲突

  •  0
  • Aamir  · 技术社区  · 6 年前

    我在用ZF3 文件在Post模块中,我有这两条路径中的一条,

    '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

    任何帮助都非常感谢。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Aamir    6 年前

    根据@jon Stirling对我的问题的评论,我改变了post-route的限制条件,它成功了。

    已将'postd'=>'\d{4}'更改为 'postd'=>'\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.
                    ],
                ]
    
        2
  •  -1
  •   koddlo    6 年前

    如果这是REST,那么应该将POST方法设置为on/POST(用于创建操作)和GET method设置为on/POST[/:id](用于显示操作)。

    否则,可以使用post/create和/post/show/[/:id]。