1、ThinkPHP入门体验
1.1控制器
a.控制器必须放在Application/Home/Controller文件夹内
b.控制器的类文件命名规则(IndexController.class.php)
c.在ThinkPHP所有控制器必须继承基础控制器,并且有命名空间
$this->assign('rows',$rows); //分配name对应的value到页面上
$this->display('index'); //选择index模板文件
1.3视图
1.1控制器
a.控制器必须放在Application/Home/Controller文件夹内
b.控制器的类文件命名规则(IndexController.class.php)
c.在ThinkPHP所有控制器必须继承基础控制器,并且有命名空间
$this->assign('rows',$rows); //分配name对应的value到页面上
$this->display('index'); //选择index模板文件
路径:Home/Controller/UserController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\UserModel;
class UserController extends Controller {
public function index(){
$UserModel = new UserModel();
$rows = $UserModel->select();
$this->assign('rows',$rows);
$this->display();
}
}
1.2模型
a.模型必须放在Model的文件夹下
b.模型的类文件必须以.class.php结尾(如果出现未找到控制器错误,请检查是否代码.class.php)
c.在Thinkphp中所有的模型都必须继承基础控制器Think/Model(注意命名空间的概念),必须引入命名空间
d.命名规范控制器必须以Model结尾,例如xxxModel.class.php;
f.在ThinkPHP中默认xxxModel操作就是xxx表
路径:Home/Model/UserModel.class.php
<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model{
}
1.3视图
a.视图模具文件存在目录Application/Home/view/控制器名称/方法.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<table bgcolor="#848484" border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<?php
foreach($rows as $a):
?>
<tr>
<td><?php echo $a['id'];?></td>
<td><?php echo $a['name'];?></td>
<td><?php echo $a['age'];?></td>
</tr>
<?php endForeach; ?>
</table>
</body>
</html>
连接数据库配置:
<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'thinkphp', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => 'electronic', // 密码
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => '', // 数据库表前缀
'DB_CHARSET'=> 'utf8', // 字符集
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志 3.2.3新增
);
访问路径:/index.php/Home/User/index