猎模筋胜钵栓汹茶枷朗谐蹲圣
将参数绑定关系改为按照名称绑定
<?php return [ // URL参数方式 0 按名称成对解析 1 按顺序解析 'url_param_type' => 0, ];
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; //先继承基类 class Index extends \think\Controller { public function index() { return "萌面人资料铺"; } public function demo1($name) { return $name; } public function demo2() { return $name; } }
访问demo1()
访问demo2()
使用:$this->request->param(‘name’);接收请求变量
<?php namespace app\index\controller; //先继承基类 class Index extends \think\Controller { public function index() { return "萌面人资料铺"; } public function demo1($name) { return $this->request->param('name'); } public function demo2() { return $this->request->param('name'); } }
执行:
上方即实现了跨操作的数据共享
跨控制器访问变量
创建Demo控制器
<?php namespace app\index\controller; //先继承基类 class Demo extends \think\Controller { public function hello() { //试图访问Index中的name变量 if($this->request->has('name','get')) { return $this->request->param('name'); } else { return "不存在"; } } }
执行:
由上可以知道请求变量不能跨控制器访问
给请求对象注入自定义属性和方法,实现跨控制器甚至跨模块数据共享:
1.在公共文件中:
<?php use think\Request; $request=Request::instance(); //请求对象的属性注入 $request->siteName="萌面人资料铺"; //请求对象的方法注入 //1.创建绑定函数 function getSiteName(Request $request)//第一个参数必须是Request类型的变量 { return "站点名称:".$request->siteName; } //2.注册请求对象的方法,也叫钩子 //第一个参数是要注入到当前请求对象的方法名称 //第二个参数是要绑定的函数 //说明:第一个参数可以为其他的名称 Request::hook('getSiteName','getSiteName');
<?php namespace app\index\controller; //先继承基类 class Index extends \think\Controller { public function index() { return "萌面人资料铺"; } public function demo1() { return $this->request->siteName; } public function demo2() { return $this->request->getSiteName(); } }
执行:
<?php namespace app\index\controller; //先继承基类 class Demo extends \think\Controller { public function demo1() { return $this->request->siteName; } public function demo2() { return $this->request->getSiteName(); } }
执行:
当前即实现了跨控制器访问
创建test模块,并创建Demo控制器
<?php namespace app\test\controller; //先继承基类 class Demo extends \think\Controller { public function demo1() { return $this->request->siteName; } public function demo2() { return $this->request->getSiteName(); } }
执行:
此时实现了跨模块共享数据
尝蹿虫堵口叛握脑付破贝童寒