singsys/laravel-quick

本包的最新版本(v2.3)没有提供任何许可信息。

v2.3 2019-10-21 10:06 UTC

This package is auto-updated.

Last update: 2024-09-24 19:59:37 UTC


README

  • 在文件 "App\Http\Controllers\Controller" 中添加特质 "Singsys\LQ\Lib\Concerns\LqResponse"。
After that you can use the following methods in your controller 
setData(Array $data) to set the data in Response conllection.
setError(String $error) to set the single error
setErrors(Array $errors) to set all errors of form.
setErrorCode(String #error_code) to set the error code.
response(Number $request_status_code = 200) to set the request status
  • 在 User 模型中添加特质 "Singsys\LQ\Lib\Concerns\LqToken",然后您可以直接从用户模型生成密码令牌,如下所示:
$user = User::findOrFail($id);
$token = $user->createUserToken();
$user->setAttribute('token', $token);
return $this->setData(['user' => $user])
    ->response();
  • 替换文件 App\Exceptions\Handler 中的以下内容:
<?php

namespace App\Exceptions;

use Singsys\LQ\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception               $exception
     *
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($request->is('api/*')) {
            return $this->lqRender($request, $exception);
        }

        return parent::render($request, $exception);
    }
}

  • 在终端执行以下命令:
php artisan migrate
php artisan lq:install
php artisan vendor:publish --tag=lq-config
  • 修改 RouteServiceProvider 文件,您只需要将 LqApiMiddleware 全局应用于所有 API,如下所示:
protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware([\Singsys\LQ\Middleware\LqApiMiddleware::class])
         ->namespace($this->namespace . '\Api')
         ->group(base_path('routes/api.php'));
}