0 follower

Final Class Yiisoft\Yii\View\Renderer\WebViewRenderer

InheritanceYiisoft\Yii\View\Renderer\WebViewRenderer
ImplementsYiisoft\View\ViewContextInterface

Factory that creates PSR-7 response instances with a content rendered by view.

Both {@see \Yiisoft\Yii\View\Renderer\WebViewRenderer::render()} and {@see \Yiisoft\Yii\View\Renderer\WebViewRenderer::renderPartial()} return an instance of {@see \Psr\Http\Message\ResponseInterface} with deferred rendering support.

{@see \Yiisoft\Yii\View\Renderer\WebViewRenderer::renderAsString()} and {@see \Yiisoft\Yii\View\Renderer\WebViewRenderer::renderPartialAsString()} are rendering immediately returning the result of the rendering as a string.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Yii\View\Renderer\WebViewRenderer
getViewPath() Returns a path to a base directory of view templates that is prefixed to the relative view name. Yiisoft\Yii\View\Renderer\WebViewRenderer
render() Returns a response instance that supports deferred rendering. Yiisoft\Yii\View\Renderer\WebViewRenderer
renderAsString() Renders a view as a string. Yiisoft\Yii\View\Renderer\WebViewRenderer
renderPartial() Returns a response instance that supports deferred rendering {@see render()} without applying a layout. Yiisoft\Yii\View\Renderer\WebViewRenderer
renderPartialAsString() Renders a view as string {@see renderAsString()} without applying a layout. Yiisoft\Yii\View\Renderer\WebViewRenderer
withAddedInjections() Return a new instance with the appended specified injections. Yiisoft\Yii\View\Renderer\WebViewRenderer
withContentType() Returns a new instance with the specified content type. Yiisoft\Yii\View\Renderer\WebViewRenderer
withController() Extracts the controller name and returns a new instance with the controller name. Yiisoft\Yii\View\Renderer\WebViewRenderer
withControllerName() Returns a new instance with the specified controller name. Yiisoft\Yii\View\Renderer\WebViewRenderer
withEncoding() Returns a new instance with the specified encoding. Yiisoft\Yii\View\Renderer\WebViewRenderer
withInjections() Returns a new instance with the specified injections. Yiisoft\Yii\View\Renderer\WebViewRenderer
withLayout() Returns a new instance with the specified layout. Yiisoft\Yii\View\Renderer\WebViewRenderer
withLocale() Returns a new instance with specified locale code. Yiisoft\Yii\View\Renderer\WebViewRenderer
withViewPath() Returns a new instance with the specified view path. Yiisoft\Yii\View\Renderer\WebViewRenderer

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Psr\Http\Message\StreamFactoryInterface $streamFactory, \Yiisoft\Aliases\Aliases $aliases, \Yiisoft\View\WebView $view, string|null $viewPath null, string|null $layout null, array $injections = [], ?\Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainerInterface $injectionContainer null, string $contentType 'text/html', string $encoding 'UTF-8' )
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

The PSR-17 response factory.

$streamFactory \Psr\Http\Message\StreamFactoryInterface

The PSR-17 stream factory.

$aliases \Yiisoft\Aliases\Aliases

The aliases instance.

$view \Yiisoft\View\WebView

The web view instance.

$viewPath string|null

The full path to the directory of views or its alias. If null, relative view paths in {@see \Yiisoft\Yii\View\Renderer\WebViewRenderer::render()} are not available.

$layout string|null

The full path to the layout file to be applied to views. If null, the layout will not be applied.

$injections array

The injection instances or class names.

$injectionContainer ?\Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainerInterface
$contentType string
$encoding string

                public function __construct(
    private readonly ResponseFactoryInterface $responseFactory,
    private readonly StreamFactoryInterface $streamFactory,
    private readonly Aliases $aliases,
    private readonly WebView $view,
    ?string $viewPath = null,
    private ?string $layout = null,
    private array $injections = [],
    ?InjectionContainerInterface $injectionContainer = null,
    private string $contentType = 'text/html',
    private string $encoding = 'UTF-8',
) {
    $this->injectionContainer = $injectionContainer ?? new StubInjectionContainer();
    $this->setViewPath($viewPath);
}

            
getViewPath() public method

Returns a path to a base directory of view templates that is prefixed to the relative view name.

If a controller name has been set {@see withController(), withControllerName()}, it will be appended to the path.

public string getViewPath ( )
return string

View templates base directory.

                public function getViewPath(): string
{
    if ($this->viewPath === null) {
        throw new LogicException('The view path is not set.');
    }
    return $this->aliases->get($this->viewPath) . ($this->name ? '/' . $this->name : '');
}

            
render() public method

Returns a response instance that supports deferred rendering.

public \Psr\Http\Message\ResponseInterface render ( string $view, array $parameters = [] )
$view string

The view name {@see \Yiisoft\View\WebView::render()}.

$parameters array

The parameters (name-value pairs) that will be extracted and made available in the view file.

return \Psr\Http\Message\ResponseInterface

The response instance.

                public function render(string $view, array $parameters = []): ResponseInterface
{
    $commonParameters = $this->getCommonParameters();
    $layoutParameters = $this->getLayoutParameters();
    $metaTags = $this->getMetaTags();
    $linkTags = $this->getLinkTags();
    $response = $this->responseFactory
        ->createResponse()
        ->withHeader('Content-Type', "$this->contentType; charset=$this->encoding");
    return new ViewResponse(
        $response,
        fn(): StreamInterface => $this->streamFactory->createStream(
            $this->renderProxy(
                $view,
                $parameters,
                $commonParameters,
                $layoutParameters,
                $metaTags,
                $linkTags,
            ),
        ),
    );
}

            
renderAsString() public method

Renders a view as a string.

public string renderAsString ( string $view, array $parameters = [] )
$view string

The view name {@see \Yiisoft\View\WebView::render()}.

$parameters array

The parameters (name-value pairs) that will be extracted and made available in the view file.

return string

The rendering result.

throws Throwable

If an error occurred during rendering.

throws \Yiisoft\View\Exception\ViewNotFoundException

If the view file does not exist.

throws RuntimeException

If the view cannot be resolved.

                public function renderAsString(string $view, array $parameters = []): string
{
    return $this->renderProxy(
        $view,
        $parameters,
        $this->getCommonParameters(),
        $this->getLayoutParameters(),
        $this->getMetaTags(),
        $this->getLinkTags(),
    );
}

            
renderPartial() public method

Returns a response instance that supports deferred rendering {@see render()} without applying a layout.

public \Psr\Http\Message\ResponseInterface renderPartial ( string $view, array $parameters = [] )
$view string

The view name {@see \Yiisoft\View\WebView::render()}.

$parameters array

The parameters (name-value pairs) that will be extracted and made available in the view file.

return \Psr\Http\Message\ResponseInterface

The response instance.

                public function renderPartial(string $view, array $parameters = []): ResponseInterface
{
    if ($this->layout === null) {
        return $this->render($view, $parameters);
    }
    return $this
        ->withLayout(null)
        ->render($view, $parameters);
}

            
renderPartialAsString() public method

Renders a view as string {@see renderAsString()} without applying a layout.

public string renderPartialAsString ( string $view, array $parameters = [] )
$view string

The view name {@see \Yiisoft\View\WebView::render()}.

$parameters array

The parameters (name-value pairs) that will be extracted and made available in the view file.

return string

The rendering result.

throws Throwable

If an error occurred during rendering.

throws \Yiisoft\View\Exception\ViewNotFoundException

If the view file does not exist.

throws RuntimeException

If the view cannot be resolved.

                public function renderPartialAsString(string $view, array $parameters = []): string
{
    if ($this->layout === null) {
        return $this->renderAsString($view, $parameters);
    }
    return $this
        ->withLayout(null)
        ->renderAsString($view, $parameters);
}

            
withAddedInjections() public method

Return a new instance with the appended specified injections.

public self withAddedInjections ( object|string $injections )
$injections object|string

The injection instances or class names.

                public function withAddedInjections(object|string ...$injections): self
{
    $new = clone $this;
    $new->setInjections(array_merge($this->injections, $injections));
    return $new;
}

            
withContentType() public method

Returns a new instance with the specified content type.

public self withContentType ( string $contentType )
$contentType string

The content type.

                public function withContentType(string $contentType): self
{
    $new = clone $this;
    $new->contentType = $contentType;
    return $new;
}

            
withController() public method

Extracts the controller name and returns a new instance with the controller name.

public self withController ( object $controller )
$controller object

The controller instance.

                public function withController(object $controller): self
{
    $new = clone $this;
    $new->name = $this->extractControllerName($controller);
    return $new;
}

            
withControllerName() public method

Returns a new instance with the specified controller name.

public self withControllerName ( string $name )
$name string

The controller name.

                public function withControllerName(string $name): self
{
    $new = clone $this;
    $new->name = $name;
    return $new;
}

            
withEncoding() public method

Returns a new instance with the specified encoding.

public self withEncoding ( string $encoding )
$encoding string

The encoding.

                public function withEncoding(string $encoding): self
{
    $new = clone $this;
    $new->encoding = $encoding;
    return $new;
}

            
withInjections() public method

Returns a new instance with the specified injections.

public self withInjections ( object|string $injections )
$injections object|string

The injection instances or class names.

                public function withInjections(object|string ...$injections): self
{
    $new = clone $this;
    $new->setInjections($injections);
    return $new;
}

            
withLayout() public method

Returns a new instance with the specified layout.

public self withLayout ( string|null $layout )
$layout string|null

The full path to the layout file to be applied to views. If null, the layout will not be applied.

                public function withLayout(?string $layout): self
{
    $new = clone $this;
    $new->layout = $layout;
    return $new;
}

            
withLocale() public method

Returns a new instance with specified locale code.

public self withLocale ( string $locale )
$locale string

The locale code.

                public function withLocale(string $locale): self
{
    $new = clone $this;
    $new->locale = $locale;
    return $new;
}

            
withViewPath() public method

Returns a new instance with the specified view path.

public self withViewPath ( string $viewPath )
$viewPath string

The full path to the directory of views or its alias.

                public function withViewPath(string $viewPath): self
{
    $new = clone $this;
    $new->setViewPath($viewPath);
    return $new;
}