Skip to content

HTTP Request

Testing Is Documentation

tests/Http/RequestTest.php

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

使用方式

使用容器 request 服务

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

依赖注入

php
class Demo
{
    private \Leevel\Http\Request $request;

    public function __construct(\Leevel\Http\Request $request)
    {
        $this->request = $request;
    }
}

使用静态代理

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

注意

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

Uses

php
<?php

use Leevel\Http\Request;
use Leevel\Kernel\Utils\Api;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

createFromSymfonyRequest 从 Symfony 请求创建 Leevel 请求

php
public function testCreateFromSymfonyRequest(): void
{
    $symfonyRequest = new SymfonyRequest(['foo' => 'bar', 'hello' => 'world'], [], [], [], [], [], 'content');
    $request = Request::createFromSymfonyRequest($symfonyRequest);

    $this->assertInstanceof(Request::class, $request);
    static::assertSame(['foo' => 'bar', 'hello' => 'world'], $request->query->all());
    static::assertSame('content', $request->getContent());
}

all 获取所有请求参数

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

    $request = new Request(['foo' => '1'], ['foo' => '2'], ['foo' => '3']);
    static::assertSame(['foo' => '2'], $request->all());
}

exists 请求是否包含非空

php
public function testExists(): void
{
    $request = new Request(['foo' => 'bar', 'hello' => 'world']);
    static::assertTrue($request->exists(['foo']));
    static::assertTrue($request->exists(['foo', 'hello']));
    static::assertFalse($request->exists(['notFound']));
}

only 取得给定的 keys 数据

php
public function testOnly(): void
{
    $request = new Request(['foo' => 'bar', 'hello' => 'world']);
    static::assertSame(['foo' => 'bar'], $request->only(['foo']));
    static::assertSame(['foo' => 'bar', 'hello' => 'world'], $request->only(['foo', 'hello']));
    static::assertSame(['foo' => 'bar', 'not' => null], $request->only(['foo', 'not']));
}

except 取得排除给定的 keys 数据

php
public function testExcept(): void
{
    $request = new Request(['foo' => 'bar', 'hello' => 'world']);
    static::assertSame(['hello' => 'world'], $request->except(['foo']));
    static::assertSame([], $request->except(['foo', 'hello']));
    static::assertSame(['hello' => 'world'], $request->except(['foo', 'not']));
}

isConsole 是否为 PHP 运行模式命令行

php
public function testIsConsole(): void
{
    $request = new Request();
    static::assertTrue($request->isConsole());
}

isCgi 是否为 PHP 运行模式 cgi

php
public function testIsCgi(): void
{
    $request = new Request();
    static::assertFalse($request->isCgi());
}

isPjax 是否为 Pjax 请求行为

php
public function testIsPjax(): void
{
    $request = new Request();

    static::assertFalse($request->isPjax());
    $request->request->set(Request::VAR_PJAX, true);
    static::assertTrue($request->isPjax());
}

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

php
public function testIsAcceptJson(): void
{
    $request = new Request();

    static::assertFalse($request->isRealAcceptJson());
    static::assertFalse($request->isAcceptJson());

    $request->headers->set('accept', 'application/json, text/plain, */*');
    static::assertTrue($request->isRealAcceptJson());
    static::assertTrue($request->isAcceptJson());
    $request->headers->remove('accept');

    static::assertFalse($request->isRealAcceptJson());
    static::assertFalse($request->isAcceptJson());

    // (isAjax && !isPjax) && isAcceptAny
    $request->request->set(Request::VAR_AJAX, 1);
    static::assertFalse($request->isRealAcceptJson());
    static::assertTrue($request->isAcceptJson());
    $request->request->remove(Request::VAR_AJAX);

    // 伪装
    $request->query->set(Request::VAR_ACCEPT_JSON, '1');
    static::assertTrue($request->isAcceptJson());
    static::assertFalse($request->isRealAcceptJson());
}

isAcceptAny 是否为接受任何请求

php
public function testIsAcceptAny(): void
{
    $request = new Request();

    static::assertTrue($request->isAcceptAny());

    $request->headers->set('accept', 'application/json');
    static::assertFalse($request->isAcceptAny());

    $request->headers->set('accept', '*/*');
    static::assertTrue($request->isAcceptAny());
}

getEnter 获取入口文件

php
public function testGetEnter(): void
{
    $request = new Request();
    static::assertSame('', $request->getEnter());
}

setPathInfo 设置 pathInfo

php
public function testSetPathInfo(): void
{
    $request = new Request();
    static::assertSame('/', $request->getPathInfo());
    $request->setPathInfo('/foo/bar');
    static::assertSame('/foo/bar', $request->getPathInfo());
}

toArray 对象转数组

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

php
public function testToArray(): void
{
    $request = new Request(['foo' => 'bar', 'hello' => 'world']);
    static::assertSame(['foo' => 'bar', 'hello' => 'world'], $request->toArray());
}