PHP code example of lalu / jer

1. Go to this page and download the library: Download lalu/jer 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/ */

    

lalu / jer example snippets


'providers' => [
    // ...
    LaLu\JER\JERServiceProvider::class,
]

'aliases' => [
    // ...
    'JER' => LaLu\JER\Facades\JERFacade::class
]

$app->register(LaLu\JER\JERServiceProvider::class);

$app->withFacades();

class_alias(LaLu\JER\Facades\JERFacade::class, 'JER');

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    // ...
}

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    public $meta = [
        'meta_field_1' => 'meta_value_1',
        // ...
    ];

    // ...
}

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    public function beforeRender($request, Exception $exception)
    {
        $this->meta = [
            'meta_field_1' => 'meta_value_1',
            // ...
        ];
    }

    // ...
}

use LaLu\JER\ExceptionHandler;
use LaLu\JER\Error;

class Handler extends ExceptionHandler
{
    // ...

    /**
     * Get exception jsonapi data.
     *
     * @param \Exception $exception
     *
     * @return array
     */
    protected function getExceptionError(Exception $exception)
    {
        if ($exception instanceof \Exception\You\Want) {
            // status must be an integer and is a HTTP error status code
            $status = 400;
            // headers must be an array of key value
            $headers = [];
            $content = [
                'title' => 'Your exception custom title',
                'detail' => 'Your exception custom detail',
            ];
            // error can be an instance/array items of \LaLu\JER\Error or array of error array
            $error = new Error(['version' => $this->jsonapiVersion], $content);
            $error->status = '400';
            // ...
            return [$status, $error, $headers];
        } elseif ($exception instanceof \Other\Exception) {
            return [400, [['title' => 'Your request is bad request']], []];
        } else {
            return parent::getExceptionError($exception);
        }
    }
}

$option = [
    'version' => '1.0', // JSONAPI specification version
    'status' => 400, // HTTP status code
    'headers' => ['My-Custom-Header' => 'Value'], // Response headers,
    'exception' => new \Exception(), // Exception
];
$attributes = [
    'meta' => [
        'apiVersion' => '1.0.0',
        'author' => 'thanh-taro',
    ],
    'errors' => new Error(['version' => '1.0'], ['title' => 'My custom error', 'detail' => 'This is an error response']), // Error content
];
$response = \JER::getResponse($option, $attributes);

(new \LaLu\JER\JsonExceptionResponse())->getResponse($option, $attributes);
bash
php artisan vendor:publish