thinkPHP parameters passed

Binding method of operating parameter functions, can automatically obtain the URL parameters, still above controller, for example, the controller code is as follows:

<?php

namespace app\index\controller;

class Index
{ public function index() { return 'index'; } public function hello($name = 'World') { return 'Hello,' . $name . '!'; } } 

When we visit

http://tp5.com/index.php/index/index/hello 

That is, access app\index\controller\Indexcontroller class hellomethod because no arguments are passed, nameparameter default value is used World. If passed in the name parameter is used:

http://tp5.com/index.php/index/index/hello/name/thinkphp 

Page output is:

Hello,thinkphp!

Now hello method to increase the second parameter:

    public function hello($name = 'World', $city = '') { return 'Hello,' . $name . '! You come from ' . $city . '.'; } 

Access address

http://tp5.com/index.php/index/index/hello/name/thinkphp/city/shanghai 

Page output is:

Hello,thinkphp! You come from shanghai.

Can be seen, hellothe method automatically gets the same name as the parameter values in the method of the URL address, and the URL is not affected by the order of the parameters passed in the order parameter, for example the following URL address and outputs the result is the same as above:

http://tp5.com/index.php/index/index/hello/city/shanghai/name/thinkphp 

Or use

http://tp5.com/index.php/index/index/hello?city=shanghai&name=thinkphp 

Can do to further simplify the URL address, the premise is that we must be clear order of the arguments on behalf of the variables, we get to change the way of the URL parameter, the application configuration file in the url_param_typevalue of the parameter is modified as follows:

// 按照参数顺序获取
'url_param_type' => 1, 

Now, the URL parameter passed by value becomes strict accordance with the method of operating variables define the order to pass a value, which means that we must use the following URL address to access the correct incoming nameand cityparameters to the hellomethod:

http://tp5.com/index.php/index/index/hello/thinkphp/shanghai 

Page output is:

Hello,thinkphp! You come from shanghai.

If the order is changed parameters

http://tp5.com/index.php/index/index/hello/shanghai/thinkphp 

Page output is:

Hello,shanghai! You come from thinkphp.

Obviously we are not the intended result.

Similarly, we tried

http://tp5.com/index.php/index/index/hello/name/thinkphp/city/shanghai 

Access will not get the right results.

note


In order to bind argument, the method of operation parameters can only use the URL pathinfo variable, but can not use the get or post variable.

 

 

 

 

Guess you like

Origin www.cnblogs.com/xu1115/p/10977611.html