【问题标题】:Making restful requests in zend framework 3在 zend 框架 3 中发出 restful 请求
【发布时间】:2019-02-22 14:04:55
【问题描述】:

所以我正在尝试发出宁静的网络请求,但是我可能配置错误,似乎无法修复。

我有这个想法。

/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】:

    你有双重声明的路线。您已声明路由 /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,该路由仍将匹配并将您发送到您在那里配置的 CompaniesController 的 index 操作。

    【讨论】:

    • 看起来很有希望。我会在星期一试一试,然后告诉你。
    • 它不起作用。我复制了路线,/companies 抛出 404。介意你试试吗?
    • 嗨,阿里,我没有遵循您的控制器和操作名称。复制此配置时是否更新了这些?基于配置的 404 通常表示找到了 route(例如 /companies),但没有找到对应的 Controller::nameAction。可能只是视图与操作不匹配,但这通常会解析为 view not found 错误类型。
    • 我确实做到了。我还省略了子路线并尝试使用 index 操作的单路线,仍然是 404
    • 您的控制器的名称是什么?使用语句是否与文件夹结构匹配(根据 PSR-4)?动作名称是什么?之前的所有配置都匹配这个配置吗? (如果没有,你能提供差异吗?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多