Skip to content

控制器

提示

BaseController中,我们已经定义了几个基础的控制器方法 save, update, destroy, read, 而我们平常定义的控制器,都是继承了BaseController, 所以我们在自己的控制器中,不需要定义这几个方法,就可以直接使用。

控制器定义

例如以下控制器中,我们只定义indeximport两个方法,由于当前控制器继承自BaseController,所以当前控制器实际包含index, import,save, update, destroy, read 一共六个方法可以被使用。

php
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller\news;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\news\ArticleLogic;
use app\cms\validate\news\ArticleValidate;
use support\Request;
use support\Response;

/**
 * 文章管理控制器
 */
class ArticleController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new ArticleLogic();
        $this->validate = new ArticleValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['title', ''],
            ['author', ''],
        ]);
        $query = $this->logic->search($where);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 导入数据
     * @param Request $request
     * @return Response
     */
    public function import(Request $request) : Response
    {
        $file = current($request->file());
        if (!$file || !$file->isValid()) {
            return $this->fail('未找到上传文件');
        }
        $this->logic->import($file);
        return $this->success('导入成功');
    }
}

分页数据

框架已经针对常规的数据分页做了封装,你可以直接使用,例如以下代码:

php
<?php
  #---省略代码------------------------
    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
      $where = $request->more([
            ['title', ''],
            ['author', ''],
        ]);
        $query = $this->logic->search($where);
        // 分页获取数据
        $data = $this->logic->getList($query);
    }

提示

前端框架请求参数中,会自动包含pagelimit两个参数,分别表示当前页码和每页显示的条数;如果前端框架增加了saiType=all这个参数,在调用该方法的时候,会自动获取全部数据,做到一个接口只需要变动参数就能获取不同类型的数据的功能

全部数据

php
<?php
  #---省略代码------------------------
    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
      $where = $request->more([
            ['title', ''],
            ['author', ''],
        ]);
        $query = $this->logic->search($where);
        // 获取全部数据
        $data = $this->logic->getAll($query);
    }

强制请求类型

我们可以通过框架自带的方法验证请求类型,例如以下操作限制当前请求方法必须为post

php
<?php
  #---省略代码------------------------

    /**
     * 检查订单
     * @param Request $request
     * @return Response
     */
    public function checkOrder(Request $request) : Response
    {
      // 强制验证请求类型为post
      $this->checkMethod('post');

      $data = $request->post();
      $this->logic->checkOrder($data);
      return $this->success('检查成功');
    }

默认排序字段和排序类型

通常我们在index中获取数据时候,希望调整以下排序字段和排序类型,默认情况下,排序字段为id,排序类型为desc,由于我们在BaseLogic里面设计了默认排序方法操作,所以我们在index中使用setOrderField设置排序字段setOrderType设置排序方式进行操作:

php
<?php
  #---省略代码------------------------
    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
      $where = $request->more([
            ['title', ''],
            ['author', ''],
        ]);
        $query = $this->logic->search($where);
        // 设置排序字段
        $this->logic->setOrderField('sort');
        // 设置排序方式
        $this->logic->setOrderType('DESC');
        $data = $this->logic->getList($query);
    }

后置方法afterChange

BaseController中,已经定义了afterChange方法,这个方法会在saveupdatedestroy 方法执行之后执行,你可以在这里进行一些操作,例如清理缓存,发送邮件等等。例如我们在SystemDictTypeController这个字典控制器中,需要在字典数据改变后,清理缓存,这样就能保持字段数据最新。

php
<?php
  #---省略代码------------------------
  /**
     * 数据改变后执行
     * @param $type
     * @param $args
     * @return void
     */
    protected function afterChange($type, $args): void
    {
        if (in_array($type, ['save', 'update'])) {
            Cache::delete(request()->input('code'));
        }
    }