流程
主要講解controller目錄,每個controller有自己獨立的路由,配置,事件,容器,控制器中可用框架核心及擴展。每個控制器其實就是一個獨立的控制類。
請求流程大致分為以下流程,依次從左到右
Middleware
中間件分為前置和后置中間件.
前置中間件在訪問控制器之前會被執行調用,通常用來攔截參數,跨域配置,多語言加載,Session初始化,權限驗證,登陸驗證等處理;
而后置中間件屬于返回數據后在執行的中間件,用來做一些返回數據后需要執行的任務等業務邏輯
下面以前置中間件為例:文件存放目錄 app/應用名/middleware/AuthMiddleware.php
<?php
namespase app\應用名\middleware;
use crmeb\interfaces\MiddlewareInterface;
class AuthMiddleware implements MiddlewareInterface
{
public function handle(Request $request, \Closure $next)
{
//這里可以設置請求header
//可以寫權限驗證
//驗證失敗直接可以拋出異常中止請求
if(false)
{
throw new AuthException('無權驗證');
}
return $next($request);
}
}
Controller
每個控制器負責相關業務的請求接收,只做最基本的數據接收,并調用相關的sevices業務處理層,返回數據。
例如:
<?php
namespace app\controller;
use app\Request;
use app\common\repositories\user\UserRepository;
class User
{
protected $repository;
public function __construct(App $app, repository $repository)
{
parent::__construct($app);
$this->repository = $repository;
}
public function lst($cid)
{
[$page, $limit] = $this->getPage();
$where = $this->request->params(['id']);
$data = $this->repository->search($where, $page, $limit);
return app('json')->success($data);
}
}
Repository
所有的業務都在Repository層中處理,Repository層調用dao層,【注意:每個獨立的services層只能調用對應的dao層,不能調用其他模型dao層。
比如:repository/user/UserRepository.php中只能調用dao/user/UserDao.php,無法調用 dao/order/StoreOrderDao.php。要想調用其他模型數據,
只能在UserServices.php中調用services/order/StoreOrderServices.php的方法來實現其他模型數據調用】。
例如:
<?php
namespace app\common\repositories\user;
use app\common\dao\user\UserDao;
class UserRepository extends BaseRepository
{
protected $dao;
public function __construct(UserDao $dao)
{
$this->dao = $dao;
}
public function search(array $where, int $page, int $limit)
{
$list = $this->dao->getList($where,'*',$page,$limit);
$count = $this->dao->count($where);
return compact('count','list');
}
}
Dao
dao層中主要用于當前模型基本的數據處理方法。dao層中調用對應的model,同樣無法跨層調用。
例如:
<?php
namespace app\dao\user;
use app\dao\BaseDao;
use app\model\user\User;
/**
* 用戶
* Class UserDao
* @package app\dao\user
*/
class UserDao extends BaseDao
{
protected function setModel(): string
{
return User::class;
}
public function getOne($uid)
{
return $this->where(['uid' => $uid])->field('username,phone')->find();
}
}
Model
model主要用于實例化數據表,只做相關數據表的基礎設置,搜索器,設置器及表關聯等操作。
<?php
namespace app\model\user;
use crmeb\basic\BaseModel;
use think\Model;
/**
* Class User
* @package app\model\user
*/
class User extends BaseModel
{
/**
* @var string
*/
protected $pk = 'uid';
protected $name = 'user';
protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
protected $hidden = [
'add_ip', 'account', 'clean_time', 'last_ip', 'pwd'
];
/**
* 自動轉類型
* @var string[]
*/
protected $type = [
'birthday' => 'int'
];
protected $updateTime = false;
/**
* 修改器
*/
protected function setAddTimeAttr($value)
{
return time();
}
/**
* 關聯訂單
* @return User|\think\model\relation\HasMany
*/
public function order()
{
return $this->hasMany(StoreOrder::class, 'uid', 'uid');
}
/**
/**
* 搜索器 用戶uid
* @param Model $query
* @param $value
*/
public function searchUidAttr($query, $value)
{
if (is_array($value))
$query->whereIn('uid', $value);
else
$query->where('uid', $value);
}
}
原文鏈接: https://blog.csdn.net/jiazi1024/article/details/124284742