php laravel的ApiModel实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php laravel的ApiModel实现相关的知识,希望对你有一定的参考价值。

# laravel的ApiModel实现
1、实现原有laravel-admin的表格来展示接口的api
2、底层继承ApiModel.php文件即可
<?php
/**
 * Created by PhpStorm.
 * User: code
 * Date: 2019-01-08
 * Time: 17:39
 */

namespace App\Repositories\Common\ApiModel;


use App\Repositories\Common\ProxyInstanceClass;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;

class ApiBuilder extends Builder
{
    /**
     * 构造函数
     *
     * @param QueryBuilder $query
     */
    public function __construct(QueryBuilder $query)
    {
        parent::__construct($query);

        $this->setQuery(new ProxyInstanceClass(new ApiQueryBuilder(), $query));
    }

    /**
     * 重写get入口方法,改为访问getApiData方法
     *
     * @param array $columns
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function get($columns = ['*'])
    {
        $result = [];
        $data = $this->getModel()->getApiData();

        // 转为laravel的model数据集
        foreach ($data as $item) {
            $result[] = $this->getModel()->make($item);
        }

        return collect($result);
    }

    /**
     * 分页获取
     *
     * 重写,解决count在设置分页前的问题
     *
     * @param null $perPage
     * @param array $columns
     * @param string $pageName
     * @param null $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
        $page = $page ?: \Illuminate\Pagination\Paginator::resolveCurrentPage($pageName);

        $perPage = $perPage ?: $this->model->getPerPage();

        // 解决count在设置分页前的问题
        // $this->getModel()->forPage()设置的话不会在toBase时候拷贝limit和offset的值
        $this->getModel()->setApiPageData($page, $perPage);

        return parent::paginate($perPage, $columns, $pageName, $page);
    }

    /**
     * @return Builder|\Illuminate\Database\Eloquent\Model|ApiModelInterface
     */
    public function getModel()
    {
        return parent::getModel();
    }

    public function setModel(Model $model)
    {
        if (!$model instanceof ApiModelInterface){
            abort(500, 'model没有实现ApiModelInterface');
        }

        $return = parent::setModel($model);
        $this->getQuery()->setModel($model);

        return $return;
    }

    /**
     * @param QueryBuilder|object $query
     * @return Builder
     */
    public function setQuery($query)
    {
        return parent::setQuery($query);
    }

    /**
     * @return QueryBuilder|ApiQueryBuilder
     */
    public function getQuery()
    {
        return parent::getQuery();
    }


}
<?php
/**
 * Created by PhpStorm.
 * User: code
 * Date: 2019-01-08
 * Time: 17:53
 */

namespace App\Repositories\Common\ApiModel;


interface ApiModelInterface
{
    public function getApiData();
    public function getApiCount();
    // 解决count在get和forPage方法之前调用
    public function setApiPageData($page, $size); // 分页数据
}
<?php
/**
 * Created by PhpStorm.
 * User: code
 * Date: 2019-01-08
 * Time: 18:14
 */

namespace App\Repositories\Common\ApiModel;



/**
 * @mixin \Illuminate\Database\Query\Builder
 */
class ApiQueryBuilder
{
    /**
     * @var \Illuminate\Database\Eloquent\Model|ApiModelInterface
     */
    protected $model;


    /**
     * 重写getCountForPagination
     *
     * @return int
     */
    public function getCountForPagination()
    {
        return $this->getModel()->getApiCount();
    }

    /**
     * @param mixed $model
     * @return $this
     */
    public function setModel($model)
    {
        $this->model = $model;

        return $this;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Model|ApiModelInterface
     */
    public function getModel()
    {
        return $this->model;
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: code
 * Date: 2019-01-09
 * Time: 10:19
 */

namespace App\Repositories\Common\ApiModel;


trait ModelNewEloquentBuilder
{
    /**
     * Create a new Eloquent query builder for the model.
     *
     * @param  \Illuminate\Database\Query\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder|static|ApiBuilder
     */
    public function newEloquentBuilder($query)
    {
        return new ApiBuilder($query);
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: code
 * Date: 2019-01-09
 * Time: 18:45
 */

namespace App\Models;


use App\Repositories\Common\ApiModel\ApiModelInterface;
use App\Repositories\Common\ApiModel\ModelNewEloquentBuilder;

abstract class ApiModel extends Model implements ApiModelInterface
{
    use ModelNewEloquentBuilder;

    protected $guarded = []; // 必须定义

    protected $pageData = [
        'page' => 1,
        'size' => 15
    ];

    /**
     * @param $page
     * @param $size
     * @return $this
     */
    public function setApiPageData($page, $size)
    {
        $this->pageData = [
            'page' => $page,
            'size' => $size,
        ];
        return $this;
    }

    /**
     * @return array
     */
    public function getPageData()
    {
        return $this->pageData;
    }
}

以上是关于php laravel的ApiModel实现的主要内容,如果未能解决你的问题,请参考以下文章

关于Swagger @ApiModel 返回内容注释不显示问题

自定义SpringBoot+Swagger中@ApiModel默认名称

Laravel 8 实现 PHP 的 DOM XML 函数输出 XML

在实体类中添加了@ApiModel不起作用

解读Laravel,看PHP如何实现Facade?

利用Http请求实现PHP异步(laravel5.4)