Use thinkphp5.0 to get information, variables, binding parameters

Source: Internet
Author: User
1. Constructors:

The controller class must inherit the Thinkcontroller class in order to use:

Method _initialize

Code:

<?phpnamespace appliancontroller;use thinkcontroller;use thinkdb;use thinkrequest;class Index extends Controller{ Public   function _initialize ()    {        echo ' init| | | ';    }        Public Function Hello ()    {        return ' hello ';    }}

Look at the output:

2. Front-facing method:

[' except ' = ' = ' method name, method name ']:

Indicates that these methods do not use a predecessor method,

[' only ' and ' = ' method name, method name ']:

Indicates that only these methods use the predecessor method.

Split Line ************************************

The Beforeactionlist property can specify a method as a predecessor to another method;

execution before execution;

Code:

<?phpnamespace appliancontroller;use thinkcontroller;use thinkdb;use thinkrequest;class Index extends Controller{ protected $beforeActionList = [        ' first ',        ' second ' + =  [' except ' = ' hello '],        ' three '  =  [' Only ' = ' hello '], '    ;        protected function First ()    {        echo ' first<br/> ';    }        protected function Second ()    {        echo ' second<br/> ';    }        protected function Three ()    {        echo ' three<br/> ';    }    Public Function Hello ()    {        return ' hello ';    }        }

Look at the output:

Should only output hello, but because of the predecessor operation, output the three method;

Note: For this operation, the method name must be lowercase;

3. Get URL Information

<?phpnamespace appliancontroller;use thinkcontroller;use ThinkDb;use Thinkrequest;class Index extends Controller{public function index () {$request = Request::instance ();//get current domain name Echo ' Domain: '. $request->domain (). ' <br/> ';//Get the current entry file echo ' file: '. $request->basefile (). ' <br/> ';//Gets the current URL address without the domain name echo ' URL: '. $request->url (). ' <br/> ';//Gets the full URL address containing the domain name echo ' url with domain: '. $request->url (True). ' <br/> ';//get the current URL address without query_stringecho ' url without query: '. $request->baseurl (). ' <br/> ';//get the root address of the URL access echo ' root: '. $request->root (). ' <br/> ';//get the root address of the URL access echo ' Root with domain: '. $request->root (True). ' <br/> ';//Get the Path_info information in the URL address echo ' PathInfo: '. $request->pathinfo (). ' <br/> ';//Get the Path_info information in the URL address without the suffix echo ' pathinfo: '. $request->path (). ' <br/> ';//get the suffix information in the URL address echo ' ext: '. $request->ext ().  ' <br/> '; }}

4. Operating variables

Get param variable

The param variable is a variable acquisition method provided by the framework to automatically identify a get, post, or put request, and is the recommended way to get request parameters, using the following:

The request object can be used to complete the detection, acquisition, and security filtering of global input variables ~

Gets the name variable of the current request Request::instance ()->param (' name ');//Gets all the variables of the current request (filtered) request::instance ()->param ();// Gets all the variables of the current request (raw data) request::instance ()->param (false);//Gets all variables of the current request (including uploaded files) request::instance ()->param (True) ;
Gets the request variable request::instance ()->request (' id '); Get a Request variable request::instance ()->request (); Get all the Request variables (filtered) request::instance ()->request (false); Get all the request raw variable data

5. Binding parameters

The parameter binding method is bound by the variable name by default;

<?phppublic function Read ($id) {    return ' id = '. $id;    } Public Function archive ($year = ' ", $month = '") {    return ' year = '. $year. ' $month = '. $month;}

Enter URL:

http://localhost/index.php/lian/index/read/id/544

Output:

Parameters bound by variable name must match the name of the variable passed in in the URL, but the parameter order does not need to be consistent

If the above errors are reported, the reason for the error is simple, because the ID parameter must pass in the parameter when the read operation method is executed, but the method cannot get the correct ID parameter information from the URL address. Since we cannot trust any input from the user, it is recommended that you add a default value to the ID parameter of the Read method

6. Request Type

ThinkPHP5.0 Uniform uses the Thinkrequest class to handle request types.

Get request Type:

public function HQ () {//is a GET request if (Request::instance ()->isget ()) echo "is currently GET request ";//whether the POST request if (Request::instance ()->ispost ())" echo "is currently a POST request;//whether to ask for a PUT request if (Request::instance ()->is Put ()) echo "Currently put request",//whether the delete request if (Request::instance ()->isdelete ()) echo "is currently DELETE request",//whether the Ajax request if (Re Quest::instance ()->isajax ()) echo "Current Ajax request",//whether the Pjax request if (Request::instance ()->ispjax ()) echo "Current for Pjax request"; Whether to access if (Request::instance ()->ismobile ()) echo "Current phone access" for the phone,//whether to request if (Request::instance ()->ishead () for HEAD) echo "Current HEAD request",//whether the patch request if (Request::instance ()->ispatch ()) echo "Current Patch request", or whether the OPTIONS request if (Request: : Instance ()->isoptions ()) echo "Current OPTIONS request";//Whether it is Cliif (Request::instance ()->iscli ()) echo "currently CLI";//Is CG    IIf (Request::instance ()->iscgi ()) echo "Current CGI"; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.