控制器的一些方法:
Maincontroller.class.php文件:
<?php namespace HomeController; use ThinkController; class MainController extends Controller{ function _before_xianshi(){ echo "显示操作的前置<br>"; } function Xianshi(){ //echo $_GET["sex"];//实质是通过get方式传值 //$this->assign("ceshi",$name); //$this->assign("test","hello"); $this->display(); //echo "asdadhjk"; //$this->display(); } function _after_xianshi(){ echo "显示操作的后置"; } function ceshi($bb){ echo $bb,$_POST["bb"];//刷新页面是进的当前方法,注意"bb"(名字必须一致) } function shengcheng(){ //$path="/Thinkphp/index.php/home/main/xianshi"//这样写路径,1是太麻烦2是后期维护的时候不好进行 echo U("xianshi")."<br>";//自动生成路径,模式跟配置有关; echo U("index/index");//不同控制器下的不同方法 echo U("Admin/index/index");//不同模块下的路径 } function changliang(){ var_dump(get_defined_constants(true));//获取常量 } function Denglu(){ //一个方法实现两个逻辑 //显示登录,实现逻辑 if(empty($_GET)) { $this->display(); } else { echo "登录成功或失败"; } } function Peizhi(){ C("URL_MODLE",1); echo U("peizhi"); } }
xianshi.html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action="__CONTROLLER__/ceshi" method="post"> <input type="text" name="bb"/> <input type="submit" value="提交" /> </form> </body> </html>
denglu.html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action="__ACTION__" method="get"> 用户名:<input type="text" name="uid"/> 密码:<input type="text" name="pwd" /> <input type="submit" value="登录" /> </form> </body> </html>
function changliang(){ var_dump(get_defined_constants(true));//获取常量 }
重要的几个常量: '__ROOT__' => string '/Thinkphp' //根 '__INFO__' => string 'Home/main/changliang' (length=20)
'__EXT__' => string '' (length=0)
'__SELF__' => string '/Thinkphp/index.php/Home/main/changliang' (length=40)URL,是什么输出什么 '__APP__' => string '/Thinkphp/index.php' (length=19)入口文件路径 '__MODULE__' => string '/Thinkphp/index.php/Home' (length=24)模块路径 '__CONTROLLER__' => string '/Thinkphp/index.php/Home/Main' (length=29)控制器路径
'__ACTION__' => string '/Thinkphp/index.php/Home/Main/changliang' (length=40)方法路径