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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2021-09-14
  • 2022-12-23
猜你喜欢
  • 2021-07-25
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-12-13
相关资源
相似解决方案