From b80210fb9c1fc8e24802622ebf5760f1840d12e3 Mon Sep 17 00:00:00 2001 From: rrd108 Date: Sat, 6 Jan 2024 13:15:57 +0100 Subject: [PATCH] new example added --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e2bf847..bdd1063 100755 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ public function initialize(): void } ``` -And add JSON view support. +And add JSON view support to `AppController.php`. ```php use Cake\View\JsonView; @@ -134,6 +134,24 @@ $routes->scope('/', function (RouteBuilder $builder): void { }); ``` +### 5. Set JSON response in controllers + +In your controllers you should set the JSON response type. + +```php +// for example in UsersController.php +public function index() +{ + $query = $this->Users->find(); + $users = $this->paginate($query); + + $this->set(compact('users')); + $this->viewBuilder()->setOption('serialize', ['users']); +} +``` + +As CakePHP response use content type negotiation it is important to add the `Accept: application/json` header to your requests. + That's it. It should be up and running. ## The `login()` method