1, the following is a typical definition of the Controller class:
<? PHP namespace Home\controller;use Think\controller; class Indexcontroller extends Controller { public function Hello () { ' hello,thinkphp! ' ; }}? >
When accessed http://serverName/index.php/Home/Index/hello , it outputs:
hello,thinkphp!
2, the action parameter binding. The parameter binding feature is turned on by default, and the principle is to bind the parameters in the URL and the parameters in the action method. There are two ways of parameter binding: Binding by variable name and binding in order of variable.
(1) The default parameter binding method is bound according to the variable name, for example, we give the blog controller to define a method of operation archive method:
namespace Home\controller;use Think\controller; class BlogController extends controller{ public Function archive ($year =' ', $month ='year=') { '$ '. $ Year. ' &month= ' . $month; }}
The URL's access address is:
http://SERVERNAME/INDEX.PHP/HOME/BLOG/ARCHIVE/YEAR/2013/MONTH/11
Parameters bound by variable name must match the name of the variable passed in in the URL, but the order of the parameters does not need to be consistent.
The parameter bindings are still valid if accessed using the following URL address:
http://servername/index.php/home/blog/archive/month/11/year/2013http:// SERVERNAME/INDEX.PHP?S=/HOME/BLOG/ARCHIVE/YEAR/2013/MONTH/11http://servername/index.php?c= Blog&a=archive&year=2013&month=11
(2) Bind by variable order. In this case, the order of the parameters in the URL address is very important and cannot be arbitrarily adjusted. To bind in the order of variables, you must first set URL_PARAMS_BIND_TYPE to 1:
' Url_params_bind_type ' + 1// Set parameter bindings bind in variable order
The definition of the operation method does not need to change, the URL's access address is changed to:
http://SERVERNAME/INDEX.PHP/HOME/BLOG/ARCHIVE/2013/11
If the action method definition has no parameters, you can turn off the parameter binding feature:
' Url_params_bind ' = false
3, url generation. To match the URL pattern used, the corresponding URL address needs to be generated based on the current URL settings, and for this reason, the thinkphp built-in provides a U method for dynamic URL generation, ensuring that the project is not affected by the environment during the migration process. The definition rules for the U method are as follows:
U (' address expression ', [' parameter '],[' pseudo static suffix '],[ ' Show domain name '])
The format of address expressions, parameters, pseudo-static suffixes, please refer to the documentation: Http://document.thinkphp.cn/manual_3_2.html#url_build
Depending on the URL settings of the project, the same U method call can intelligently correspond to different URL address effects, for example:
U ('blog/read?id=1');
If the current URL is set to normal mode, the last generated URL address is:
http://servername/index.php?m=blog&a=read&id=1
If the current URL is set to PathInfo mode, the last URL generated by the same method is:
http://SERVERNAME/INDEX.PHP/HOME/BLOG/READ/ID/1
If the current URL is set to rewrite mode, the last URL generated by the same method is:
http://SERVERNAME/HOME/BLOG/READ/ID/1
If the current URL is set to rewrite mode, and the pseudo-static suffix is set to. html, the same way the last generated URL address is:
http://servername/home/blog/read/id/1.html
For a description of the URL pattern, refer to the documentation: Http://document.thinkphp.cn/manual_3_2.html#url
3. Get the variable. Although you can still use the traditional way to obtain various system variables during the development process, for example:
$id = $_get['ID'];//get a Get variable$name = $_post['name'];//Get post Variables$value = $_session['var'];//Get Session variable$name = $_cookie['name'];//Get Cookie Variables$file = $_server['php_self'];//Get Server Variables
However, we do not recommend the direct use of traditional methods to obtain, because there is no unified security processing mechanism, later if the adjustment, the change will be more troublesome. Therefore, a better approach is to use the I function uniformly in the framework for variable acquisition and filtering. The I method is thinkphp for more convenient and secure access to the system input variables, which can be used anywhere, we take the Get variable type as an example to illustrate the use of the next I method:
echo I ('get.id'// = $_get[' id ')echo i ('get.name ' // equivalent to $_get[' name ')
thinkphp Development Note-Controller