ThinkPHP — how the controller allocates variables to the public template

Time:2021-1-23

There should be many solutions, and I will provide an idea here

Define a common controller base from which other controllers inherit

Assign variables in the initialize method of the base controller. The syntax of tp6 is as follows:

<?php namespace app\qingadmin\controller;

use app\BaseController;use think\facade\View;class Base extends BaseController
{
    public function initialize(){
      $loginAdmin=session('adminSessionData');
      View::assign('loginUser',$loginAdmin['user_name']);

    }
}

Public template can be used directly