在tp5中可以给Request请求对象绑定属性,方便全局调用。比如我们可以在公共控制器中绑定当前登录的用户模型到请求对象。
1. 首先在公共控制器中绑定:
<?php
namespace app/common/controller;
use app/index/model/User;
use think/Controller;
use think/Request;
use think/Session;
class Base extends Controller
{
public function _initialize(){
$user = User::get(Session::get("user_id"));
Request::instance()->bind("user",$user);
//$this->request->bind("user",$user);
}
}
2. 获取该属性(在控制器中):
Request::instance()->user;
//$this->request->user;
//request()->user;
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/2264.html