I am trying to output json. using the example in the docs here https://book.cakephp.org/4/en/views/json-and-xml-views.html
Going to the following URL gives me a missing template error. I just want the json I dont want to use a template
http://localhost:8765/Articles/index
What am I doing wrong?
namespace App\Controller;
use Cake\View\JsonView;
class ArticlesController extends AppController
{
public function viewClasses(): array
{
return [JsonView::class];
}
public function index()
{
// Some code that created $articles and $comments
$articles = [
"one" => "Article 1",
"two" => "Article 2",
"three" => "Article 3",
"four" => "Article 4",
];
// Set the view vars that have to be serialized.
$this->set("articles",$articles);
// Specify which view vars JsonView should serialize.
$this->viewBuilder()->setOption('serialize', 'articles');
}
}