ZF3高级路由-使用HTTP方法

问题描述 投票:0回答:1

我正在使用ZF3创建一个站点。我在某些路线上遇到了麻烦。

对于样品,

我希望在访问此URL时:http://localhost/customer-import/,如果POST方法:CustomerImportController :: Process将被执行,如果GET方法:CustomerImportController :: Index将被执行

实际上:始终执行CustomerImportController :: Index

配置文件:

'router' => [
        'routes' => [
            'customers' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/customers',
                    'defaults' => [
                        'controller' => Controller\CustomerController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'customers-import' => [
                'type'    => Literal::class,
                'options' => [
                    'route'    => '/customer-import-tool',
                    'defaults' => [
                        'controller' => Controller\CustomerImportController::class,
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'import_customer' => [
                        'type' => Method::class,
                        'options' => [
                            'verb' => 'post',
                            'defaults' => [
                                'controller' => Controller\CustomerImportController::class,
                                'action' => 'import',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],

我在做什么错?

zend-framework3
1个回答
1
投票

您总是以CustomerImportController :: Index(-> customers-import路由结尾),因为未指定必须仅对GET请求进行匹配。您正在击中相同的URL(host/customer-import-tool),但只声明了POST子路由。.除了POST和GET之前都已匹配。

这里的解决方案非常简单:-您声明了主要文字路线,但没有调度程序-您声明了两个方法子路由,一个用于GET,另一个用于POST

© www.soinside.com 2019 - 2024. All rights reserved.