使用 Slim 在组中路由 REST API
Routing a REST API in groups with Slim
我正在使用 slim 3 构建休息 api,但我在理解如何进行路由时遇到了一些问题。我最初让 get 方法在 api.com/v1/companies/get 和 api.com/v1/companies/get/id 和 post 方法在 api.com/v1/companies/post,但我重构了所有方法,因此所有方法都位于 api.com/v1/companies/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():
$app->group('/v1', function() use ($app) {
$app->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) use ($app) {
//here's where I don't know what to do
if($request->isGet()) {
//What goes here? And I seem to be having problems accessing my parameters?
}
}
}
这段代码对我有用:
<?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();
请不要在组内使用$app。在 docs 中,您可以看到 $this inside group 已经指向 'Slim\App' 的实例。还要检查您的 .htaccess 文件是否按照 Slim3 文档中的描述进行了配置。
我正在使用 slim 3 构建休息 api,但我在理解如何进行路由时遇到了一些问题。我最初让 get 方法在 api.com/v1/companies/get 和 api.com/v1/companies/get/id 和 post 方法在 api.com/v1/companies/post,但我重构了所有方法,因此所有方法都位于 api.com/v1/companies/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():
$app->group('/v1', function() use ($app) {
$app->map(['GET', 'POST', 'PUT', 'DELETE'], '/companies/{code}', function($request, $response, $args) use ($app) {
//here's where I don't know what to do
if($request->isGet()) {
//What goes here? And I seem to be having problems accessing my parameters?
}
}
}
这段代码对我有用:
<?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();
请不要在组内使用$app。在 docs 中,您可以看到 $this inside group 已经指向 'Slim\App' 的实例。还要检查您的 .htaccess 文件是否按照 Slim3 文档中的描述进行了配置。