Final Class Yiisoft\HttpMiddleware\RedirectMiddleware
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
Middleware that responds with a redirect to the specified URL.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| found() | Creates a middleware with "302 Found" status. | Yiisoft\ |
| permanent() | Creates a middleware with "301 Moved Permanently" status. | Yiisoft\ |
| process() | Yiisoft\ |
|
| seeOther() | Creates a middleware with "303 See Other" status. | Yiisoft\ |
| temporary() | Creates a middleware with "307 Temporary Redirect" status. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $responseFactory | \ |
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 self found ( \ | ||
| $responseFactory | \ |
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 self permanent ( \ | ||
| $responseFactory | \ |
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 \ | ||
| $request | \ |
|
| $handler | \ |
|
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 self seeOther ( \ | ||
| $responseFactory | \ |
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 self temporary ( \ | ||
| $responseFactory | \ |
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);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.