注册并登录cakephp2.0

时间:2012-03-07 02:26:03

标签: cakephp cakephp-2.0

美好的一天!我现在将我的代码从1.3到2.0迁移到CakePHP。我只是想问一下,我该怎么做这个代码(从1.3)到2.0?这是代码:

function register() {
 if(!empty($this->data)) {
        // unset unrequired validation rules
        unset($this->User->validate['username']['check_user']);

        // validate & save data
        if($this->User->save($this->data)) {
            $this->data['User']['Password'] = md5($this->data['User']['Password']);         
            $this->User->save($this->data);
            // set Flash & redirect
            $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
            $this->redirect(array('action'=>'login'));
        }
        else{
            //$this->Session->setFlash(__('The user could not be saved.' , true));
            //$this->redirect(array('action' => 'register'));
        }
    }
}

这是我尝试解决的尝试代码:

public function register() {
    if($this->request->is('post')) {
    //unset($this->User->validate['username']['check_user']);

        // validate & save data

        //$this->data['User']['Password'] = md5($this->data['User']['Password']);     
         $this->request->data('User.Password', $this->request->data('User.Password'));    
        // $this->User->save($this->data);
        // set Flash & redirect
        if($this->User->save($this->request->data)) {
            $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
            $this->redirect(array('action'=>'login'));
        }
        else{
            //$this->Session->setFlash(__('The user could not be saved.' , true));
            //$this->redirect(array('action' => 'register'));
        }
    }
}

此代码用于登录,在1.3

中进行
function login() {
    //echo $_SESSION['User']['auth'];
    if(!isset($_SESSION['User']['id'])){
        if(!empty($this->data)) {
            if(($user = $this->User->validateLogin($this->data['User'])) == true) 
            { 
            //print_r(md5($this->data['User']['password']));
                $user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));
                //print_r ($user);
                if(!empty($user)){
                    $_SESSION['User']['id'] = $user['User']['id'];
                    $_SESSION['User']['name'] = $user['User']['Name'];
                    $_SESSION['User']['auth'] = $user['User']['auth'];
                    $this->redirect(array('controller'=>'ads','action'=>'index'));
                }else{
                    $this->Session->setFlash('Username/Password not match');
                    $this->redirect(array('action'=>'login'));
                }
            } 
        }
    }
        else{
        $this->Session->setFlash('Login First.');
        $this->redirect(array('controller'=>'ads','action'=>'index'));

    }
}

这是我在2.0中的代码,但它仍然不起作用。

public function login() {

    if(!($this->Session->read('user_id'))){
        if($this->request->is('post')) {        
            //$user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));

                if(!empty($user)){
                    $this->Session->write('user_id',$user['User']['id']);
                    $this->Session->write('name',$user['User']['Name']);
                    //$this->Session->write('name',$user['User']['Name']);
                    $this->redirect(array('controller'=>'ads','action'=>'index'));
                }else{
                    $this->Session->setFlash('Username/Password not match');
                    $this->redirect(array('action'=>'login'));
                }

        } 
    }else{
        $this->redirect(array('controller'=>'ads','action'=>'index'));
    }
}//end login

我希望有人回答我的问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我首先尝试在现有代码上运行迁移,您可能会感到惊讶。这是升级shell的链接:

http://book.cakephp.org/2.0/en/console-and-shells/upgrade-shell.html#upgrade-shell

先尝试一下。