1. Go to this page and download the library: Download wendelladriel/laravel-hut 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/ */
wendelladriel / laravel-hut example snippets
// CHANGE THIS
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
// TO THIS
namespace App\Exceptions;
use Throwable;
use WendellAdriel\LaravelHut\Exceptions\ApiHandler;
class Handler extends ApiHandler
namespace App\Exceptions;
use Exception;
use Illuminate\Http\Response;
use WendellAdriel\LaravelHut\Exceptions\AppExceptionInterface;
class AccessDeniedException extends Exception implements AppExceptionInterface
{
public function __construct()
{
parent::__construct('Access Denied', Response::HTTP_FORBIDDEN);
}
}
/**
* Builds and sends a simple success API response
*
* @param int $code
* @return JsonResponse
*/
protected function apiSimpleSuccessResponse(int $code = Response::HTTP_CREATED): JsonResponse
{
return response()->json(['success' => true], $code);
}
/**
* Builds and sends a success API response
*
* @param mixed $data
* @param int $code
* @param bool $forceUTF8Convert
* @return JsonResponse
*/
protected function apiSuccessResponse(
$data,
int $code = Response::HTTP_OK,
bool $forceUTF8Convert = false
): JsonResponse {
$formattedData = $forceUTF8Convert ? $this->convertToUTF8Recursively($data) : $data;
return response()->json($formattedData, $code);
}
/**
* Builds and sends an error API response
*
* @param string $message
* @param Throwable|null $exception
* @param int $code
* @return JsonResponse
*/
protected function apiErrorResponse(
string $message,
Throwable $exception = null,
int $code = Response::HTTP_INTERNAL_SERVER_ERROR
): JsonResponse {
$response = ['message' => $message];
if (!empty($exception) && config('app.debug')) {
$response['debug'] = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString()
];
}
return response()->json($response, $code);
}