Laravel5.5更新,透過Laravel5.5開發Api更加順暢了,在這裡就分享一下Laravel開發Api的經驗吧
1.封裝返回的統一訊息
返回的自定義訊息,和錯誤訊息,我自己封裝了一個Trait,用來做基本的返回,Trait的封裝如下
namespace App\Api\Helpers\Api;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Response;
trait ApiResponse
{
/**
* @var int
*/
protected $statusCode = FoundationResponse::HTTP_OK;
/**
* @return mixed
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* @param $statusCode
* @return $this
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* @param $data
* @param array $header
* @return mixed
*/
public function respond($data, $header = [])
{
return Response::json($data,$this->getStatusCode(),$header);
}
/**
* @param $status
* @param array $data
* @param null $code
* @return mixed
*/
public function status($status, array $data, $code = null){
if ($code){
$this->setStatusCode($code);
}
$status = [
'status' => $status,
'code' => $this->statusCode
];
$data = array_merge($status,$data);
return $this->respond($data);
}
/**
* @param $message
* @param int $code
* @param string $status
* @return mixed
*/
public function failed($message, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'error'){
return $this->setStatusCode($code)->message($message,$status);
}
/**
* @param $message
* @param string $status
* @return mixed
*/
public function message($message, $status = "success"){
return $this->status($status,[
'message' => $message
]);
}
/**
* @param string $message
* @return mixed
*/
public function internalError($message = "Internal Error!"){
return $this->failed($message,FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
}
/**
* @param string $message
* @return mixed
*/
public function created($message = "created")
{
return $this->setStatusCode(FoundationResponse::HTTP_CREATED)
->message($message);
}
/**
* @param $data
* @param string $status
* @return mixed
*/
public function success($data, $status = "success"){
return $this->status($status,compact('data'));
}
/**
* @param string $message
* @return mixed
*/
public function notFound($message = 'Not Found!')
{
return $this->failed($message,Foundationresponse::HTTP_NOT_FOUND);
}
}
然後建立一個ApiController,透過所有的Api控制器繼承該控制器,實現簡潔的Api返回
<?php
namespace App\Http\Controllers\Api;
use App\Api\Helpers\Api\ApiResponse;
use App\Http\Controllers\Controller;
class ApiController extends Controller
{
use ApiResponse;
// 其他通用的Api幫助函式
}
然後,Api控制器就可以簡潔的返回
<?php
namespace App\Http\Controllers\Api;
class IndexController extends ApiController
{
public function index(){
return $this->message('請求成功');
}
}
2.資源型別的返回
資源返回透過5.5的新特性,API資源實現,具體參見
learnku.com/docs/laravel/5.5/eloqu...
使用方面比之前的Transformer方式更好用更優雅
比如返回使用者的分頁資料,只需要這樣
文件已經很詳細了,這裡不再做概述
<?php
namespace App\Http\Controllers\Api;
use App\Models\User;
use App\Http\Resources\User as UserCollection;
use Illuminate\Support\Facades\Input;
class IndexController extends ApiController
{
public function index(){
return UserCollection::collection(User::paginate(Input::get('limit') ?: 20));
}
}
3. Api授權模組
這裡直接拋棄之前的jwt轉向passport的認證方式,之前論壇已經有相關認證的帖子了
這裡透過重寫AuthenticatesUsers
透過password
的的授權模式模式進行實現
<?php
namespace App\Http\Controllers\Api;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Client;
use Socialite;
use App\Models\User;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Validator;
class AuthenticateController extends ApiController
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('auth:api')->only([
'logout'
]);
}
public function username()
{
return 'phone';
}
// 登入
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'phone' => 'required|exists:user',
'password' => 'required|between:5,32',
]);
if ($validator->fails()) {
$request->request->add([
'errors' => $validator->errors()->toArray(),
'code' => 401,
]);
return $this->sendFailedLoginResponse($request);
}
$credentials = $this->credentials($request);
if ($this->guard('api')->attempt($credentials, $request->has('remember'))) {
return $this->sendLoginResponse($request);
}
return $this->setStatusCode(401)->failed('登入失敗');
}
// 退出登入
public function logout(Request $request)
{
if (Auth::guard('api')->check()){
Auth::guard('api')->user()->token()->revoke();
}
return $this->message('退出登入成功');
}
// 第三方登入
public function redirectToProvider($driver) {
if (!in_array($driver,['qq','wechat'])){
throw new NotFoundHttpException;
}
return Socialite::driver($driver)->redirect();
}
// 第三方登入回撥
public function handleProviderCallback($driver) {
$user = Socialite::driver($driver)->user();
$openId = $user->id;
// 第三方認證
$db_user = User::where('xxx',$openId)->first();
if (empty($db_user)){
$db_user = User::forceCreate([
'phone' => '',
'xxUnionId' => $openId,
'nickname' => $user->nickname,
'head' => $user->avatar,
]);
}
// 直接建立token
$token = $db_user->createToken($openId)->accessToken;
return $this->success(compact('token'));
}
//呼叫認證介面獲取授權碼
protected function authenticateClient(Request $request)
{
$credentials = $this->credentials($request);
// 個人感覺透過.env配置太複雜,直接從資料庫查更方便
$password_client = Client::query()->where('password_client',1)->latest()->first();
$request->request->add([
'grant_type' => 'password',
'client_id' => $password_client->id,
'client_secret' => $password_client->secret,
'username' => $credentials['phone'],
'password' => $credentials['password'],
'scope' => ''
]);
$proxy = Request::create(
'oauth/token',
'POST'
);
$response = \Route::dispatch($proxy);
return $response;
}
protected function authenticated(Request $request)
{
return $this->authenticateClient($request);
}
protected function sendLoginResponse(Request $request)
{
$this->clearLoginAttempts($request);
return $this->authenticated($request);
}
protected function sendFailedLoginResponse(Request $request)
{
$msg = $request['errors'];
$code = $request['code'];
return $this->setStatusCode($code)->failed($msg);
}
}
4.自定義返回異常
這裡我的做法是直接攔截App\Exceptions\Handler
的render
方法,實現自定義返回
<?php
namespace App\Exceptions;
use App\Api\Helpers\Api\ExceptionReport;
use Exception;
use Illuminate\Foundation\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)
{
// 將方法攔截到自己的ExceptionReport
$reporter = ExceptionReport::make($exception);
if ($reporter->shouldReturn()){
return $reporter->report();
}
return parent::render($request, $exception);
}
}
然後在該方法實現自定義返回
<?php
namespace App\Api\Helpers\Api;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
class ExceptionReport
{
use ApiResponse;
/**
* @var Exception
*/
public $exception;
/**
* @var Request
*/
public $request;
/**
* @var
*/
protected $report;
/**
* ExceptionReport constructor.
* @param Request $request
* @param Exception $exception
*/
function __construct(Request $request, Exception $exception)
{
$this->request = $request;
$this->exception = $exception;
}
/**
* @var array
*/
public $doReport = [
AuthenticationException::class => ['未授權',401],
ModelNotFoundException::class => ['該模型未找到',404]
];
/**
* @return bool
*/
public function shouldReturn(){
if (! ($this->request->wantsJson() || $this->request->ajax())){
return false;
}
foreach (array_keys($this->doReport) as $report){
if ($this->exception instanceof $report){
$this->report = $report;
return true;
}
}
return false;
}
/**
* @param Exception $e
* @return static
*/
public static function make(Exception $e){
return new static(\request(),$e);
}
/**
* @return mixed
*/
public function report(){
$message = $this->doReport[$this->report];
return $this->failed($message[0],$message[1]);
}
}
好啦,所有的基礎模組都構建完了,現在就可以開發你需要的業務邏輯啦~
個人部落格地址:www.timenotes.me/articles/code/11
歡迎大佬參觀~
本作品採用《CC 協議》,轉載必須註明作者和本文連結