In this section, the controller is explained in detail, taking the JIAOWU application catalogue as an example.
1. How to write the controller, how to write the operation method?
Create a new controller file under the module Controller directory controllers MainController.class.php, write
<?phpnamespace Home\controller; Define a virtual namespace under this folder use Think\controller; Call the parent class Maincontroller extends Controller {//Inherit the parent class, define a subclass public function Main () // The main action under Maincontroller, put in the controller inside the method {echo "page display"; }}
2. How do I get the configuration and modify the configuration in the Operation method?
<?phpnamespace home\controller;use think\controller;class Maincontroller extends controller{public function Xianshi () {//Get configuration C (' config name ');//echo c (' Url_model '); ' Url_model ' configuration for routing mode//modify config C (' config name ', config value);//c (' Url_model ', 0);//echo U ("Xianshi");//u ("Function name in Controller") Get Path}}
3. How to implement pre-operation and post-operation?
<?phpnamespace home\controller;use think\controller;class Maincontroller extends controller{//pre-operation public function _before_xianshi () {echo "before performing display";} Public Function Xianshi () {echo "page display";} Post operation Public Function _after_xianshi () {echo "after execution display";}}
4. Register the variable in the TP frame and display it in the template interface?
First, in the controller Maincontroller, the main class writes an operation method (function) Ceshi, and registers the variable with the TP framework in the operation method.
<?phpnamespace home\controller;use think\controller;class Maincontroller extends controller{public function Ceshi () {$this->assign ("test", "Hello"); Register variable $this->display () in the TP frame;}}
Finally, create a new folder like the class name in the View Module catalog view folder Main, and create a new HTML file in its folder with the same name as the action method ceshi.html
The think template "<{,}>" tag is the same as the Smarty template, because we are configuring it in the configuration file config.php
<?phpreturn Array (//' config item ' = ' config value '///If you need to do your own special configuration, simply copy the contents of the thinkphp/conf/convention.php and modify the configuration. ' Tmpl_l_delim ' = ' <{', //Template engine plain label start tag ' Tmpl_r_delim ' + ' }> ', // Template engine normal tag end tag);
5. Use the Get_defined_constant (true) method to get the path of the user in the information
' __module__ ' = String '/tp/index.php/home '
' __controller__ ' = String '/tp/index.php/home/main '
' __action__ ' = String '/tp/index.php/home/main/ceshi '
6. Pass parameters in the Operation method
First, write a function with parameters
<?phpnamespace home\controller;use think\controller;class Maincontroller extends Controller{public function Xianshi ($name, $ids) {echo $name. " The main page displays ". $ids; Pass parameters, write arguments in the function name brackets}}
Enter http://localhost/tp/home/main/xianshi/$name/zhangsan/$ids in the browser/2564
Route/Parameter name/value of parameters
7.post Value (example: page login, display logic)
First, in the controller Maincontroller, the main class writes an operation method (function) Login,
<?phpnamespace home\controller;use think\controller;class MainController Extends controller{public Function Login () {
Implement two logic, display page, implement login
if (empty ($_post))
{
$this->display ();
}
Else
{
Implement Login
echo "Login Successful";
}
}}
Finally, create a new folder like the class name in the View Module catalog view folder Main, and create a new login.html in its folder like the action method name
thinkphp Explanation (ii) controller