1、控制器以下php
引用use app\index\model\User;app
//注意模型类名不能和控制器类名相同post
public function index()
{
return $this->fetch('index');//显示模版
}
public function add()//添加数据
{
$user=new User;
//allowField过滤无用的字符串
//validata开启字段验证机制
//input("post.")表示传送过来的所有参数
if($user->allowField(true)->validate(true)->save(input("post.")))
{
return '添加成功';
}else{
return $user->getError();
}
}
2、模型内容以下fetch
namespace app\index\model;
use think\Model;
//没有处理任何东西,能够选加载一些要用的如自动完成
class User extends Model
{
}
三模版内容ui
//{:url('index/users/add')}当前控制器提交地址this
//{$Request.token}随机TOKEN须要引用REQUESTurl
<h2>建立用户</h2>
<form method="post" action="{:url('index/users/add')}">
用户:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
邮箱:<input type="mail" name="mail"/><br/>
<input type="hidden" name="__token__" value="{$Request.token}">
<input type="submit" value="提交"/>
</form>
4、验证层spa
一、在application新建validate文件目录orm
二、新建类文件user.php 注意与表名相同。与模型类名也是相同的内容以下:token
namespace app\index\validate;
use think\Validate;
class User extends Validate
{
//验证规则变量必须是rule,更多验证规则请参考手册
protected $rule=[ ['username','require|min:4','用户名必须|用户名不能少于4位'], ['mail','email','邮箱格式不正确'], ['password','require','密码必须'] ];}