Skip to content

Commit

Permalink
new example added
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Jan 6, 2024
1 parent b6afab1 commit b80210f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b80210f

Please sign in to comment.