TP5给request对象动态绑定属性

在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;

猜你喜欢

转载自www.cnblogs.com/lty-fly/p/11947418.html
tp5
今日推荐