Final Class Yiisoft\Yii\View\Renderer\WebViewRenderer
| Inheritance | Yiisoft\Yii\View\Renderer\WebViewRenderer |
|---|---|
| Implements | Yiisoft\View\ViewContextInterface |
Factory that creates PSR-7 response instances with a content rendered by view.
Both Yiisoft\Yii\View\Renderer\WebViewRenderer::render() and Yiisoft\Yii\View\Renderer\WebViewRenderer::renderPartial() return an instance of \Psr\Http\Message\ResponseInterface with deferred rendering support.
Yiisoft\Yii\View\Renderer\WebViewRenderer::renderAsString() and Yiisoft\Yii\View\Renderer\WebViewRenderer::renderPartialAsString() are rendering immediately returning the result of the rendering as a string.
Public 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 render() without applying a layout. | Yiisoft\Yii\View\Renderer\WebViewRenderer |
| renderPartialAsString() | Renders a view as string 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
| public __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|null $injectionContainer = null, string $contentType = 'text/html', string $encoding = 'UTF-8' ): mixed | ||
| $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 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|null | |
| $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);
}
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 withController(), withControllerName(), it will be appended to the path.
| public getViewPath( ): string | ||
| 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 : '');
}
Returns a response instance that supports deferred rendering.
| public render( string $view, array $parameters = [] ): \Psr\Http\Message\ResponseInterface | ||
| $view | string |
The view name \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,
),
),
);
}
Renders a view as a string.
| public renderAsString( string $view, array $parameters = [] ): string | ||
| $view | string |
The view name \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(),
);
}
Returns a response instance that supports deferred rendering render() without applying a layout.
| public renderPartial( string $view, array $parameters = [] ): \Psr\Http\Message\ResponseInterface | ||
| $view | string |
The view name \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);
}
Renders a view as string renderAsString() without applying a layout.
| public renderPartialAsString( string $view, array $parameters = [] ): string | ||
| $view | string |
The view name \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);
}
Return a new instance with the appended specified injections.
| public withAddedInjections( object|string $injections ): self | ||
| $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;
}
Returns a new instance with the specified content type.
| public withContentType( string $contentType ): self | ||
| $contentType | string |
The content type. |
public function withContentType(string $contentType): self
{
$new = clone $this;
$new->contentType = $contentType;
return $new;
}
Extracts the controller name and returns a new instance with the controller name.
| public withController( object $controller ): self | ||
| $controller | object |
The controller instance. |
public function withController(object $controller): self
{
$new = clone $this;
$new->name = $this->extractControllerName($controller);
return $new;
}
Returns a new instance with the specified controller name.
| public withControllerName( string $name ): self | ||
| $name | string |
The controller name. |
public function withControllerName(string $name): self
{
$new = clone $this;
$new->name = $name;
return $new;
}
Returns a new instance with the specified encoding.
| public withEncoding( string $encoding ): self | ||
| $encoding | string |
The encoding. |
public function withEncoding(string $encoding): self
{
$new = clone $this;
$new->encoding = $encoding;
return $new;
}
Returns a new instance with the specified injections.
| public withInjections( object|string $injections ): self | ||
| $injections | object|string |
The injection instances or class names. |
public function withInjections(object|string ...$injections): self
{
$new = clone $this;
$new->setInjections($injections);
return $new;
}
Returns a new instance with the specified layout.
| public withLayout( string|null $layout ): self | ||
| $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;
}
Returns a new instance with specified locale code.
| public withLocale( string $locale ): self | ||
| $locale | string |
The locale code. |
public function withLocale(string $locale): self
{
$new = clone $this;
$new->locale = $locale;
return $new;
}
Returns a new instance with the specified view path.
| public withViewPath( string $viewPath ): self | ||
| $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;
}
Signup or Login in order to comment.