2013-03-09 6 views
5

मैं cakePHP के साथ एक 3 चरण पंजीकरण पृष्ठ बनाने की कोशिश कर रहा हूं। पहला कदम ठीक है, यह डेटा को डीबी में सम्मिलित करता है लेकिन दूसरा चरण काम नहीं कर रहा है। क्या कोई मदद कर सकता है?केकेपीएचपी 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

क्या आप बता सकते हैं कि क्या काम नहीं कर रहा है? डेटा डीबी में बचत नहीं कर रहा है? –

+0

हां पहले चरण में यह बचत कर रहा है लेकिन दूसरे में यह डीबी में बचत नहीं कर रहा है। –

+0

क्या आप इस मुद्दे को स्पष्ट कर सकते हैं? आप कहते हैं कि दूसरा कदम काम नहीं कर रहा है, किस अर्थ में यह काम नहीं कर रहा है? – David

उत्तर

6

मिला समाधान। सत्यापन में एक त्रुटि हुई थी। दूसरे चरण पर इसने उपयोग की शर्तें चेकबॉक्स को सत्यापित करने का प्रयास किया।

उपयोगकर्ता नियंत्रक::

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


?> 

दृश्य '/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; ?> 
तो यहाँ 3 कदम पंजीकरण की पूरी कोड है

अगर कोई और कदम के साथ पंजीकरण करना चाहता है, तो बाकी वही है!

1

नीचे दिए गए $step को जांचने का प्रयास करें।

if(!$step){ 

empty अगर array खाली है या नहीं की जाँच करने के लिए किया जाता है।

+0

यह ठीक काम करता है, मेरे पास सभी 3 पृष्ठ सही तरीके से प्रदर्शित होते हैं लेकिन जब मैं दूसरा सबमिट करने का प्रयास करता हूं तो यह मुझे एक त्रुटि दे रहा है। –

+0

@ user2150848 कौन सी त्रुटि।? –

+0

उपयोगकर्ता सहेजा नहीं जा सका। कृपया पुन: प्रयास करें। सफलता नहीं होने पर सत्र फ़्लैश है। –

4

आपको चरणों को सरल और आसान बनाने/डीबग करने के लिए अलग-अलग कार्यों और विचारों में कदमों को विभाजित करने पर विचार करना चाहिए।

चरण 1

आप इस बिंदु पर उपयोगकर्ता खाता बन जाएगा के बाद से, आप स्वचालित रूप से एक बार बचाया और मान्य उपयोगकर्ता प्रवेश करना होगा। तब आपको सत्र में जानकारी जारी रखने की ज़रूरत नहीं है, जो मेरे अनुभव में बुरा है।

एक ईमेल मॉडल afterSave() से इस बिंदु पर उपयोगकर्ता के लिए भेजा जा सकता है

चरण 2

मैं अद्यतन करने के लिए एक उपयोगकर्ता कोई ऐसा कार्य जो की अनुमति देता है "प्रोफ़ाइल" कहा जाता है को यह कदम होगा उनके अतिरिक्त प्रोफाइल जानकारी। चूंकि वे पहले से लॉग इन हैं, आप आसानी से() उपयोगकर्ता को ढूंढ सकते हैं और अपनी प्रोफ़ाइल सहेज सकते हैं। भविष्य में इसका पुन: उपयोग किया जा सकता है।

चरण 3

यह सिर्फ "धन्यवाद" मैं क्या देख सकते हैं से पेज है। आप पेज नियंत्रक पर रीडायरेक्ट कर सकते हैं और उपयोगकर्ता नियंत्रक क्रिया का उपयोग करने के बजाय एक सरल thankyou.ctp प्रस्तुत कर सकते हैं जो दृश्य के लिए कुछ भी नहीं करता है।

लॉगिन अभी भी हो सकता है भले ही उनका ईमेल पता सत्यापित नहीं है। आप केवल वेबसाइट के कुछ हिस्सों तक पहुंच की इजाजत देंगे जब तक कि उन्होंने उन्हें ईमेल में भेजे गए लिंक पर क्लिक नहीं किया हो।

संबंधित मुद्दे