Blogger Information
Blog 61
fans 0
comment 0
visits 62941
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
从控制器中调用Request类的四种方法
Pengsir
Original
1105 people have browsed it

从控制器中调用Ruquest类的四种方法:

* 1.传统的new Request
* 2.静态代理:think\facade\Request
* 3.依赖注入:Request $request
* 4.父类Controller中的属性$request :$this->request

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/21 0021
 * Time: 23:42
 */

namespace app\index\controller;

use think\Request;
use think\Controller;
class Tast extends Controller
{
    //方法一:传统的new Request方法
    public function test1()
    {
        $request = new Request();
        dump($request->get());
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/21 0021
 * Time: 23:42
 */

namespace app\index\controller;
use think\facade\Request;//导入请求对象的静态代理

class Tast
{

    public function test2()
    {
        //用静态代理:think\facade\Request
        dump( Request::get());
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/21 0021
 * Time: 23:42
 */

namespace app\index\controller;

use think\Request;

class Tast
{
        //用依赖注入
    public function test3(Request $request)
    {
        dump($request->get());
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/21 0021
 * Time: 23:42
 */

namespace app\index\controller;

use think\Controller;//导入think命名空间的Controller类
use think\Request;//导入think命名空间的Request类

class Tast extends Controller
{
//直接调用父类Controller中的属性:$request : $this->request
    public function test4()
    {
        dump($this->request->get());
    }
}

运行效果图:

从控制器调用Ruquest类.png

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