Final Class Yiisoft\HttpMiddleware\RedirectMiddleware
| Inheritance | Yiisoft\HttpMiddleware\RedirectMiddleware |
|---|---|
| Implements | Psr\Http\Server\MiddlewareInterface |
Middleware that responds with a redirect to the specified URL.
Psalm Types
| Name | Value |
|---|---|
| ConditionCallable | callable |
Public 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
| 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
|
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;
}
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);
}
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);
}
| 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);
}
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);
}
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);
}
Signup or Login in order to comment.