yiisoft / yiisoft/app

JSON API action: WebViewRenderer::render('') throws 'The view path is not set'

Open
#478 0 comments 0 reactions 0 assignees View on GitHub

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-project 2026-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
  1. Is WebViewRenderer::render('') intended to be usable for non-HTML responses, or should the docs/examples explicitly recommend ResponseFactoryInterface for JSON APIs?
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.