JSON API action: WebViewRenderer::render('') throws 'The view path is not set'
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 381
- Forks
- 68
- Avg merge
- 1h 10m
- Merged PRs (30d)
- 1
Description
JSON API action: WebViewRenderer::render('') throws "The view path is not set"
Description
When building a JSON API endpoint in a Yii3 yiisoft/app project, the most intuitive approach is to reuse WebViewRenderer and return an empty view with a JSON body, e.g.:
final readonly class HealthAction
{
public function __construct(private WebViewRenderer $viewRenderer) {}
public function __invoke(): ResponseInterface
{
$response = $this->viewRenderer->render('')
->withHeader('Content-Type', 'application/json');
$response->getBody()->write(json_encode([...]));
return $response;
}
}
This throws:
LogicException: The view path is not set.
vendor/yiisoft/yii-view-renderer/src/WebViewRenderer.php:101
Environment
yiisoft/app(latest stable,composer create-project2026-08)- PHP 8.3.30, current
yiisoft/yii-view-renderer
Workaround that works
Use Psr\Http\Message\ResponseFactoryInterface (already bound in config/web/di/psr17.php) to build the JSON response directly instead of WebViewRenderer.
Questions for maintainers
- Is
WebViewRenderer::render('')intended to be usable for non-HTML responses, or should the docs/examples explicitly recommendResponseFactoryInterfacefor JSON APIs? - If the former, is "The view path is not set" the expected behaviour, or should an empty/raw response path be allowed for API actions?
A minimal reproducible example is attached (Vanilla JS + Yii3 co-work demo, see linked PR/discussion).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at vendor/yiisoft/yii-view-renderer/src/WebViewRenderer.php:101 and review config/web/di/psr17.php, which already binds ResponseFactoryInterface. Clarify whether the intended outcome is renderer support or documentation recommending the response factory; done requires a maintainer decision and corresponding behavior or guidance, with validation for the JSON API case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100