在构造函数中注入user函数,userInfo函数须写在common.php中
<?php
namespace app\index\controller;
use think\Controller;
use \think\Request;
class RequestTest extends Controller{
public function _initialize()
{
//方法注入
Request::hook("user", "userInfo");
}
public function zhuru()
{
dump(Request::instance()->user(1));
}
}
在common.php代码(注意引入Request的命名空间):
<?php
use \think\Request;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
//方法注入的函数
function userInfo(Request $request, $userId)
{
return "I am id is ".$userId;
}