企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 请求概述 ***** Your Slim app’s routes and middleware are given a PSR-7 request object that represents the current HTTP request received by your web server. The request object implements the[PSR-7 ServerRequestInterface](http://www.php-fig.org/psr/psr-7/#3-2-1-psr-http-message-serverrequestinterface)with which you can inspect and manipulate the HTTP request method, headers, and body. > Slim应用程序的路由和中间件都有一个PSR-7请求对象,该对象表示web服务器接收到的当前HTTP请求。请求对象实现了[PSR-7 ServerRequestInterface](http://www.php-fig.org/psr/psr-7/#3-2-1-psr-http-message-serverrequestinterface),您可以使用它检查和操作HTTP请求方法、头和正文。 ## 如何获取请求对象 The PSR-7 request object is injected into your Slim application routes as the first argument to the route callback like this: > 将PSR-7请求对象作为路由回调的第一个参数注入到您的slim程序路由中,如下所示: ~~~php <?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/hello', function (Request $request, Response $response) { $response->getBody()->write('Hello World'); return $response; }); $app->run(); ~~~ Figure 1: Inject PSR-7 request into application route callback. The PSR-7 request object is injected into your Slim application*middleware*as the first argument of the middleware callable like this: 将PSR-7请求对象注入到您的slim应用程序*中间件*中,作为可调用的中间件的第一个参数,如下所示: ~~~php <?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->add(function (ServerRequestInterface $request, RequestHandler $handler) { return $handler->handle($request); }); // ...define app routes... $app->run(); ~~~ Figure 2: Inject PSR-7 request into application middleware.