1. Go to this page and download the library: Download idopin/api-support 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/ */
idopin / api-support example snippets
public function render($request, Throwable $e)
{
if ($request->is('api/*')) {
return $this->jsonResponse($e);
}
return parent::render($request, $e);
}
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* Find the user instance for the given username.
*
* @param string $username
* @return \App\Models\User
*/
public function findForPassport($username)
{
return $this->where('name', $username)->first();
}
}
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* Validate the password of the user for the Passport password grant.
*
* @param string $password
* @return bool
*/
public function validateForPassportPasswordGrant($password)
{
return Hash::check($password, $this->password);
}
}
namespace Idopin\ApiSupport\Enums;
enum ApiCode: string
{
case ATTEMPT_TO_MANY = 'attempt_to_many';
case CAPTCHA_EXCEPTION = 'captcha_exception';
case CAPTCHA_INCORRECT = 'captcha_incorrect';
case CAPTCHA_INVALID = 'captcha_invalid';
case DATABASE_QUERY = 'database_query';
case FORM_DATA_INVALID = 'form_data_invalid';
case NO_CONTENT = 'no_content';
case OK = 'ok';
case REDIRECT = 'redirect';
case RESOURCE_CREATED = 'resource_created';
case RESOURCE_EXIST = 'resource_exist';
case RESOURCE_NOT_FOUND = 'resource_not_found';
case RESOURCE_UNCHANGED = 'resource_unchanged';
case RESOURCE_UPDATED = 'resource_updated';
case ROUTE_NOT_FOUND = 'route_not_found';
case SMS_CODE_EXCEPTION = 'sms_code_exception';
case SMS_CODE_INCORRECT = 'sms_code_incorrect';
case SMS_CODE_INVALID = 'sms_code_invalid';
case TOKEN_INVALID = 'token_invalid';
case UNDEFINE = 'undefine';
case USER_AUTH_FAILED = 'user_auth_failed';
case USER_CREATE_FAILED = 'user_create_failed';
case USER_NOT_AUTH = 'user_not_auth';
case USER_NOT_EXIST = 'user_not_exist';
case VERIFICATION_CODE_EXCEPTION = 'verification_code_exception';
case VERIFICATION_CODE_INCORRECT = 'verification_code_incorrect';
case VERIFICATION_CODE_INVALID = 'verification_code_invalid';
}
namespace Idopin\ApiSupport\Traits;
use Idopin\ApiSupport\Enums\ApiCode;
use Throwable;
trait ApiExceptionTrait
{
use ApiResponseTrait;
public function jsonResponse(Throwable $e)
{
...
}
...
}
use Idopin\ApiSupport\Middleware\Human;
...
public function __construct()
{
$this->middleware(Human::class)->only('xxx');
}
sh
php artisan migrate
sh
php artisan authorization:install
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.