2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
namespace App\Abstracts\Http;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-11-18 13:19:24 +03:00
|
|
|
use App\Traits\Jobs;
|
|
|
|
use App\Traits\Permissions;
|
|
|
|
use App\Traits\Relationships;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Exceptions\Http\Resource as ResourceException;
|
2020-11-18 13:19:24 +03:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Illuminate\Http\Request;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Http\Response;
|
2020-11-18 13:19:24 +03:00
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-11-18 13:19:24 +03:00
|
|
|
abstract class ApiController extends BaseController
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2022-06-01 10:15:55 +03:00
|
|
|
use AuthorizesRequests, Jobs, Permissions, Relationships, ValidatesRequests;
|
2020-11-18 13:19:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->assignPermissionsToController();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Create the response for when a request fails validation.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param array $errors
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
protected function buildFailedValidationResponse(Request $request, array $errors)
|
|
|
|
{
|
|
|
|
if ($request->expectsJson()) {
|
|
|
|
throw new ResourceException('Validation Error', $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to($this->getRedirectUrl())->withInput($request->input())->withErrors($errors, $this->errorBag());
|
|
|
|
}
|
2022-06-01 10:15:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond with a location and a created resource.
|
|
|
|
*
|
|
|
|
* @param string $location
|
|
|
|
* @param object $resource
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function created($location, $resource): JsonResponse
|
|
|
|
{
|
|
|
|
return $resource
|
|
|
|
->response()
|
|
|
|
->setStatusCode(201)
|
|
|
|
->header('Location', $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond with a location and an accepted resource.
|
|
|
|
*
|
|
|
|
* @param string $location
|
|
|
|
* @param object $resource
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function accepted($location, $resource): JsonResponse
|
|
|
|
{
|
|
|
|
return $resource
|
|
|
|
->response()
|
|
|
|
->setStatusCode(202)
|
|
|
|
->header('Location', $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond with empty content.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function noContent(): Response
|
|
|
|
{
|
|
|
|
return (new Response)
|
|
|
|
->setStatusCode(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an error response.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param int $statusCode
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function error($message, $statusCode)
|
|
|
|
{
|
|
|
|
throw new HttpException($statusCode, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 404 not found error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorNotFound($message = 'Not Found')
|
|
|
|
{
|
|
|
|
$this->error($message, 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 400 bad request error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorBadRequest($message = 'Bad Request')
|
|
|
|
{
|
|
|
|
$this->error($message, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 403 forbidden error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorForbidden($message = 'Forbidden')
|
|
|
|
{
|
|
|
|
$this->error($message, 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 500 internal server error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorInternal($message = 'Internal Error')
|
|
|
|
{
|
|
|
|
$this->error($message, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 401 unauthorized error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorUnauthorized($message = 'Unauthorized')
|
|
|
|
{
|
|
|
|
$this->error($message, 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 405 method not allowed error.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function errorMethodNotAllowed($message = 'Method Not Allowed')
|
|
|
|
{
|
|
|
|
$this->error($message, 405);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|