PHP code example of rockyfc / laravel-smart-apidoc
1. Go to this page and download the library: Download rockyfc/laravel-smart-apidoc 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/ */
rockyfc / laravel-smart-apidoc example snippets
/**
* 接口标题
* ...
*
* @return AnonymousResourceCollection
* ...
*/
public function index(FormRequest $request)
{
//code...
}
/**
* 接口标题
* ...
* @see LengthAwarePaginator
* @return AnonymousResourceCollection
* ...
*/
public function index(FormRequest $request)
{
//code...
}
/**
* 需要排序的字段
* @return string[]
*/
public function sorts()
{
return ['id', 'updated_at'];
}
namespace App\Http\Controllers;
use App\Http\Repositories\CateRepository;
use App\Http\Requests\CateRequest;
use App\Http\Resources\CateResource;
use App\Models\Cate;
use Exception;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Validation\ValidationException;
/**
* 分类接口
*
* 这是一个文章分类的接口管理,其中包括增删改查。
*
* @author 张三
* @date 2021-02-07
* @package App\Http\Controllers
*/
class CateController extends \Illuminate\Routing\Controller
{
/**
* @var CateRepository
*/
protected $repository;
/**
* @param CateRepository $repository
*/
public function __construct(CateRepository $repository)
{
$this->repository = $repository;
}
/**
* 列表
*
* 列表数据,支持分页、自定义排序、按需获取。
* @param CateRequest $request
* @see LengthAwarePaginator
* @see CateResource
* @return AnonymousResourceCollection
* @throws ValidationException
* @author 张三
* @date 2021-02-07
*/
public function index(CateRequest $request)
{
//创建一条查询
$query = $this->repository->search(
$request->validated(), //获取验证后的数据
$request->getFilteredRelations(), //加载关联
$request->getResolvedSorts() //加载排序
);
//返回集合
return CateResource::collection(
$query->paginate($request->getPerPage())
);
}
/**
* 详情
* @param Cate $cate
* @param CateRequest $request
* @return CateResource
* @throws ValidationException
* @author 张三
* @date 2021-02-07
*/
public function show(Cate $cate, CateRequest $request)
{
//加载关联
if ($relations = $request->getFilteredRelations()) {
$cate->load($relations);
}
//返回一条资源
return new CateResource(
$cate
);
}
/**
* 新建
* @param CateRequest $request
* @return CateResource
* @author 张三
* @date 2021-02-07
*/
public function store(CateRequest $request)
{
$model = new Cate();
$model->fill($request->validated());
$model->save();
return new CateResource(
$model
);
}
/**
* 编辑
* @param CateRequest $request
* @param Cate $cate
* @return CateResource
* @author 张三
* @date 2021-02-07
*/
public function update(CateRequest $request, Cate $cate)
{
$cate->fill($request->validated());
$cate->save();
return new CateResource(
$cate
);
}
/**
* 删除
* @param Cate $cate
* @return Response
* @throws Exception
* @author 张三
* @date 2021-02-07
*/
public function destroy(Cate $cate)
{
$cate->delete();
return response(null, 204);
}
}
shell
php artisan smart:install-doc