日期:2014-05-17  浏览次数:20423 次

ThinkPHP学习笔记(七)实例化中create方法的作用,以及令牌的使用

其中的success的模板,可以在ThinkPHP中的example中搜索到,然后放入tpl下的default下的Public中


create使用

<?php
class CurdAction extends Action{
	//创建和增删改成
	//create()
	//创建会完成自动映射、自动填充、自动验证
	//如果开启了字段缓存,会去匹配字段,如果不存在,会获取字段,返回一个数组
	//create方法中有自动令牌,向当前的session会话中放入Md5加密的字符串会将字符串插入点表单的</from>之前,session和form中有
	//令牌可以在conf中配置;可以在页面设置不显示令牌但是一样使用令牌<!--{__NOTOKEN__}-->
	//如果页面中有多个表单,只有一个表单需要令牌,可以在此表单中显示指定:<!--{__TOKEN__}-->
	//create默认获取数据的方法是post方法
	public function index() {
		$this->display();
	}
	
	public function add(){
		$user=new Model('User');
		
//		//不使用create方法验证令牌
//		if (!$user->autoCheckToken($_POST)) {
//			//令牌验证失败的代码;
//			$this->error($user->getError());
//		}else{
//			//获取其中信息$user
//			$user->username;
//			$user->username=md5($user->password);
//	//		dump($user);
//			if ($user->add()) {
//				$this->success('添加成功');
//			}else{
//				$this->error($user->getError());
//			}
//		}
		
		
		//返回的数组信息如果用自动填充,返回值和信息更多
		if ($vo=$user->create()) {
//			//这个方法是执行成功的一个方法,会给出页面的各种信息
//			dump($vo);
//			//successs模板可以在ThinkPHP的example中找到,放入到default中的public目录中
//			$this->success('create成功');
			
			//获取其中信息$user
			$user->username=md5($user->password);
//			dump($user);
			if ($user->add()) {
				$this->success('添加成功');
			}else{
				$this->error($user->getError());
			}
		}else {
			//如果执行失败,代码不会继续向后执行
//			dump($vo);
//			dump($user);
			//error模板可以在ThinkPHP的example中找到,放入到default中的public目录中
			$this->error($user->getError());
		}
	}
}
?>

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CURD</title>
</head>
<body>
<form action="__URL__/add" method="post">
	<input type="text" name="username"/>
	<input type="text" name="password"/>
	<input type="submit" value="提交"/>
	<!--{__NOTOKEN__}-->
</form>
</body>
</html>