HTTP Response

Testing Is Documentation

tests/Http/ResponseTest.phpHTTP Response - 图1

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

注意

为了一致性或者更好与 Swoole 对接,请统一使用响应对象返回,框架会自动处理返回结果,请避免直接使用 echodie 等中断后续处理。

Uses

  1. <?php
  2. use Leevel\Http\Response;

setHeader 设置响应头

  1. public function testSetHeader(): void
  2. {
  3. $response = new Response();
  4. $response->setHeader('foo', 'bar');
  5. $this->assertSame('bar', $response->headers->get('foo'));
  6. }

withHeaders 批量设置响应头

  1. public function testWithHeaders(): void
  2. {
  3. $response = new Response();
  4. $response->withHeaders(['foo' => 'bar']);
  5. $this->assertSame('bar', $response->headers->get('foo'));
  6. }

setCookie 设置 COOKIE

  1. public function testSetCookie(): void
  2. {
  3. $response = new Response();
  4. $response->setCookie('foo', 'bar');
  5. $this->assertCount(1, $response->headers->getCookies());
  6. }

withCookies 批量设置 COOKIE

  1. public function testWithCookies(): void
  2. {
  3. $response = new Response();
  4. $response->withCookies(['hello' => 'world']);
  5. $this->assertCount(1, $response->headers->getCookies());
  6. }