galaxydevs / laravel-api-master
该软件包最新版本(v1.0.0)没有提供许可证信息。
创建 Laravel API
v1.0.0
2022-07-15 09:41 UTC
This package is auto-updated.
Last update: 2024-09-15 14:32:43 UTC
README
步骤 1:通过 Composer 安装
composer require galaxydevs/laravel-api-master
步骤 2:在 config/app.php 的 providers[] 数组中添加新的服务提供者
DevDr\ApiCrudGenerator\DrCrudServiceProvider::class
步骤 3:在 "app/Exceptions/Handler.php" 中添加此函数
use Psy\Util\Json; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; public function _errorMessage($responseCode = 400, $message = 'Bad Request'){ $body = Json::encode( array( "success" => false, "responseCode" => $responseCode, 'message' => $message ) ); echo $body; die; }
步骤 4:将以下函数添加到 "render()" 中
public function render($request, Exception $exception) { if($exception instanceof NotFoundHttpException){ $this->_errorMessage(404,'Page Not Found'); } if ($exception instanceof MethodNotAllowedHttpException) { $this->_errorMessage(405,'Method is not allowed for the requested route'); } return parent::render($request, $exception); }
步骤 5:将此行添加到 "app/Http/Kernel.php" 的 $routeMiddleware[] 中
'api.auth' => \DevDr\ApiCrudGenerator\Middleware\CheckAuth::class,
现在您可以在任何地方使用此 'api.auth' 中间件
您可以将 "AUTH-TOKEN" 通过 API 头部传递进行身份验证检查
步骤 6:使用 "crud:api-generator" 创建 CRUD
php artisan crud:api-generator User
步骤 7:在函数中使用该中间件后,您可以按此方式使用用户对象
$user = $request->get('users');
步骤 8:在 Users 模型中添加此函数
public static function findIdentityByAccessToken($token, $type = null) { return static::where(['auth_token' => $token])->first(); }
或者
如果没有使用中间件,您可以直接在控制器 api 动作中使用以下函数
$user = $this->_checkAuth();
贡献
我们不建议您使用此软件包。使用它需自行承担风险。