Blogger Information
Blog 14
fans 0
comment 0
visits 16080
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
请求对象属性注入与方法注入
依然很m丶的博客
Original
1349 people have browsed it

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中文网

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post