0 follower

Final Class Yiisoft\Yii\Middleware\Redirect

InheritanceYiisoft\Yii\Middleware\Redirect
ImplementsPsr\Http\Server\MiddlewareInterface

This middleware generates and adds a Location header to the response.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Yii\Middleware\Redirect
permanent() Returns a new instance with the response status code of permanent redirection. Yiisoft\Yii\Middleware\Redirect
process() Yiisoft\Yii\Middleware\Redirect
temporary() Returns a new instance with the response status code of temporary redirection. Yiisoft\Yii\Middleware\Redirect
toRoute() Returns a new instance with the specified route data for redirection. Yiisoft\Yii\Middleware\Redirect
toUrl() Returns a new instance with the specified URL for redirection. Yiisoft\Yii\Middleware\Redirect
withStatus() Returns a new instance with the specified status code of the response for redirection. Yiisoft\Yii\Middleware\Redirect

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Yiisoft\Router\UrlGeneratorInterface $urlGenerator )
$responseFactory \Psr\Http\Message\ResponseFactoryInterface
$urlGenerator \Yiisoft\Router\UrlGeneratorInterface

                public function __construct(
    private ResponseFactoryInterface $responseFactory,
    private UrlGeneratorInterface $urlGenerator,
) {
}

            
permanent() public method

Returns a new instance with the response status code of permanent redirection.

See also \Yiisoft\Http\Status::MOVED_PERMANENTLY.

public self permanent ( )

                public function permanent(): self
{
    $new = clone $this;
    $new->statusCode = Status::MOVED_PERMANENTLY;
    return $new;
}

            
process() public method

public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler )
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface
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);
}

            
temporary() public method

Returns a new instance with the response status code of temporary redirection.

See also \Yiisoft\Http\Status::FOUND.

public self temporary ( )

                public function temporary(): self
{
    $new = clone $this;
    $new->statusCode = Status::FOUND;
    return $new;
}

            
toRoute() public method

Returns a new instance with the specified route data for redirection.

If you've set a redirect URL with {@see \Yiisoft\Yii\Middleware\toUrl()}, the middleware ignores the route data, since the URL is a priority.

public self toRoute ( string $name, array<string, scalar|\Stringable|null> $parameters = [] )
$name string

The route name for redirection.

$parameters array<string, scalar|\Stringable|null>

$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;
}

            
toUrl() public method

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;
}

            
withStatus() public method

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;
}