Thinkphp 框架2

一、打开控制类文件

——————————

1
2
3
4
5
6
7
8
<?php
namespace 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 //每个单词首字母大写

 

1
2
3
4
5
6
7
8
<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function login(){
        echo"登录页面!";
    }
}

 

 地址:http://localhost/tp/index.php/Home/Login/login

效果:

二、新建模板

——————

在view下新建一个文件夹  ,文件夹名和上面方法名相对应,然后在文件下新建一个和方法名相同的html文件

1
<div>登录页面</div>

 在控制类中显示模板的方法

1
2
3
4
5
6
7
8
9
<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function login(){
        //显示模板
        $this->show();
    }
}

 

 地址:http://localhost/tp/index.php/Home/Login/login

效果:

三、往摸板里扔变量

看一下——打开——

login.html代码

1
2
<div>登录页面</div>
<div>{$ceshi}</div>

LoginController.class.php代码

1
2
3
4
5
6
7
8
9
10
11
<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function login(){
        //向TP里面注册变量
        $this->assign("ceshi","张三");
        //显示模板
        $this->show();
    }
}

 

四、从前端往后端传数据

login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace 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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace 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"];
    }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
    public function login(){
             
        //向TP里面注册变量
        $this->assign("ceshi","张三");
        //显示模板
        $this->show();
    }
    public function chuli(){
        echo $_POST["uid"];
    }
}

 效果一样

 

posted @   navyyouth  阅读(228)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示