博主信息
博文 29
粉丝 0
评论 0
访问量 33215
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
从控制器中调用Request类的四种方法
LIWEN的博客
原创
1939人浏览过

四种方法从控制器中调用Request类:
* 1、传统的new Request
* 2、静态代理:think\facade\Request
* 3、依赖注入:Request $request
* 4、直接调用父类Controller中的属性:$request : $this->request

类名:Demo1

位置:application/index/controller/Demo1.php

URL:http://www.tp5.com/index.php/index/Demo1/test4?name=peter&sex=male

运行结果均为下图:

2018-01-20_091550.png

代码分别如下:

<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Demo1 extends Controller   //方法1和4中用到extends Controller,use think\Request;use think\Controller;
{
    //方法一:传统的new Request方法
    public function test1()
    {
        $request = new Request();
        dump($request->get());
    }
}
<?php
namespace app\index\controller;
use think\facade\Request;  //导入请求对象的静态代理,用于方法二
class Demo1
{
   //方法二:静态代理:think\facade\Request
    public function test2()
    {
        dump(Request::get());
    }
}
<?php
namespace app\index\controller;
class Demo1
{
    //方法三:依赖注入:Request $request
    public function test3(\think\Request $request)
    {
        dump($request->get());
    }
}
<?php
namespace app\index\controller;
use think\Request;  //导入think命名空间的Request类
use think\Controller;  //导入think命名空间的Controller类
class Demo1 extends Controller   //继承Controller类
{
    //方法四:直接调用父类Controller中的属性:$request : $this->request
    public function test4()
{
    dump($this->request->get());
}
}


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学