PHP code example of kodepandai / laravel-api-response

1. Go to this page and download the library: Download kodepandai/laravel-api-response 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/ */

    

kodepandai / laravel-api-response example snippets


use KodePandai\ApiResponse\ApiExceptionHandler;

return Application::configure(basePath: dirname(__DIR__))
    //...
    ->withExceptions(function (Exceptions $exceptions) {
        // dont report any api response exception
        $exceptions->dontReport([
            \KodePandai\ApiResponse\Exceptions\ApiException::class,
            \KodePandai\ApiResponse\Exceptions\ApiValidationException::class,
        ]);
        // api response exception handler for /api
        $exceptions->renderable(function (Throwable $e, Request $request) {
            if ($request->wantsJson() || $request->is('*api*')) {
                return ApiExceptionHandler::render($e, $request);
            }
        });
    });

use KodePandai\ApiResponse\ApiExceptionHandler;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        \KodePandai\ApiResponse\Exceptions\ApiException::class,
        \KodePandai\ApiResponse\Exceptions\ApiValidationException::class,
    ];

    public function register()
    {
        $this->renderable(function (Throwable $e, Request $request) {
            if ($request->wantsJson() || $request->is('*api*')) {
                return ApiExceptionHandler::render($e, $request);
            }
        });
    }
}