PHP code example of aslnbxrz / simple-exception

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

    

aslnbxrz / simple-exception example snippets


'response' => [
    'template' => 'default',

    'templates' => [
        'default' => [
            'success' => ':success',
            'data'    => ':data',
            'error'   => [
                'message' => ':message',
                'code'    => ':code',
            ],
            'meta'    => ':meta',
        ],
    ],
],

'default_error_code' => -1,

'enum_generation' => [
    'resp_codes_dir' => 'Enums/RespCodes', // relative to app/
],

'translations' => [
    'base_path' => 'vendor/simple-exception',
],

use App\Enums\RespCodes\UserRespCode;

// Always throws SimpleErrorResponse
error(UserRespCode::NotFound);

// Conditional helpers
error_if(!$user, UserRespCode::NotFound);
error_unless($user->can('update'), UserRespCode::Forbidden);

// Custom string error
error_response('Custom failure', 1001);

use App\Enums\RespCodes\UserRespCode;

class UserController extends Controller
{
    public function show($id)
    {
        $user = User::find($id);

        error_if(!$user, UserRespCode::NotFound);

        return response()->json(['user' => $user]);
    }
}



return [
    'not_found'  => 'User not found',
    'forbidden'  => 'Access denied',
];
bash
php artisan vendor:publish --tag=simple-exception-config
bash
php artisan make:resp-code User --cases="NotFound=404,Forbidden=403" --locale=en,uz
bash
php artisan sync:resp-translations UserRespCode --locale=uz