Can't get JSON route to work in basic application

I’m trying to learn about JSON routes, so I whipped up a fresh “bookmarker tutorial” app and tried adding the option for using a /bookmarks.json URL. But I’m getting an error message saying that the

Template file “Bookmarks/json/index.ctp” is missing.

Here’s what I tried: I installed a new version of the bookmarker tutorial from the cakePHP site; baked the scaffolded, hash-protected the passwords; added a user; added a few bookmarks. Then I tried two different approaches to adding a json option to the ‘config/routes.php’ file:

Router::extensions(['json','xml']);

and then when that didn’t work, I tried

Router::scope('/', function (RouteBuilder $routes) {
    $routes->setExtensions(['json','xml']);
    ...

instead.

(Those methods are both mentioned in : Routing - 3.10 )

I confirmed that I have a call to the RequestHandler in my AppController.php file.

$this->loadComponent('RequestHandler');

You can see a git repo of these developments at: Commits · kenirwin/cake-json · GitHub

Any idea what I’m doing wrong?

(This, of course, it not what I’m trying to work on. This was supposed to be the easy setup so I could start learning about making it more complex…)

Thanks
Ken

try to also add in controller action

$this->set('_serialize', ['bookmarks']);

EDIT:
https://book.cakephp.org/3.0/en/views/json-and-xml-views.html#using-data-views-with-the-serialize-key

1 Like

That worked great - thank you!