0

所以我试图发出宁静的网络请求,但是,我可能把它配置错了,我似乎无法修复。

我有这个想法。

/companies      GET      -> index method
/companies      POST     -> add post method
/companies/1    GET      -> show method
/companies/1    POST     -> edit post method

这是我尝试过的

"router"                             => [
    "routes"                         => [
        "companies"                  => [
            "type"                   => "segment",
            "options"                => [
                "route"              => "/companies[/:id]",
                "constraints"        => [
                    "id"     => "[0-9]*",
                ],
                "defaults"           => [
                    "controller"     => Controller\CompaniesController::class,
                    "action"         => "index",
                ],
            ],
            "may_terminate"          => true,
            "child_routes"           => [
                "companiesIndex"     => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "GET",
                        "route"      => "/companies",
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "index"
                        ],
                    ],
                ],
                "companiesAddPost"   => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "POST",
                        "route"      => "/companies",
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "add"
                        ],
                    ],
                ],
                "companiesShow"  => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "GET",
                        "route"      => "/companies/:id",
                        "constraints"        => [
                            "id"     => "[0-9]*",
                        ],
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "show"
                        ],
                    ],
                ],
                "companiesEditPost"  => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "PATCH",
                        "route"      => "/companies/:id",
                        "constraints"        => [
                            "id"     => "[0-9]*",
                        ],
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "edit"
                        ],
                    ],
                ],
            ],
        ],

/companies索引方法有效。不确定邮政。但是每当我尝试请求它时,/companies/1它仍然显示索引方法。出了什么问题,我应该如何解决。

4

1 回答 1

1

您有双重声明的路线。您已经声明了路线/companies[/:id]并给了它 child_routes: /companies。本质上,你有/companies/companies/:id/companies/companies。此外,您正在使用segment路线。对于 Rest 路线,您应该使用Method routes

例如:

<?php

namespace Company;

use Company\Controller\Company\AddController;
use Company\Controller\Company\DeleteController;
use Company\Controller\Company\EditController;
use Company\Controller\Company\IndexController;
use Company\Controller\Company\ViewController;
use Zend\Router\Http\Method;

return [
    'router' => [
        'routes' => [
            'companies_index' => [
                'type'          => Method::class,
                'may_terminate' => true,
                'options'       => [
                    'verb'     => 'GET',
                    'route'    => '/companies',
                    'defaults' => [
                        'controller' => IndexController::class,
                        'action'     => 'index',
                    ],
                ],
                'child_routes'  => [
                    'companies_view'   => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'GET',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => ViewController::class,
                                'action'     => 'view',
                            ],
                        ],
                    ],
                    'companies_edit'   => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'PATCH',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => EditController::class,
                                'action'     => 'edit',
                            ],
                        ],
                    ],
                    'companies_delete' => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'DELETE',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => DeleteController::class,
                                'action'     => 'delete',
                            ],
                        ],
                    ],
                ],
            ],
            'companies_add'   => [
                'type'          => Method::class,
                'may_terminate' => true,
                'options'       => [
                    'verb'     => 'POST',
                    'route'    => '/companies',
                    'defaults' => [
                        'controller' => AddController::class,
                        'action'     => 'add',
                    ],
                ],
            ],
        ],
    ],
];

还有这个子问题:

但是每当我尝试请求 /companies/1 时,它仍然显示索引方法。

那是因为您的初始索引路线是/companies[/:id]. 因此,如果您将/1请求添加到 URL,该路由仍将匹配并将您发送到您在那里配置的index操作。CompaniesController

于 2019-02-23T08:35:19.390 回答