2013-03-09 146 views
5

我正在尝试用h cakePHP创建一个3步注册页面。 第一步是好的,它将数据插入分贝,但第二步不起作用。有人可以帮忙吗?CakePHP 3步注册

这里是我的控制器代码:

<?php 

App::uses('Controller', 'Controller'); 

class UsersController extends AppController { 

public $name = 'Users'; 

public function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Auth->allow('signup'); 
} 

public function login(){ 
    if($this->request->is('post')){ 
     if($this->Auth->login()){ 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg')); 
     } 
    } 
} 
public function logout(){ 
    $this->redirect($this->Auth->logout()); 
} 
// SIGN UP ACTIONS 
public function signup($step = null){ 
    // SET STEP VARIABLE 
    $this->set('s', $step); 
    // STEP 1 - REGISTER 
    if(empty($step)){ 
     if ($this->request->is('post')) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->write('regUser', $this->request->data['User']['email']); 
       // $this->Session->setFlash('The user has been saved, please enter other information!', 
       //       'default', array('class' => 'okormsg')); 
       $regUser = $this->User->find( 'first', array('conditions' => array(
               'User.username' => $this->Session->read('regUser')))); 
       $id = $regUser['User']['id']; 

       $this->Session->write('regUserId', $id); 

       $this->redirect(array('personal-info')); 
      } else { 
       $this->Session->setFlash( 'There was an error, please check the fields bellow!', 
              'default', array('class' => 'errormsg')); 
      } 
     } 
    // STEP 2 - PERSONAL INFORMATION 
    } elseif($step == 'personal-info') { 
     if ($this->request->is('post')) { 
      $id = $this->Session->read('regUserId'); 
      $this->User->id = $id; 

      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash('The user has been saved'); 
       $this->redirect(array('complete')); 
      } else { 
       $this->Session->setFlash('User could not be saved. Please, try again.'); 
      } 
     } 
    // STEP 3 - COMPLETE REGISTRATION 
    } elseif($step == 'complete') { 

    } 
} 
} 

?> 

这里是型号:

<?php 

class User extends AppModel { 

public $validate = array (
    'name'=>array(
     'Please enter your name!'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your name!' 
     ) 
    ), 
    'surname'=>array(
     'Please enter your surname!'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your surname!' 
     ) 
    ), 
    'email'=>array(
     'Valid email'=>array(
      'rule'=>array('email'), 
      'message'=>'Please enter a valid Email address!' 
     ), 
     'Already exists'=>array(
      'rule'=>'isUnique', 
      'message'=>'This Email is already registered in our database!' 
     ) 
    ), 
    'password'=>array(
     'Not empty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your password!' 
     ), 
     'Match password'=>array(
      'rule'=>'matchPasswords', 
      'message'=>'Your passwords do not match!' 
     ) 
    ), 
    'password_confirm'=>array(
     'Not empty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please confirm your password!' 
     ) 
    ), 
    'terms'=>array(
     'Agree'=>array(
      'rule'=>'notEmpty', 
      'required'=>true, 
      'message'=>'You must agree to Terms and conditions!' 
     ) 
    ) 
); 

public function matchPasswords($data){ 
    if ($data['password'] == $this->data['User']['password_confirm']){ 
     return true; 
    } else { 
     $this->invalidate('password_confirm', 'Your passwords do not match!'); 
     return false; 
    } 
} 
public function beforeSave(){ 
    if(!empty($this->data['User']['password'])) { 
     $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 
    } 
    if (empty($this->data['User']['username'])) { 
     $this->data['User']['username'] = $this->data['User']['email']; 
    } 
    return true; 
} 
} 

?> 

这里是signup.ctp认为

<!-- SIGN UP STEPS --> 
<!-- SIGN UP - 1 (REGISTER) --> 
<?php if(empty($s)): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate active">Register</div> 
     <div class="paginate">Personal Information</div> 
     <div class="paginate">Complete Registration</div> 
     <h1>User Registration</h1> 
     <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br /> 
     <span style="color:#900">All field are required!</span></p> 
    <?php 
     echo $this->Form->create(); 
     echo $this->Form->input('name'); 
     echo $this->Form->input('surname'); 
     echo $this->Form->input('email'); 
     echo $this->Form->input('password'); 
     echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password')); 
     echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox')); 
     ?> I accept <a href="#" class="link">Terms Of Use</a> <? 
     echo $this->Form->end('Continue Registration'); 
    ?> 
</div> 
</div> 
<!-- SIGN UP - 2 (PERSONAL INFO) --> 
<?php elseif($s == 'personal-info'): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate">Register</div> 
     <div class="paginate active">Personal Information</div> 
     <div class="paginate">Complete Registration</div> 
     <h1>User Registration</h1> 
     <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br /> 
     <span style="color:#900">All field are required!</span></p> 
    <?php 
     echo $this->Form->create(); 
     echo $this->Form->input('phone'); 
     echo $this->Form->input('address'); 
     echo $this->Form->input('city'); 
     echo $this->Form->input('ptt', array('label'=>'Postal Code')); 
     echo $this->Form->input('state'); 
     echo $this->Form->input('country'); 
     echo $this->Form->end('Complete Registration'); 
    ?> 
</div> 
</div> 
<!-- SIGN UP - 3 (COMPLETE) --> 
<?php elseif($s == 'complete'): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate">Register</div> 
     <div class="paginate">Personal Information</div> 
     <div class="paginate active">Complete Registration</div> 
     <h1>User Registration</h1> 
     <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br /> 
     <span style="color:#900">All field are required!</span></p> 
</div> 
</div> 
<? else: ?> 
<div id="register" class="container padded"> 
<div id="register"> 
    Unknown page! 
</div> 
</div> 
<? endif; ?> 

因此,正如我在说第一步它一切正常,它将用户保存在数据库中,但第二步我想提供更多信息,当我提交时显示会话闪烁消息表明用户无法保存,这意味着第二个用户信息不会保存在数据库中。

请帮忙!

+1

你能告诉什么是不工作?数据是不是保存在数据库中? – 2013-03-09 06:15:44

+0

第一步是保存,但第二步不保存在数据库中。 – 2013-03-09 06:25:33

+0

你能否澄清你遇到的问题?你说第二步不起作用,它在什么意义上不起作用? – David 2013-03-09 06:27:18

回答

6

找到解决方案。验证时出错。第二步,它试图验证使用条款复选框。因此,这里是3工序登录的完整代码:

用户控制器:

<?php 

App::uses('Controller', 'Controller'); 

class UsersController extends AppController { 

public $name = 'Users'; 

public function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Auth->allow('signup'); 
} 

public function login(){ 
    if($this->request->is('post')){ 
     if($this->Auth->login()){ 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg')); 
     } 
    } 
} 
public function logout(){ 
    $this->redirect($this->Auth->logout()); 
} 
// SIGN UP ACTIONS 
public function signup($step = null){ 
    // SET STEP VARIABLE 
    $this->set('s', $step); 
    // STEP 1 - REGISTER 
    if(!$step){ 
     $this->set('r', 1); 
     if ($this->request->is('post')) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->write('regUser', $this->request->data['User']['email']); 
       // $this->Session->setFlash('The user has been saved, please enter other information!', 
       //       'default', array('class' => 'okormsg')); 
       $regUser = $this->User->find( 'first', array('conditions' => array(
               'User.username' => $this->Session->read('regUser')))); 
       $id = $regUser['User']['id']; 
       $this->Session->write('regUserId', $id); 
       $this->redirect(array('personal-info')); 
      } else { 
       $this->Session->setFlash( 'There was an error, please check the fields bellow!', 
              'default', array('class' => 'errormsg')); 
      } 
     } 
    // STEP 2 - PERSONAL INFORMATION 
    } elseif($step == 'personal-info') { 
     $id = $this->Session->read('regUserId'); 
     $this->User->id = $id; 
     if ($this->request->is('post')) { 
      if ($this->User->save($this->request->data, array('validate' => false))) { 
       $this->redirect(array('complete')); 
      } else { 
       $this->Session->setFlash( 'User could not be saved. Please, try again.', 
              'default', array('class' => 'errormsg')); 
      } 
     } 
    // STEP 3 - COMPLETE REGISTRATION 
    } elseif($step == 'complete') { 
     // Send email function 
    } 
} 
} 

用户模型:

<?php 

class User extends AppModel { 

public $validate = array (
    'name'=>array(
     'Please enter your name!'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your name!' 
     ) 
    ), 
    'surname'=>array(
     'Please enter your surname!'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your surname!' 
     ) 
    ), 
    'email'=>array(
     'Valid email'=>array(
      'rule'=>array('email'), 
      'message'=>'Please enter a valid Email address!' 
     ), 
     'Already exists'=>array(
      'rule'=>'isUnique', 
      'message'=>'This Email is already registered in our database!' 
     ) 
    ), 
    'password'=>array(
     'Not empty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter your password!' 
     ), 
     'Match password'=>array(
      'rule'=>'matchPasswords', 
      'message'=>'Your passwords do not match!' 
     ) 
    ), 
    'password_confirm'=>array(
     'Not empty'=>array(
      'rule'=>'notEmpty', 
      'message'=>'Please confirm your password!' 
     ) 
    ), 
    'terms'=>array(
     'Agree'=>array(
      'rule'=>'notEmpty', 
      'required'=>true, 
      'message'=>'You must agree to Terms and conditions!' 
     ) 
    ) 
); 
public function matchPasswords($data){ 
    if ($data['password'] == $this->data['User']['password_confirm']){ 
     return true; 
    } else { 
     $this->invalidate('password_confirm', 'Your passwords do not match!'); 
     return false; 
    } 
} 
public function beforeSave(){ 
    if(!empty($this->data['User']['password'])) { 
     $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 
    } 
    if(!empty($this->data['User']['email'])){ 
     if (empty($this->data['User']['username'])) { 
      $this->data['User']['username'] = $this->data['User']['email']; 
     } 
    } 
} 
} 


?> 

VIEW '/Users/signup.ctp'

<!-- SIGN UP STEPS --> 
<!-- SIGN UP - 1 (REGISTER) --> 
<?php if(empty($s)): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate active">Register</div> 
     <div class="paginate">Personal Information</div> 
     <div class="paginate">Complete Registration</div> 
     <h1>User Registration</h1> 
     <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br /> 
     <span style="color:#900">All field are required!</span></p> 
    <?php 
     echo $this->Form->create(); 
     echo $this->Form->input('name'); 
     echo $this->Form->input('surname'); 
     echo $this->Form->input('email'); 
     echo $this->Form->input('password'); 
     echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password')); 
     echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox')); 
     ?> I accept <a href="#" class="link">Terms Of Use</a> <? 
     echo $this->Form->end('Continue Registration'); 
    ?> 
</div> 
</div> 
<!-- SIGN UP - 2 (PERSONAL INFO) --> 
<?php elseif($s == 'personal-info'): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate">Register</div> 
     <div class="paginate active">Personal Information</div> 
     <div class="paginate">Complete Registration</div> 
     <h1>User Registration</h1> 
     <p>This personal information are not required.<br /> 
    <?php 
     echo $this->Form->create(); 
     echo $this->Form->input('phone'); 
     echo $this->Form->input('address'); 
     echo $this->Form->input('city'); 
     echo $this->Form->input('ptt', array('label'=>'Postal Code')); 
     echo $this->Form->input('state'); 
     echo $this->Form->input('country'); 
     echo $this->Form->end('Complete Registration'); 
    ?> 
</div> 
</div> 
<!-- SIGN UP - 3 (COMPLETE) --> 
<?php elseif($s == 'complete'): ?> 
<div id="register" class="container padded"> 
<div id="register"> 
     <div class="paginate">Register</div> 
     <div class="paginate">Personal Information</div> 
     <div class="paginate active">Complete Registration</div> 
     <h1>Congratulation!</h1> 
     <p>Your account has been created and <strong>Email</strong> has been send to your inbox. Please check you inbox and verify address by clicking on the link provided in mail.</p> 
</div> 
</div> 
<? else: ?> 
<div id="register" class="container padded"> 
<div id="register"> 
    Unknown page! 
</div> 
</div> 
<? endif; ?> 

如果有人想用更多的步骤进行注册,其余的都是一样的!

1

请尝试检查$step如下。

if(!$step){ 

empty被用来检查array为空。

+0

这工作正常,我有所有3页正确显示,但当我尝试提交第二次它给我一个错误。 – 2013-03-09 06:17:28

+0

@ user2150848哪个错误。? – 2013-03-09 06:18:10

+0

用户无法保存。请再试一次。 即使没有成功,会话也会闪现。 – 2013-03-09 06:38:42

4

您应该考虑将步骤分解为单独的操作和视图,以使事情更简单,更易于读取/调试。

步骤1

因为你会在这一点上创建用户帐户,您应自动登录一次保存和验证用户。那么你不必坚持会话中的信息,这在我的经验中是不好的。

的电子邮件,可以在此时从型号afterSave()被发送到用户

步骤2

我会提出这个名为“个人资料”的操作,它允许用户更新他们的额外的资料信息。由于他们已经登录,您可以轻松找到()用户并保存他们的个人资料。这也可以在未来重用。

步骤3

这仅仅是“谢谢”从我所看到的页面。您可以重定向到PagesController并呈现一个简单的thankyou.ctp,而不是使用用户控制器操作,该操作对视图不做任何事情。

即使他们的电子邮件地址未验证,登录仍然可能发生。您只允许访问网站的某些部分,直到他们点击通过电子邮件发送给他们的链接。