2013-03-09 27 views
5

Tôi đang cố gắng tạo trang đăng ký 3 bước withh cakePHP. Bước đầu tiên là OK, nó chèn dữ liệu vào db nhưng bước thứ hai không hoạt động. Ai đó có thể giúp đỡ được không?CakePHP 3 bước đăng ký

Đây là mã điều khiển của tôi:

<?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') { 

    } 
} 
} 

?> 

Đây là Model:

<?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; 
} 
} 

?> 

Và đây là quan điểm cho 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; ?> 

Vì vậy, như tôi đã nói trên Bước đầu tiên, tất cả đều OK, nó sẽ lưu người dùng trong DB nhưng bước thứ hai tôi muốn đặt thêm thông tin và khi tôi gửi nó hiển thị thông báo Flash phiên mà người dùng không thể lưu, do đó có nghĩa là thông tin người dùng thứ hai không được lưu trong DB.

Vui lòng trợ giúp!

+1

bạn có thể cho biết những gì không hoạt động không? sao dữ liệu không được lưu trong DB? –

+0

Có trong bước đầu tiên nó tiết kiệm nhưng trong giây nó không tiết kiệm trong DB. –

+0

Bạn có thể làm rõ vấn đề bạn đang gặp phải không? Bạn nói rằng bước thứ hai không hiệu quả, theo nghĩa nào là nó không hoạt động? – David

Trả lời

6

Tìm thấy giải pháp. Đã xảy ra lỗi khi xác thực. Ở bước thứ hai, nó đã cố gắng xác thực hộp kiểm Điều khoản Sử dụng. Vì vậy, đây là mã hoàn chỉnh của 3 BƯỚC ĐĂNG KÝ:

USER CONTROLLER:

<?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 
    } 
} 
} 

USER MODEL:

<?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']; 
     } 
    } 
} 
} 


?> 

XEM '/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; ?> 

Nếu ai đó muốn đăng ký với các bước khác, phần còn lại chỉ giống nhau!

1

thử kiểm tra $step như bên dưới.

if(!$step){ 

empty được sử dụng để kiểm tra xem array có trống hay không.

+0

Điều đó làm việc tốt, tôi có tất cả 3 trang được hiển thị chính xác nhưng khi tôi cố gắng gửi thứ hai, nó cho tôi một lỗi. –

+0

@ user2150848 lỗi nào.? –

+0

Không thể lưu người dùng. Vui lòng thử lại. Đó là phiên flash nếu không thành công. –

4

Bạn nên xem xét tách các bước thành các hành động và chế độ xem riêng lẻ để làm cho mọi thứ đơn giản và dễ đọc hơn/gỡ lỗi hơn.

Bước 1

Vì bạn sẽ tạo tài khoản người dùng vào thời điểm này, bạn sẽ tự động đăng nhập cho người dùng một lần lưu lại và xác nhận. Sau đó, bạn không phải duy trì thông tin trong Phiên, điều đó là xấu trong trải nghiệm của tôi.

Một email có thể được gửi đến người dùng vào thời điểm này từ mẫu afterSave()

Bước 2

tôi sẽ di chuyển này để một hành động được gọi là "Hồ sơ" cho phép người dùng cập nhật của họ thông tin hồ sơ bổ sung. Vì họ đã đăng nhập, bạn có thể dễ dàng tìm thấy() Người dùng và lưu hồ sơ của họ. Điều này có thể được tái sử dụng trong tương lai.

Bước 3

Đây mới chỉ là "cảm ơn" từ những gì tôi có thể nhìn thấy. Bạn có thể chuyển hướng đến PagesController và trả về một thankyou.ctp đơn giản hơn là sử dụng hành động User Controller mà không làm gì cho khung nhìn.

Đăng nhập vẫn có thể xảy ra mặc dù địa chỉ email của họ chưa được xác minh. Bạn sẽ chỉ cho phép truy cập vào các phần nhất định của trang web cho đến khi họ nhấp vào liên kết được gửi tới họ trong email.