流程
主要講解controller目錄,每個(gè)controller有自己獨(dú)立的路由,配置,事件,容器,控制器中可用框架核心及擴(kuò)展。每個(gè)控制器其實(shí)就是一個(gè)獨(dú)立的控制類。
請求流程大致分為以下流程,依次從左到右
Middleware
中間件分為前置和后置中間件.
前置中間件在訪問控制器之前會被執(zhí)行調(diào)用,通常用來攔截參數(shù),跨域配置,多語言加載,Session初始化,權(quán)限驗(yàn)證,登陸驗(yàn)證等處理;
而后置中間件屬于返回?cái)?shù)據(jù)后在執(zhí)行的中間件,用來做一些返回?cái)?shù)據(jù)后需要執(zhí)行的任務(wù)等業(yè)務(wù)邏輯
下面以前置中間件為例:文件存放目錄 app/應(yīng)用名/middleware/AuthMiddleware.php
<?php
namespase app\應(yīng)用名\middleware;
use crmeb\interfaces\MiddlewareInterface;
class AuthMiddleware implements MiddlewareInterface
{
public function handle(Request $request, \Closure $next)
{
//這里可以設(shè)置請求header
//可以寫權(quán)限驗(yàn)證
//驗(yàn)證失敗直接可以拋出異常中止請求
if(false)
{
throw new AuthException('無權(quán)驗(yàn)證');
}
return $next($request);
}
}
Controller
每個(gè)控制器負(fù)責(zé)相關(guān)業(yè)務(wù)的請求接收,只做最基本的數(shù)據(jù)接收,并調(diào)用相關(guān)的sevices業(yè)務(wù)處理層,返回?cái)?shù)據(jù)。
例如:
<?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
所有的業(yè)務(wù)都在Repository層中處理,Repository層調(diào)用dao層,【注意:每個(gè)獨(dú)立的services層只能調(diào)用對應(yīng)的dao層,不能調(diào)用其他模型dao層。
比如:repository/user/UserRepository.php中只能調(diào)用dao/user/UserDao.php,無法調(diào)用 dao/order/StoreOrderDao.php。要想調(diào)用其他模型數(shù)據(jù),
只能在UserServices.php中調(diào)用services/order/StoreOrderServices.php的方法來實(shí)現(xiàn)其他模型數(shù)據(jù)調(diào)用】。
例如:
<?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層中主要用于當(dāng)前模型基本的數(shù)據(jù)處理方法。dao層中調(diào)用對應(yīng)的model,同樣無法跨層調(diào)用。
例如:
<?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主要用于實(shí)例化數(shù)據(jù)表,只做相關(guān)數(shù)據(jù)表的基礎(chǔ)設(shè)置,搜索器,設(shè)置器及表關(guān)聯(lián)等操作。
<?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'
];
/**
* 自動轉(zhuǎn)類型
* @var string[]
*/
protected $type = [
'birthday' => 'int'
];
protected $updateTime = false;
/**
* 修改器
*/
protected function setAddTimeAttr($value)
{
return time();
}
/**
* 關(guān)聯(lián)訂單
* @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