thinkphp 3.2 模型的使用示例

下面是 UserController

 1 <?php
 2 namespace Home\Controller;
 3 use Think\Controller;
 4 class UserController extends Controller {
 5     /**-- 用户中心 --**/
 6     public function index(){
 7         $this->display();
 8     }
 9     /**-- 登陆页面 --**/
10     public function login(){
11         $this->display();
12     }
13     /**-- 执行登陆操作 --**/
14     public function dologin(){
15         $data = I('post.');
16         $result = D('User')->login($data);
17         var_dump($result);
18     }
19     /**-- 验证验证码 --
20     private function check_verify($code, $id = ''){   
21         $verify = new \Think\Verify();   
22         return $verify->check($code, $id);
23     }
24     **/
25 }

 

UserModel

 1 <?php
 2 namespace Home\Model;
 3 use Think\Model;
 4 class UserModel extends Model {
 5     /**-- 登陆 --**/
 6     public function login($data){
 7         if(!($this->check_verify($data['code']))){
 8             return '验证码错误';
 9         }
10         if($data['name'] == '' || $data['password'] == ''){
11             return '用户名或密码不能为空!';
12         }
13         $data['password'] = md5($data['password']);
14         $result = $this->where(array('name'=>$data['name'],'password'=>$data['password']))->find();
15         if($result){
16             return '欢迎您 '.$result['name'];
17         }else{
18             return '没有该用户';
19         }
20     }
21     /**-- 验证验证码 --**/
22     private function check_verify($code, $id = ''){   
23         $verify = new \Think\Verify();   
24         return $verify->check($code, $id);
25     }
26 }
27 ?>

 

posted @ 2019-04-15 16:23  哟风  Views(346)  Comments(0)    收藏  举报