Final Class Yiisoft\Yii\Middleware\Redirect
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
This middleware generates and adds a Location header to the response.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| permanent() | Returns a new instance with the response status code of permanent redirection. | Yiisoft\ |
| process() | Yiisoft\ |
|
| temporary() | Returns a new instance with the response status code of temporary redirection. | Yiisoft\ |
| toRoute() | Returns a new instance with the specified route data for redirection. | Yiisoft\ |
| toUrl() | Returns a new instance with the specified URL for redirection. | Yiisoft\ |
| withStatus() | Returns a new instance with the specified status code of the response for redirection. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $responseFactory | \ |
|
| $urlGenerator | \ |
|
public function __construct(
private ResponseFactoryInterface $responseFactory,
private UrlGeneratorInterface $urlGenerator,
) {}
Returns a new instance with the response status code of permanent redirection.
See also \
| public self permanent ( ) |
public function permanent(): self
{
$new = clone $this;
$new->statusCode = Status::MOVED_PERMANENTLY;
return $new;
}
| public \ | ||
| $request | \ |
|
| $handler | \ |
|
| throws | RuntimeException |
If the data for redirection wasn't set earlier. |
|---|---|---|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->route === null && $this->uri === null) {
throw new RuntimeException('Either `toUrl()` or `toRoute()` method should be used.');
}
/** @psalm-suppress PossiblyNullArgument */
$uri = $this->uri ?? $this->urlGenerator->generate($this->route, $this->parameters);
return $this->responseFactory
->createResponse($this->statusCode)
->withAddedHeader('Location', $uri);
}
Returns a new instance with the response status code of temporary redirection.
See also \
| public self temporary ( ) |
public function temporary(): self
{
$new = clone $this;
$new->statusCode = Status::FOUND;
return $new;
}
Returns a new instance with the specified route data for redirection.
If you've set a redirect URL with {@see \
| public self toRoute ( string $name, array | ||
| $name | string |
The route name for redirection. |
| $parameters | array |
$parameters The route parameters for redirection. |
public function toRoute(string $name, array $parameters = []): self
{
$new = clone $this;
$new->route = $name;
$new->parameters = $parameters;
return $new;
}
Returns a new instance with the specified URL for redirection.
| public self toUrl ( string $url ) | ||
| $url | string |
URL for redirection. |
public function toUrl(string $url): self
{
$new = clone $this;
$new->uri = $url;
return $new;
}
Returns a new instance with the specified status code of the response for redirection.
| public self withStatus ( integer $statusCode ) | ||
| $statusCode | integer |
The status code of the response for redirection. |
public function withStatus(int $statusCode): self
{
$new = clone $this;
$new->statusCode = $statusCode;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.