PHP code example of rrd108 / cakephp-json-api-exception
1. Go to this page and download the library: Download rrd108/cakephp-json-api-exception library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rrd108 / cakephp-json-api-exception example snippets
$this->addPlugin('JsonApiException');
// for example in /src/Controller/UsersController.php slightly change the baked add function
use JsonApiException\Error\Exception\JsonApiException;
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if (!$this->Users->save($user)) {
throw new JsonApiException($user, 'Save failed');
// throw new JsonApiException($user, 'Save failed', 418); // you can set the response's status code in the 3rd parameter
}
}
$this->set(compact('user'));
$this->viewBuilder()->setOption('serialize', ['user']);
}
// for example in /src/Controller/UsersController.php slightly change the baked add function
use JsonApiException\Error\Exception\JsonApiException;
public function bulkAdd()
{
$users = $this->Users->newEntities($this->request->getData());
if (!$this->Users->saveMany($users)) {
throw new JsonApiException($users, 'Errors in request data');
}
$this->set(compact('users'));
$this->viewBuilder()->setOption('serialize', ['users']);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.