PHP code example of cyd622 / laravel-api

1. Go to this page and download the library: Download cyd622/laravel-api 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/ */

    

cyd622 / laravel-api example snippets


// 成功返回 第一个参数可接受item和resource
return $this->success($user,$meta);
// 只返回信息无内容
return $this->setHttpCode(201)->setStatusCode(1002001)->message('用户创建成功...');
// 错误返回
return $this->error('用户登录失败',401,10001);

namespace App\Http\Resources;

use App\Traits\PaginatedCollection;
use Illuminate\Http\Resources\Json\JsonResource;

class Company extends JsonResource
{
    use PaginatedCollection;

}

 return \App\Http\Resources\Company::collection($company);

use LaravelApi\Response\ExceptionReport;

public function render($request, Exception $exception)
{
    // 将方法拦截到自己的ExceptionReport
    $reporter = ExceptionReport::make($exception);

    if ($reporter->shouldReturn()) {
        return $reporter->report();
    }

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

class User extends Authenticatable implements JWTSubject
{
    ...
    
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }
}
shell
php artisan vendor:publish --provider="Cyd622\LaravelApi\ApiServiceProvider"
shell
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
shell
php artisan jwt:secret

Route::middleware('api.refresh:api')->group(function () {
    // api看守器登陆授权
    ...
});

Route::middleware('api.refresh:admin')->group(function () {
    // admin看守器登陆授权
    ...
});