Thinkphp 框架2
一、打开控制类文件
——
——
——
——
——
|
|
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { public function index(){ echo"hello world!"; }} |
用浏览器打开index方法:http://localhost/tp/index.php

为啥会这样
——打开

所以以后在做东西要通过浏览器输入地址查看效果
二、新建控制类
一、在controller文件下新建 例如:LoginController.class.php //每个单词首字母大写
|
|
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ echo"登录页面!"; }} |
地址:http://localhost/tp/index.php/Home/Login/login
效果:

二、新建模板
——
——
——
在view下新建一个文件夹 ,文件夹名和上面
方法名相对应,然后在文件下新建一个和方法名相同的html文件
|
|
<div>登录页面</div> |
在控制类中显示模板的方法
|
|
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ //显示模板 $this->show(); }} |
地址:http://localhost/tp/index.php/Home/Login/login
效果:

三、往摸板里扔变量
看一下
——打开
——
login.html代码
|
|
<div>登录页面</div><div>{$ceshi}</div> |
LoginController.class.php代码
|
|
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ //向TP里面注册变量 $this->assign("ceshi","张三"); //显示模板 $this->show(); }} |

四、从前端往后端传数据
login.html
|
|
<html><head></head><body><div>登录页面</div><div>{$ceshi}</div><form action="http://localhost/tp/index.php/Home/Login/chuli" method="post"> <div><input type="text" name="uid" /></div> <input type="submit" value="登录" /></form></body></html> |
LoginController.class.php代码
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ //向TP里面注册变量 $this->assign("ceshi","张三"); //显示模板 $this->show(); } public function chuli(){ echo $_POST["uid"]; }} |
点击登录
注意:上面form表单中提交的地址过于复杂,容易出错。找一个简单的方法
get_defined_constants(ture);
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ var_dump(get_defined_constants(ture)); //向TP里面注册变量 $this->assign("ceshi","张三"); //显示模板 $this->show(); } public function chuli(){ echo $_POST["uid"]; }} |
<html><head></head><body><div>登录页面</div><div>{$ceshi}</div><form action="__CONTROLLER__/chuli" method="post"> <div><input type="text" name="uid" /></div> <input type="submit" value="登录" /></form></body></html> |
<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{ public function login(){ //向TP里面注册变量 $this->assign("ceshi","张三"); //显示模板 $this->show(); } public function chuli(){ echo $_POST["uid"]; }} |
效果一样