1)在控制器UserController.php中新建一个处理用户登录action的方法 actionLogin
2)我们的用户登录表单不会和数据库有直接的关系, 所以我们的表单模型应该为表单模型, 显示在表单内的 label 内的 “字段” 我们应该全部定义为表单模型的属性,这样,当我们使用$form = $this->widget('CActiveForm');$form->textField($model, 'username');时才能够在表单模型的属性中找到该 label 的“字段名”
3)当用户填写完用户名和密码后,点击提交时,我们要在控制器的login方法内对用户提交的用户名和密码进行验证,
//关键代码如下 //实例化表单模型 $loginFormModel = new LoginForm();//注意:这里使用new关键字 if (isset($_POST['LoginForm'])) {//判断用户是否提交了表单 //如果提交了表单 //给表单模型内的属性对应的赋上值 $LoginFormModel->attributes = $_POST['LoginForm']; //判断用户信息 if ($LoginFormModel->validate() && $LoginFormModel->login()) { //用户通过验证,跳转到首页 $this->redirect('./index.php'); } $this->render('list', array('LoginFormModel' => $LoginFormModel)); }
上面我们调用了LoginForm模型内的validate方法和login方法.
validate()方法会按照rules方法内的验证规则对用户提交的数据进行验证, login()方法用来持久化用户登录