# 请求概述
*****
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.
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir