Use of Request Request Objects and Common Operations in ThinkPHP 5.0

Keywords: Session

Use of request

First method

Add request reference in the controller header

Then call the'instance'class in the method

Then call the method:

  public function index($name='name')
    {
        $request = Request::instance();

        echo 'Request method:'.$request->method() . '<br/>';

        echo 'Resource type:'.$request->type() . '<br/>';

        echo 'Visit ip: '.$request->ip() . '<br/>';

        echo 'Is it ajax Request:'.var_export($request->isAjax(), true) . '<br/>';

        echo 'Request parameters:';
        dump($request->param());

        echo 'Request parameters: Include only name';
        dump($request->only(['name']));

        echo 'Request Parameters: Exclusion name';
        dump($request->except(['name']));

        echo 'Resource type:'.$request->type() . '<br/>';
        echo '<br/>Operation:'.$request->action();
        echo 'Get the current domain name:'.$request->domain() . '<br/>';

        // Get the current entry file
        echo 'Get the current entry file:'.$request->baseFile() . '<br/>';

        // Get the current URL address without domain name
        echo 'Get Current URL Address, without domain name:'.$request->url() . '<br/>';

        // Get the full url address containing the domain name
        echo 'Get the complete containing domain name url Address:'.$request->url(true) . '<br/>';

        // Get URL address without QUERY_STRING
        echo 'Obtain URL Address does not contain QUERY_STRING: '.$request->baseurl() . '<br/>';

        // Get the ROOT address of URL access
        echo 'Obtain URL Accessed ROOT Address:'.$request->root() . '<br/>';

        // Get the ROOT address of URL access
        echo 'Obtain URL Accessed ROOT Address:'.$request->root(true) . '<br/>';

        // Get PATH_INFO information in the URL address
        echo 'Obtain URL In address PATH_INFO Information:'.$request->pathinfo() . '<br/>';

        // Gets the PATH_INFO information in the URL address, without the suffix
        echo 'Obtain URL In address PATH_INFO Information, without suffix:'.$request->path() . '<br/>';

        // Get suffix information in URL address
        echo 'Obtain URL Suffix information in address:'.$request->ext() . '<br/>';
      Session::set('name','thinkphp');
      Cookie::set('name','thinkphp2');
        dump($request->route());
        dump($request->dispatch());
        echo Session::get('name');


    }

The result is:

There are still some screenshots because the screen is not big enough, but you just need to know how to use them.

Second method

This method is simple but slightly cumbersome, as long as it references the Controller class and then inherits it, it can be called without first referencing request as above and then calling $request = Request::instance() in the method; then it can be used.

Referencing Controller is the same as referencing request above
use think\Controller

Then inherit the controller from the controller

Last call to'request'
Is it a little more trouble than the method?Each call needs to be preceded by $this->request

The overall code is:

    public function test(){
// Get the current domain name
        echo 'domain: ' . $this->request->domain() . '<br/>';
// Get the current entry file
        echo 'file: ' . $this->request->baseFile() . '<br/>';
// Get the current URL address without a domain name
        echo 'url: ' . $this->request->url() . '<br/>';
// Get the full URL address containing the domain name
        echo 'url with domain: ' . $this->request->url(true) . '<br/>';
// Get the current URL address without QUERY_STRING
        echo 'url without query: ' . $this->request->baseUrl() . '<br/>';
// Get the ROOT address of URL access
        echo 'root:' . $this->request->root() . '<br/>';
// Get the ROOT address of URL access
        echo 'root with domain: ' . $this->request->root(true) . '<br/>';
// Get PATH_INFO information in the URL address
        echo 'pathinfo: ' . $this->request->pathinfo() . '<br/>';
// Get PATH_INFO information from URL address without suffix
        echo 'pathinfo: ' . $this->request->path() . '<br/>';
// Get suffix information in URL address
        echo 'ext: ' . $this->request->ext() . '<br/>';

        echo "The current module name is" . $this->request->module();
        echo "The current controller name is" . $this->request->controller();
        echo "The current operation name is" . $this->request->action();
        echo 'Request method:' . $this->request->method() . '<br/>';
        echo 'Resource type:' . $this->request->type() . '<br/>';
        echo 'Visit ip Address:' . $this->request->ip() . '<br/>';
        echo 'Whether AJax Request:' . var_export($this->request->isAjax(), true) . '<br/>';
        echo 'Request parameters:';
        dump($this->request->param());
        echo 'Request parameters: Include only name';
        dump($this->request->only(['name']));
        echo 'Request Parameters: Exclusion name';
        dump($this->request->except(['name']));
    }

Final result graph:

Refer to the documentation for more information

Posted by LordPsyan on Fri, 01 May 2020 14:02:49 -0700