Index 控制器
<?php
namespace app\index\controller;
class Index extends \think\Controller
{
public function index()
{
return '正在学习中...';
}
public function demo1()
{
return $this->request->site_Name;
}
public function demo2()
{
return $this->request->getSiteName();
}
}common 文件 公共 方法
<?php
//跨控制器访问
// 应用公共文件
use think\Request;
$request = Request::instance();
//请求对象属性注入
$request->site_Name = 'php中文网';
//请求对象方法注入
function getSiteName(Request $request) //第一个参数必须是Request
{
return '站点名称:'.$request->site_Name;
}
//注册请求对象的方法,也叫钩子
Request::hook('getSiteName','getSiteName');夸模块调用
只需要新建: test/controller/Demo.php //模块/控制器/控制器文件 内容同: Index 控制器内容 访问需要需改为: www.tp5.cpm/test/demo/demo1
返回结果:php中文网
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号