TP5 dynamic binding property to the request object

In tp5 can request to the Request object binding properties to facilitate the global call . For example, we can bind the currently logged in user model common controller to the request object.

1. First Binding in public controllers:

<?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. Obtain the property (controller):

Request::instance()->user;
//$this->request->user;
//request()->user;

Guess you like

Origin www.cnblogs.com/lty-fly/p/11947418.html