PHP code example of ibnudirsan / handle-http-api

1. Go to this page and download the library: Download ibnudirsan/handle-http-api 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/ */

    

ibnudirsan / handle-http-api example snippets


// app/Exceptions/Handler.php



namespace App\Exceptions;

use Throwable;
use Ibnudirsan\HandleHttpApi\Handler\HttpResponse;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception) {

        if ($request->is('api/*')) {
            $code = method_exists($exception, 'getStatusCode');
            if (method_exists($exception, 'getStatusCode')) {
                $statusCode = $this->prepareException($exception)->getStatusCode();
                return HttpResponse::Status($statusCode);
            } else {
                $statusCode = 500;
                    return HttpResponse::Status($statusCode);
            }
                return parent::render($request, $exception);
        }

    }
}