1-----Create a controller
2-----Visit the view page
3-----Register variables to the view page
4-----Get form data
1. Create a controller under the Home module
Example: Create Test Controller.class.php in the directory tp/Application/ Home/ Controller r
Access the controller localhost/i/tp/Home/Test/ action method
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function test(){ echo "hello!"; } // Access the method hello
//http://localhost/i/tp/index.php/Home/Test/test }
Blog from "Wannian", address: http://www.cnblogs.com/wannian/p/8998141.html
Two access method display view page
--In the View folder under the Home module
--Create a new folder with the same name as the controller, and create a view page under this folder
E.g:
--If there is a Test Controller.class.php controller in the Home/Controller folder , and the controller has a ceshi method, that is, the ceshi() method in the TestController class under the Test controller
There is $this->show() in the --ceshi method ; access the view page corresponding to the method in the view folder corresponding to the controller
--Then create the Test folder under the Home/View folder and create the view page ceshi.html
--The way to access this page is: localhost/i/tp/index.php/Home/Test/ ceshi() method under TestController class of Test( TestController.class,php ) controller under i Home module
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function ceshi(){ $this->show(); } }
Three register variables to the view page
①-- Method in TP framework:
-- Register variables in the controller method $this->assign("variable name", "variable value");
Such as: $this ->assign( 'uname' , ' Zhang San ' );
-- View page access method {$ variable name} Write $ symbol and variable name in curly brackets, no spaces, line breaks, etc.
For example: {$uname}
②-- If the registered variable is an array, get method: {$arr['0']} or {$arr['id']}
③--cycle output
-- <foreach> tag , TP-specific loop output
-- The value of name is the variable name (the same as the variable name in the method), the value of item is the value of the obtained array (custom naming)
-- register an array
public function test(){ $arr=array( array("code"=>"n001","name"=>"汉族"), array("code"=>"n002","name"=>"天族") ); $this->assign("nation",$arr); $this->show(); }
-- Front-end acquisition
<select> <foreach name="nation" item="v"> <option value='{$v.code}'>{$v.name}</option> </foreach> </select>
④--if judgment
-- <if> tag and else tag , TP-specific front-end judgment
-- The value of the condition attribute in the <if> tag is the judgment condition ,
--The variable name in the judgment condition needs to be prefixed with $ , for example, a registered variable name is id and the value is 0, then the judgment id should be condition=" $id==0 "
For example, register the variable in the test method $this->assign("dc",0);
The front-end page judges and outputs, because dc is not equal to 1, the page displays Li Si
<if condition="$dc==1"> <b>张三</b> <else /> <i>李四</i> </if>
⑤-- The <literal> tag is displayed as it is , and the <if> tag <foreach> tag is written in the tag and does not execute judgment and loop
Four get form data
①-- Get the path of the method through the get_defined_constants(true); method and return a two-dimensional associative array
--path is in 'user' so var_dump(get_defined_constants(true)['user']);
-- __ROOT__ root path of TP framework
-- __SELF__ path to own method
-- __APP__ entry directory
-- __MODULE__ current module path
-- __CONTROLLER__ to the controller's directory
-- __ACTION__ to the action method directory
②-- The front end creates the view page sub.html and writes it into the form form, and the submission address is the operation method directory __ACTION__ of this page
<form action="__ACTION__" method="post"> <input type="text" name="uid"> <input type="password" name="pwd"> <input type="submit" value="登录"> </form>
③-- Some sub methods in the Test controller get data
public function sub () { if ( empty ( $_POST )){ $this -> show(); // If the POST is empty, continue to display the submission page } else { echo $_POST ['uid' ]; // If there is a POST, operate on it, Or output or store in database, etc. } var_dump ( get_defined_constants ( true )['user' ]); }