ThinkPHP为PHP开发搭建了简洁易用的框架,学习总结下
1、建立项目文件app,新建网站入口文件index.php,入口文件几乎都一致:
<?php
define('THINK_PATH','../ThinkPHP/'); //定义ThinkPHP框架路径(相对于入口文件)
define('APP_NAME','Myapp');
define('APP_PATH','.');
require(THINK_PATH."/ThinkPHP.php");
App::run();
?>
2、运行入口文件后会,项目会自动产生多个文件夹如conf、lib下的Action和Model、Tpl等。
3、配置项目
在conf目录下面的config.php是配置文件,可以用来配置数据库信息。
<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'haqiu',
'DB_USER'=>'root',
'DB_PWD'=>'',
'DB_PREFIX'=>'blog_',
//'DEFAULT_THEME'=>'default',
);
?>
4、模板定义
项目自动生成已经为我们生成了Tpl/default目录,我们只需要在default目录下创建Index目录,表示存放Index模块的模板文件。
可以定义多个html文件,这里是注册页面
<HTML>
<HEAD>
<TITLE> 欢迎注册 </TITLE>
</head>
<form name="myform" method="post" action="__URL__/insertUser">
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" maxLength="15" name="username" size="20">
</td>
</tr>
<tr>
<td>密 码:</td>
<td>
<input type="password" maxLength="15" name="password" size="20">
</td>
</tr>
<tr>
<td>确认密码:</td>
<td>
<input type="password" maxLength="15" name="repassword" size="20">
</td>
</tr>
<tr>
<td>邮 箱:</td>
<td>
<input type="text" maxLength="15" name="email" size="20">
</td>
</tr>
<tr>
<td>状态设置:</td>
<td>
<input type="text" maxLength="15" name="state" size="50">
</td>
</tr>
<tr>
<td align="center" valign="middle" >
<input type="submit" name="show" value="注册" >
</td>
</tr>
</table>
</form>
</HTML>
5、模型定义
在模型中,可以对数据进行相关设置如自动验证和自动完成,调用模型类的时候,可以用快捷D方法来实现。
下面是对用户数据表的相关设置,类名前缀必须和数据表的名字一致。
<?php
class loginModel extends Model{
protected $_validate = array(
array('username','checkname','用户名不能为空!',0,'callback',1),
array('username','','该账号已存在!',0,'unique',1),
array('password','checkpwd','密码至少六位!',0,'callback'),
array('repassword','password','确认密码不正确!',1,'confirm'),
array('email','checkemail','邮箱格式不正确!',0,'callback'),
);
public function checkname(){
$name = $_POST["username"];
if ($name==null){
return false;
}
else {
return true;
}
}
public function checkpwd(){
$pwdlen = strlen($_POST["password"]);
if ($pwdlen<6){
return false;
}
else {
return true;
}
}
public function checkemail(){
$email = $_POST["email"];
$a = substr_count($email,'@');
if ($a==1){
return true;
}
else {
return false;
}
}
protected $_auto = array(
array('password','md5',1,'function'),
);
}
?>
6、实现业务逻辑
在项目的Lib\Action目录下自动生成的IndexAction.class.php文件,这个文件就是控制器,完成Index的模块实现。删除IndexAction类默认生成的Index方法,添加新的方法:
<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action{
public function zhuce(){
$this->display();
}
public function insertUser(){
$user=D("login");
$vo = $user->create();
if($vo != false){
$user->add();
$this->redirect('login');
}else{
echo $user->getError();
}
}
public function login(){
$this->display();
}
public function userlogin(){
$user=M("login");
//echo $_POST["username"];
//
$vo = $user->create();
$condition["username"] = $vo['username'];
//echo $condition["username"];
//var_dump($vo);
//exit;
$usercheck = $user->where($condition)->findAll();
//var_dump ($usercheck);
// echo $usercheck[0]["password"];
//$usercheck = $user->getByName($vo['username']);
//var_dump ( $usercheck);
if ($usercheck==null){
echo "用户名不存在";
}
elseif($usercheck[0]["password"]!=md5($vo["password"])){
echo "密码输入错误!";
}
else {
setcookie('user_id',$usercheck[0]["id"],time()+3600);
$this->redirect('edit');
}
}
public function edit(){
//var_dump($_COOKIE["user_id"]);
$this->display();
}
public function insertblog(){
$diary=D("diary");
$vo=$diary->create();
if($vo != false){
$diary->add();
$this->redirect('show');
}else{
echo $diary->getError();
}
}
public function show(){
$diary = D("diary");
$board = D("board");
$condition["userID"] = $_COOKIE["user_id"];
$list = $diary->where($condition)->order('id desc')->find();
//setcookie("diaryID",$list[0]["id"],time()+3600);
$_SESSION["diaryID"]=$list["id"];
echo "主题:".$list["title"]." 时间:".$list["datetime"].'<br />';
echo "内容:".$list["content"].'<br />';
echo "***********************************************<br />";
//var_dump ($_SESSION["diaryID"]);
$conditionBoard["diaryID"] = $_SESSION["diaryID"];
$boardlist = $board->where($conditionBoard)->order('id desc')->findAll();
foreach ($boardlist as $value){
echo $value["username"]." ".$value["creat_time"].'<br />';
echo $value["content"].'<br />';
echo "____________________________________________<br />";
}
$this->display();
}
public function boardinsert(){
$board = D("board");
$vo = $board->create();
if($vo != false){
$board->add();
echo "评论成功!";
$this->redirect('show');
}
else{
echo $diary->getError();
}
}
}
?>
7、URL解析
http://localhost/app/index.php/Index/zhuce
表示app项目,Index.php入口文件,Index控制类中zhuce方法
提个词吧