HTTP Request

Testing Is Documentation

tests/Http/RequestTest.phpHTTP Request - 图1

QueryPHP 请求对象构建在 Symfony HttpFoundation 之上,增加了少量的功能。

使用方式

使用容器 request 服务

  1. \App::make('request')->get($key, $default = null);
  2. \App::make('request')->all(): array;

依赖注入

  1. class Demo
  2. {
  3. private \Leevel\Http\Request $request;
  4. public function __construct(\Leevel\Http\Request $request)
  5. {
  6. $this->request = $request;
  7. }
  8. }

使用静态代理

  1. \Leevel\Router\Proxy\Request::get(string $key, $default = null);
  2. \Leevel\Router\Proxy\Request::all(): array;

注意

为了一致性或者更好与 Swoole 对接,请统一使用请求对象处理输入,避免直接使用 $_GET$_POST,$_COOKIE,$_FILES,$_SERVER 等全局变量。

Uses

  1. <?php
  2. use Leevel\Http\Request;
  3. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

createFromSymfonyRequest 从 Symfony 请求创建 Leevel 请求

  1. public function testCreateFromSymfonyRequest(): void
  2. {
  3. $symfonyRequest = new SymfonyRequest(['foo' => 'bar', 'hello' => 'world'], [], [], [], [], [], 'content');
  4. $request = Request::createFromSymfonyRequest($symfonyRequest);
  5. $this->assertInstanceof(Request::class, $request);
  6. $this->assertSame(['foo' => 'bar', 'hello' => 'world'], $request->query->all());
  7. $this->assertSame('content', $request->getContent());
  8. }

all 获取所有请求参数

  • 包含 request、query 和 attributes
  • 优先级从高到底依次为 attributes、query 和 request,优先级高的会覆盖优先级低的参数
  1. public function testAll(): void
  2. {
  3. $request = new Request(['query' => '1'], ['request' => '2'], ['attributes' => '3']);
  4. $this->assertSame(['request' => '2', 'query' => '1', 'attributes' => '3'], $request->all());
  5. $request = new Request(['foo' => '1'], ['foo' => '2'], ['foo' => '3']);
  6. $this->assertSame(['foo' => '2'], $request->all());
  7. }

exists 请求是否包含非空

  1. public function testExists(): void
  2. {
  3. $request = new Request(['foo' => 'bar', 'hello' => 'world']);
  4. $this->assertTrue($request->exists(['foo']));
  5. $this->assertTrue($request->exists(['foo', 'hello']));
  6. $this->assertFalse($request->exists(['notFound']));
  7. }

only 取得给定的 keys 数据

  1. public function testOnly(): void
  2. {
  3. $request = new Request(['foo' => 'bar', 'hello' => 'world']);
  4. $this->assertSame(['foo' => 'bar'], $request->only(['foo']));
  5. $this->assertSame(['foo' => 'bar', 'hello' => 'world'], $request->only(['foo', 'hello']));
  6. $this->assertSame(['foo' => 'bar', 'not' => null], $request->only(['foo', 'not']));
  7. }

except 取得排除给定的 keys 数据

  1. public function testExcept(): void
  2. {
  3. $request = new Request(['foo' => 'bar', 'hello' => 'world']);
  4. $this->assertSame(['hello' => 'world'], $request->except(['foo']));
  5. $this->assertSame([], $request->except(['foo', 'hello']));
  6. $this->assertSame(['hello' => 'world'], $request->except(['foo', 'not']));
  7. }

isConsole 是否为 PHP 运行模式命令行, 兼容 Swoole HTTP Service

Swoole HTTP 服务器也以命令行运行,也就是 Swoole 情况下会返回 false。

  1. public function testIsConsole(): void
  2. {
  3. $request = new Request();
  4. $this->assertTrue($request->isConsole());
  5. }

isConsole 是否为 PHP 运行模式命令行,Swoole 场景测试

  1. public function testIsConsoleForSwoole(): void
  2. {
  3. $request = new Request([], [], [], [], [], ['SERVER_SOFTWARE' => 'swoole-http-server']);
  4. $this->assertFalse($request->isConsole());
  5. }

isCgi 是否为 PHP 运行模式 cgi

  1. public function testIsCgi(): void
  2. {
  3. $request = new Request();
  4. $this->assertFalse($request->isCgi());
  5. }

isPjax 是否为 Pjax 请求行为

  1. public function testIsPjax(): void
  2. {
  3. $request = new Request();
  4. $this->assertFalse($request->isPjax());
  5. $request->request->set(Request::VAR_PJAX, true);
  6. $this->assertTrue($request->isPjax());
  7. }

isAcceptAny 是否为接受任何请求,支持伪装

  1. public function testIsAcceptJson(): void
  2. {
  3. $request = new Request();
  4. $this->assertFalse($request->isRealAcceptJson());
  5. $this->assertFalse($request->isAcceptJson());
  6. $request->headers->set('accept', 'application/json, text/plain, */*');
  7. $this->assertTrue($request->isRealAcceptJson());
  8. $this->assertTrue($request->isAcceptJson());
  9. $request->headers->remove('accept');
  10. $this->assertFalse($request->isRealAcceptJson());
  11. $this->assertFalse($request->isAcceptJson());
  12. // (isAjax && !isPjax) && isAcceptAny
  13. $request->request->set(Request::VAR_AJAX, 1);
  14. $this->assertFalse($request->isRealAcceptJson());
  15. $this->assertTrue($request->isAcceptJson());
  16. $request->request->remove(Request::VAR_AJAX);
  17. // 伪装
  18. $request->query->set(Request::VAR_ACCEPT_JSON, '1');
  19. $this->assertTrue($request->isAcceptJson());
  20. $this->assertFalse($request->isRealAcceptJson());
  21. }

isAcceptAny 是否为接受任何请求

  1. public function testIsAcceptAny(): void
  2. {
  3. $request = new Request();
  4. $this->assertTrue($request->isAcceptAny());
  5. $request->headers->set('accept', 'application/json');
  6. $this->assertFalse($request->isAcceptAny());
  7. $request->headers->set('accept', '*/*');
  8. $this->assertTrue($request->isAcceptAny());
  9. }

getEnter 获取入口文件

  1. public function testGetEnter(): void
  2. {
  3. $request = new Request();
  4. $this->assertSame('', $request->getEnter());
  5. }

setPathInfo 设置 pathInfo

  1. public function testSetPathInfo(): void
  2. {
  3. $request = new Request();
  4. $this->assertSame('/', $request->getPathInfo());
  5. $request->setPathInfo('/foo/bar');
  6. $this->assertSame('/foo/bar', $request->getPathInfo());
  7. }

toArray 对象转数组

Request 请求对象实现了 \Leevel\Support\IArray 接口。

  1. public function testToArray(): void
  2. {
  3. $request = new Request(['foo' => 'bar', 'hello' => 'world']);
  4. $this->assertSame(['foo' => 'bar', 'hello' => 'world'], $request->toArray());
  5. }