Examples of four calling modes of Request class in thinkPHP 5.1 framework

  • 2021-12-13 07:47:44
  • OfStack

In this paper, four calling modes of Request class in thinkPHP 5.1 framework are described by examples. Share it for your reference, as follows:

1. Traditional invocation

Access method: http://127.0.0.1/demo/demo3/test? name = kk & age=22


<?php
/**
 * Created by PhpStorm.
 * User: 10475
 * Date: 2018/8/27
 * Time: 22:59
 */
namespace app\demo\controller;
use think\Request;
class Demo3
{
  public function test()
  {
    $request = new Request();
    dump($request->get());
  }
}

2. Static invocation

Request has built-in static proxy classes in THINKPHP 5.1, which can be used directly

Access method http://127.0.0.1/demo/demo3/test? name = kk & age=22 & sex=male


<?php
/**
 * Created by PhpStorm.
 * User: 10475
 * Date: 2018/8/27
 * Time: 22:59
 */
namespace app\demo\controller;
use think\Facade\Request;
class Demo3
{
  public function test()
  {
    dump(Request::get());
  }
}

3. Dependency injection, or type constraint

Access method http://127.0.0.1/demo/demo3/test? name = kk & age=22 & sex=male


<?php
/**
 * Created by PhpStorm.
 * User: 10475
 * Date: 2018/8/27
 * Time: 22:59
 */
namespace app\demo\controller;
use think\Request;
class Demo3
{
  public function test(Request $request)
  {
    dump($request->get());
  }
}

4. request attribute in Controller class


<?php
/**
 * Created by PhpStorm.
 * User: 10475
 * Date: 2018/8/27
 * Time: 22:59
 */
namespace app\demo\controller;
class Demo3 extends \think\Controller
{
   public function test()
   {
     dump($this->request->get());
   }
}

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on the framework of ThinkPHP PHP programming help.


Related articles: