Skip to content

HTTP Response

Testing Is Documentation

tests/Http/ResponseTest.php

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

注意

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

Uses

php
<?php

use Leevel\Http\Response;
use Leevel\Kernel\Utils\Api;

setHeader 设置响应头

php
public function testSetHeader(): void
{
    $response = new Response();
    $response->setHeader('foo', 'bar');
    self::assertSame('bar', $response->headers->get('foo'));
}

withHeaders 批量设置响应头

php
public function testWithHeaders(): void
{
    $response = new Response();
    $response->withHeaders(['foo' => 'bar']);
    self::assertSame('bar', $response->headers->get('foo'));
}
php
public function testSetCookie(): void
{
    $response = new Response();
    $response->setCookie('foo', 'bar');
    self::assertCount(1, $response->headers->getCookies());
}
php
public function testWithCookies(): void
{
    $response = new Response();
    $response->withCookies(['hello' => 'world']);
    self::assertCount(1, $response->headers->getCookies());
}