I am trying to output json view (without the need of a template) in my Cake5 controller.
Base on JSON Docs, my code is similar. but i am still getting the error ‘no template’.
Am I missing something ?
// My controller file. No template files has been created.
public function initialize(): void {
parent::initialize();
$this->addViewClasses([JsonView::class, ]);
}
public function viewClasses(): array
{
return [JsonView::class];
}
public function testJsonView()
{
$articles = ['test view'];
// Set the view vars that have to be serialized.
$this->set(compact('articles', ));
// Specify which view vars JsonView should serialize.
$this->viewBuilder()->setOption('serialize', ['articles', ]);
}