thinkphp5.0 study notes (iii) get information, variables, binding parameters

Source: Internet
Author: User

1. constructors:

The controller class must inherit the \think\Controller class in order to Use:

Method_initialize

Code:

 <? phpnamespace app\lian\controller;  use   think\controller;  use   think\db;  use   think\request;  class  Index extends   controller{ public  function   _ Initialize () { echo  ' init| | | '    ;  public  function   Hello () { return  ' Hello ' ; }}

Address: Http://localhost/index.php/lian/index/hello

Look at the Output:

2. front-facing method:
[‘except‘ => ‘方法名,方法名‘]:

Indicates that these methods do not use a predecessor method,

[‘only‘ => ‘方法名,方法名‘]:

Indicates that only these methods use the predecessor Method.

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

beforeActionListProperty can specify a method as a predecessor to another method;

Execution before execution;

Code:

<?phpnamespace app\lian\controller; usethink\controller; usethink\db; usethink\request;classIndexextendscontroller{protected $beforeActionList= [        ' First ', ' second ' = [' except ' + ' Hello '], ' three ' = [' only ' = ' Hello '],    ]; protected functionfirst () {Echo' First<br/> '; }        protected functionSecond () {Echo' Second<br/> '; }        protected functionthree () {Echo' Three<br/> '; }     public functionHello () {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 app\lian\controller; usethink\controller; usethink\db; usethink\request;classIndexextendscontroller{ public functionindex () {$request= Request::instance ();//get current domain nameEcho' Domain: '.$request->domain (). ' <br/> ';//get current Portal fileEcho' File: '.$request->basefile (). ' <br/> ';//get current URL address without domain nameEcho' Url: '.$request->url (). ' <br/> ';//get the full URL address that contains the domain nameEcho' URL with domain: '.$request->url (true) . ' <br/> ';//get current URL address does not contain query_stringEcho' URL without query: '.$request->baseurl (). ' <br/> ';//gets the root address of the URL accessEcho' Root: '.$request->root (). ' <br/> ';//gets the root address of the URL accessEcho' Root with domain: '.$request->root (true) . ' <br/> ';//get the Path_info information in the URL addressEcho' Pathinfo: '.$request-PathInfo() . ' <br/> ';//get path_info information in URL address without suffixEcho' Pathinfo: '.$request->path (). ' <br/> ';//gets the suffix information in the URL addressEcho' Ext: '.$request->ext (). ' <br/> '; }}
4. Operating Variables

Get PARAM variable

The param variable is a method that the framework provides for automatic recognition GET POST or PUT request, and is the recommended way to get request parameters, using the Following:

You can Request complete the detection, acquisition, and security filtering of global input variables through objects ~

// gets the name variable for 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); // get all variables for the current request (including upload files) Request::instance ()->param (true);
// get the request variable // get a request variable // 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;

<? PHP  public 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 unifies the think\Request class processing request Type.

GET Request Type:

 public functionHQ () {//is a GET requestif(request::instance ()->isget ())Echo"currently a GET request";//is a POST requestif(request::instance ()->ispost ())Echo"currently a POST request";//is a PUT requestif(request::instance ()->isput ())Echo"current PUT request";//whether the DELETE requestif(request::instance ()->isdelete ())Echo"current DELETE request";//is the Ajax requestif(request::instance ()->isajax ())Echo"currently for Ajax requests";//whether to request for Pjaxif(request::instance ()->ispjax ())Echo"current Request for pjax";//whether to access the phoneif(request::instance ()->ismobile ())Echo"current Phone access";//whether to request for HEADif(request::instance ()->ishead ())Echo"current HEAD request";//is a Patch requestif(request::instance ()->ispatch ())Echo"current PATCH request";//whether the OPTIONS requestif(request::instance ()->isoptions ())Echo"current OPTIONS request";//is the CLIif(request::instance ()->iscli ())Echo"current cli";//whether it is a CGIif(request::instance ()->iscgi ())Echo"current cgi"; }

thinkphp5.0 study notes (iii) get information, variables, binding parameters

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.