0 follower

Final Class Yiisoft\HttpMiddleware\RedirectMiddleware

InheritanceYiisoft\HttpMiddleware\RedirectMiddleware
ImplementsPsr\Http\Server\MiddlewareInterface

Middleware that responds with a redirect to the specified URL.

Psalm Types

Name Value
ConditionCallable callable

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\HttpMiddleware\RedirectMiddleware
found() Creates a middleware with "302 Found" status. Yiisoft\HttpMiddleware\RedirectMiddleware
permanent() Creates a middleware with "301 Moved Permanently" status. Yiisoft\HttpMiddleware\RedirectMiddleware
process() Yiisoft\HttpMiddleware\RedirectMiddleware
seeOther() Creates a middleware with "303 See Other" status. Yiisoft\HttpMiddleware\RedirectMiddleware
temporary() Creates a middleware with "307 Temporary Redirect" status. Yiisoft\HttpMiddleware\RedirectMiddleware

Method Details

Hide inherited methods

__construct() public method

public __construct( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, string $url, integer $statusCode 301, callable|null $condition null ): mixed
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

Factory to create a response.

$url string

The URL to redirect to.

$statusCode integer

The HTTP status code for the redirect response.

$condition callable|null

Optional condition callable with signature callable(ServerRequestInterface): bool. If provided, the redirect is performed only when the callable returns true. For example: `php static fn(ServerRequestInterface $request): bool => $request->getMethod() === 'POST' `

                public function __construct(
    private readonly ResponseFactoryInterface $responseFactory,
    private readonly string $url,
    private readonly int $statusCode = 301,
    ?callable $condition = null,
) {
    $this->condition = $condition ?? static fn(): bool => true;
}

            
found() public static method

Creates a middleware with "302 Found" status.

See also __construct().

public static found( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, string $url, callable|null $condition null ): self
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

Factory to create a response.

$url string

The URL to redirect to.

$condition callable|null

Optional condition callable.

                public static function found(
    ResponseFactoryInterface $responseFactory,
    string $url,
    ?callable $condition = null,
): self {
    return new self($responseFactory, $url, 302, $condition);
}

            
permanent() public static method

Creates a middleware with "301 Moved Permanently" status.

See also __construct().

public static permanent( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, string $url, callable|null $condition null ): self
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

Factory to create a response.

$url string

The URL to redirect to.

$condition callable|null

Optional condition callable.

                public static function permanent(
    ResponseFactoryInterface $responseFactory,
    string $url,
    ?callable $condition = null,
): self {
    return new self($responseFactory, $url, 301, $condition);
}

            
process() public method

public process( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ): \Psr\Http\Message\ResponseInterface
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface

                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    if (!($this->condition)($request)) {
        return $handler->handle($request);
    }
    return $this->responseFactory
        ->createResponse($this->statusCode)
        ->withHeader('Location', $this->url);
}

            
seeOther() public static method

Creates a middleware with "303 See Other" status.

See also __construct().

public static seeOther( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, string $url, callable|null $condition null ): self
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

Factory to create a response.

$url string

The URL to redirect to.

$condition callable|null

Optional condition callable.

                public static function seeOther(
    ResponseFactoryInterface $responseFactory,
    string $url,
    ?callable $condition = null,
): self {
    return new self($responseFactory, $url, 303, $condition);
}

            
temporary() public static method

Creates a middleware with "307 Temporary Redirect" status.

See also __construct().

public static temporary( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, string $url, callable|null $condition null ): self
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

Factory to create a response.

$url string

The URL to redirect to.

$condition callable|null

Optional condition callable.

                public static function temporary(
    ResponseFactoryInterface $responseFactory,
    string $url,
    ?callable $condition = null,
): self {
    return new self($responseFactory, $url, 307, $condition);
}