在zend框架中做出休息请求3

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

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

我很介意。

/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时,它仍然显示索引方法。有什么问题,我应该如何解决它。

zend-framework restful-url zend-framework3 zend-route zend-rest-route
1个回答
1
投票

你有双重声明的路线。你已经宣布路线/companies[/:id]并给它child_routes:/companies。从本质上讲,你有:/companies/companies/:id/companies/companies。此外,您正在使用segment路线。对于休息路线,您应该使用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,该路由仍将匹配,并将您发送到您在那里配置的indexCompaniesController操作。

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