PHP code example of salmanzafar / laravel-api-exceptions-handler

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

    

salmanzafar / laravel-api-exceptions-handler example snippets


public function getUser(User $user)
{
  return $user;
}

namespace App\Exceptions;
 
use Exception;
use App\Exceptions\ExceptionTrait;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
 
class Handler extends ExceptionHandler
{
     use ExceptionTrait;
 
     /**
      * A list of the exception types that are not reported.
      *
      * @var array
      */
     protected $dontReport = [
         //
     ];
 
     /**
      * A list of the inputs that are never flashed for validation exceptions.
      *
      * @var array
      */
     protected $dontFlash = [
         'password',
         'password_confirmation',
     ];
 
     /**
      * Report or log an exception.
      *
      * @param  \Exception  $exception
      * @return void
      */
     public function report(Exception $exception)
     {
         parent::report($exception);
     }
 
     /**
      * Render an exception into an HTTP response.
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  \Exception  $exception
      * @return \Illuminate\Http\Response
      */
     public function render($request, Exception $exception)
     {
         if ($request->expectsJson())
         {
             return $this->ApiExceptions($request,$exception);
         }
 
         return parent::render($request, $exception);
     }
}



namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Salman\ApiExceptionHandler\Concerns\MyValidationException;

class CarRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
    
    protected function failedValidation(Validator $validator)
    {
        throw new MyValidationException($validator);
    }
}

namespace App;

use \Salman\ApiExceptionHandler\Concerns\UsesUuid;
use Illuminate\Database\Eloquent\Model;

class Car extends Model
{
   use UsesUuid;
}



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCarsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->uuid('id')->primary();            
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

bash
php artisan vendor:publish --provider="Salman\ApiExceptionHandler\ApiExceptionHandlerServiceProvider::class"
bash
php artisan publish:traits