1-----Create a Controller
2-----Access View page
3-----Register variable to view page
4-----Get form data
Create a controller under the home module
Example: Table of Contents tp/application/home/controller Create TestController.class.php
Access the controller localhost/i/tp/home/test/how to do it
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction Test () { echo "hello!" ; } // access the method hello
Http://localhost/i/tp/index.php/Home/Test/test}
Blog from "Million", Address:http://www.cnblogs.com/wannian/p/8998141.html
Two-way access method Display View page
--In the View folder under the Home module
--Create A view page in this folder just like the controller name in the new folder
For example:
--if there is a TestController.class.php Controller under the Home/controller folder, and the controller has a Ceshi method, The Ceshi () method in the TestController class under the test controller
The--ceshi method has $this->show (); Access the view page for this method corresponding to the view folder for the controller
--Then create the T-est folder under the home/view folder and create the View page ceshi.html
--This page is accessed by: localhost/i/tp/index.php/home/test/ceshi Home module Test (testcontroller.class,php) Ceshi () method under the TestController class of the Controller
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction Ceshi () { $thisShow (); }}
Three registered variables to the view page
①--method in TP frame:
--The Controller's method registers the variable $this->assign ("Variable name", "Variable Value");
such as: $this->assign (' uname ',' Zhang San ');
--View page access method {$ variable name} curly braces inside write $ symbol with variable name, cannot have space line wrapping etc.
such as: {$uname}
②--If the registered variable is an array, get the method: {$arr [' 0 ']} or {$arr [' id ']}
③--Loop Output
--<foreach> label , TP-specific cyclic output
--The value ofname is the variable name (consistent with the variable name in the method), and the value ofitem is the value of the obtained array (custom named)
--Register an array
Public function Test () { $arr=array ("Code" = "n001", "name" and "=" Han "), array(" Code "=" n002 "," name "=" Heavenly Clan ") ); $this->assign ("Nation",$arr); $this-Show (); }
--Front-end access
<Select> <foreachname= "Nation"Item= "V"> <optionvalue= ' {$v. code} '>{$v. Name}</option> </foreach> </Select>
④--if judgment
--<if> tags and else tags , TP-specific front-end judgment
The value of the condition attribute in the--<if> tag is the judging condition ,
--The variable name in the judging condition must have the $ character prefix, for example a registered variable name is an ID, the value is 0, then the ID should be condition= "$id ==0"
For example, registering variables in the test method $this->assign ("DC", 0);
The front page is judged and output because the DC is not equal to 1 so the page displays John Doe
< if condition = "$DC ==1" > < b > Zhang San </ b > < else /> < i > John Doe </ i > </ if >
⑤-- <literal> tags are displayed as-is,<if> tags <foreach> labels are not executed in this tag. Judgment and Loop
Four Get form data
①--by Get_defined_constants (true); method gets the path of the method and returns a two-dimensional associative array
--The path is in ' user ' so Var_dump (Get_defined_constants (true) [' user ']);
--Root path of __root__ TP frame
--The path of __self__ 's own method
--__app__ Entry directory
--__module__ Current module path
--__CONTROLLER__ to the Controller directory
--__action__ to the directory of Operation Methods
②--Front-End CREATE view page sub.html and write to form form, submit address to This page action method directory __action__
<formAction= "__action__"Method= "POST"> <inputtype= "text"name= "UID"> <inputtype= "Password"name= "pwd"> <inputtype= "Submit"value= "Login"></form>
③--some sub methods in the test controller to get the data
Public functionSub () {if(Empty($_post)){ $this-Show (); //if post is empty, continue to show submission page}Else{ Echo $_post[' UID ']; //If there is a post then operate on it, or output or deposit the database, etc. } Var_dump(get_defined_constants(true) [' User ']); }
tinkphp Framework Learning-02 Controller basic operation