Zend framework 在zend framework 3中发出restful请求

Zend framework 在zend framework 3中发出restful请求,zend-framework,restful-url,zend-framework3,zend-route,zend-rest-route,Zend Framework,Restful Url,Zend Framework3,Zend Route,Zend Rest Route,因此,我试图发出restful web请求,但我可能配置错误,似乎无法修复 我不介意 /companies GET -> index method /companies POST -> add post method /companies/1 GET -> show method /companies/1 POST -> edit post method 这是我试过的 "router"

因此,我试图发出restful web请求,但我可能配置错误,似乎无法修复

我不介意

/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"
                        ],
                    ],
                ],
            ],
        ],

/companys
索引方法有效。我不太清楚邮政局的情况。但每当我试图请求
/companys/1
时,它仍然显示index method。有什么问题,我应该如何解决。

您有双重声明的路由。您已经声明了路由
/companys[/:id]
,并为其指定了子路由:
/companys
。本质上,您有:
/companys
/companys/:id
/companys/companys
。此外,您正在使用
路由。对于休息路线,您应该使用

例如:

<?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',
                    ],
                ],
            ],
        ],
    ],
];

看起来很有希望。我会在星期一试试,然后告诉你。它不起作用。我复制了路线和
/companys
抛出404。介意你试试吗?嗨,阿里,我没听你的控制器和动作名称。复制此配置时是否更新了这些配置?基于配置的404通常表示找到了
路由(例如
/companys
),但没有找到相应的
控制器::nameAction
。可能只是视图与操作不匹配,但通常会解析为
未找到视图
错误类型。我还忽略了子路由,尝试了使用
索引
操作的单路由,仍然是404控制器的名称?use语句是否与文件夹结构匹配(根据PSR-4)?动作名称是什么?所有以前的配置是否都与此配置匹配?(如果没有,您能否提供差异?)