Php 使用Slim分组路由REST API

Php 使用Slim分组路由REST API,php,rest,api,slim,Php,Rest,Api,Slim,我正在用slim 3构建rest api,但在理解如何进行路由时遇到了一些问题。我最初在api.com/v1/companys/get和api.com/v1/companys/get/id使用get方法,在api.com/v1/companys/post使用post方法,但我进行了重构,因此所有方法都将在api.com/v1/companys/id使用,重构后,我在post请求中收到405个错误,表示只有get方法存在 所以我做了更多的研究;我在其他slim 3指南中发现了一些小的、但却令人心烦

我正在用slim 3构建rest api,但在理解如何进行路由时遇到了一些问题。我最初在api.com/v1/companys/get和api.com/v1/companys/get/id使用get方法,在api.com/v1/companys/post使用post方法,但我进行了重构,因此所有方法都将在api.com/v1/companys/id使用,重构后,我在post请求中收到405个错误,表示只有get方法存在

所以我做了更多的研究;我在其他slim 3指南中发现了一些小的、但却令人心烦的不一致之处,但我的解决方案似乎是
map()
函数,只是我不知道如何使用它,甚至官方文档也跳过了我不懂的部分

这就是代码在破坏它的重构之后的样子:

$app->group('/v1', function() use ($app) {
    $app->group('/companies', function() use ($app) {
        $app->get('/{code}', function($request, $response, $args) {...}
        $app->get('', function($request, $response, $args) {...}
        $app->post('', function($request, $response, $args) {...}
    });
});
我第一次尝试使用map():


此代码适用于我:

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

$app->group('/v1', function() {
    $this->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) {

        if($request->isGet()) {
            $response->getBody()->write("it's GET");
        }

        if($request->isPost()) {
            $response->getBody()->write("it's POST");
        }

        if($request->isPut()) {
            $response->getBody()->write("it's PUT");
        }

        if($request->isDelete()) {
            $response->getBody()->write("it's DELETE");
        }

        return $response;
    });
});

$app->run();

关于$this和$app,我认为最初是$this,但我一直在其他地方看到$app。谢谢
<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

$app->group('/v1', function() {
    $this->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) {

        if($request->isGet()) {
            $response->getBody()->write("it's GET");
        }

        if($request->isPost()) {
            $response->getBody()->write("it's POST");
        }

        if($request->isPut()) {
            $response->getBody()->write("it's PUT");
        }

        if($request->isDelete()) {
            $response->getBody()->write("it's DELETE");
        }

        return $response;
    });
});

$app->run();