2012-08-01 2 views
4

저는 Yii 프레임 워크를 처음 접했습니다. Yii와 함께 등록 양식을 구현하고 싶습니다. 데모 프로젝트에서 블로그 프로젝트를 확인했지만 등록 양식이 없습니다. 누구든지이 문제에 대한 자습서를 알고 있습니까?Yii 프레임 워크를 사용한 등록 양식

+0

여기에 Yii 포럼의 코드 [http://www.yiiframework.com/forum/index.php/topic/27242-registration/](html/www.yiiframework.com/)을 사용하여 등록하는 방법에 대한 설명이 나와 있습니다. forum/index.php/topic/27242-registration /) –

답변

1

CActiveForm을 사용하여 yii로 양식을 쉽게 만들 수 있습니다. 여기

(예를 들어 LoginForm 참조) YII 문서의 링크 :: 당신은 등록 양식을 작성해야합니다 Yii CActiveForm

0
  • , CAvtiveForm을 사용하고 있습니다.
  • 사용자 모델을 만듭니다.
  • 컨트롤러를 만듭니다

    class UsersController extends Controller 
    { 
        public function actionSignup() 
        { 
         $model = new RegistrationForm; 
    
         if (isset($_POST['RegistrationForm'])) { 
          if ($model->validate()) { 
           $user = new Users; 
           $user->populateRecord($form->attributes); 
           $user->save(); 
           $this->redirect(Yii::app()->createUrl('/')); 
          } 
         } 
    
         $this->render('signup', array('model' => $model)); 
        } 
    } 
    
4

먼저, 사용자를위한 테이블을 만들 수 있습니다. gii를 사용하여이 테이블을 기반으로 사용자 모델과 컨트롤러를 생성하고 원하는대로 코드를 조정하십시오. 사용자 컨트롤러 내부 등록 처리하는 함수를 만들 :

function actionRegister() { 
    $model = new User('register'); 

    // collect user input data 
    if(isset($_POST['User'])) 
    { 
     $model->attributes=$_POST['User']; 

     // validate user input and redirect to the previous page if valid 
     $model->setAttributes(array(
      'datereg' => time(), //additional data you want to insert 
      'lastlogin' => time() //additional 

     )); 

     if($model->save()) 
     { 
          //optional 
      $login=new LoginForm; 
      $login->username = $_POST['User']['username']; 
      $login->password = $_POST['User']['password']; 
      if($login->validate() && $login->login()) 
       $this->redirect('/pages/welcome'); 
     } 

    } 
    else 
     // display the registration form 
     $this->render('register',array('model'=>$model)); 
    } 

당신은 유효한보기 파일이 있어야합니다 (register.php)

$login=new LoginForm; 
$login->username = $_POST['User']['username']; 
$login->password = $_POST['User']['password']; 
if($login->validate() && $login->login()) 
$this->redirect('/pages/welcome'); 

이 코드 블록은 '인증'사용자가 그를 기록 성공적인 등록 직후에 그것은 CUserIdentity를 사용합니다. $ login-> login()은 암호를 해시합니다.

또한, 당신은 컨트롤러도 괜찮 내부에 암호를 해싱 테이블

public function beforeSave() { 

    if(parent::beforeSave() && $this->isNewRecord) { 

     $this->password = md5($this->password); 

    } 

    return true; 
} 

에 삽입하기 전에 데이터를 처리하기 위해 이런 식으로 뭔가가 있어야합니다. 그러나 나는 Model 클래스 안에서 DB 관련 모든 것을하고 싶다.

$ model-validate()를 호출하지 않은 것을 볼 수 있습니다. 이는 $ model-> save()가 유효성 검사 메소드를 호출하기 때문입니다. 더 많은 정보 : http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

+0

데이터가 데이터베이스에 삽입되지 않고 뷰에 등록 버튼을 클릭하면 다시로드됩니다. – micky

5

등록 시스템 용.

다음 단계를 따라야합니다.

  1. 먼저 사용자/가입/등록 테이블을 원하는대로 만듭니다.
  2. 이제 CRUD를 생성하여 사용자 테이블에 데이터를 쉽게 삽입 할 수 있습니다.

나는 테이블을 가지고 있고 그것은 즉

  1. 지금 당신이 "등록"라는 이름의보기에서 폴더를 만들 ID, 이름, conatct_no, 이메일, 비밀번호 다섯 개 가지 분야가있다.
  2. 보기에서 방금 index.php 및 register.php 파일을 만들었습니다.
  3. register.php는 (는) 귀하의 양식입니다. 당신이 만들 수 있습니다.

등록.PHP

<h2>Registration Form</h2> 
<div class="form"> 
<?php echo CHtml::beginForm(); ?> 

    <?php echo CHtml::errorSummary($model); ?> 

    <div class="row"> 
     <?php echo CHtml::activeLabel($model,'Name'); ?> 
     <?php echo CHtml::activeTextField($model,'name') ?> 
    </div> 


    <div class="row"> 
     <?php echo CHtml::activeLabel($model,'Contact Number'); ?> 
     <?php echo CHtml::activeTextField($model,'contact_no') ?> 
    </div> 

    <div class="row"> 
     <?php echo CHtml::activeLabel($model,'email'); ?> 
     <?php echo CHtml::activeTextField($model,'email') ?> 
    </div> 

    <div class="row"> 
     <?php echo CHtml::activeLabel($model,'password'); ?> 
     <?php echo CHtml::activePasswordField($model,'password') ?> 
    </div> 

    <div class="row"> 
     <?php echo CHtml::activeLabel($model,'Retype Password'); ?> 
     <?php echo CHtml::activePasswordField($model,'retypepassword') ?> 
    </div> 

    <div class="row submit"> 
     <?php echo CHtml::submitButton('Register'); ?> 
    </div> 

    <?php echo CHtml::endForm(); ?> 
</div><!-- form --> 
  1. 이제 즉 registerController.php 등록을위한 컨트롤러 파일이 필요합니다. registerController.php

    class registerController extends Controller 
    { 
        public function actionIndex() { 
        $this->render('index'); 
        } 
    
        public function actionRegister() { 
        $model = new RegisterForm ; 
        $newUser = new User; 
    
    if(isset($_POST['RegisterForm'])) { 
         $model->attributes = $_POST['RegisterForm']; 
         if($model->validate()) { 
         $newUser->name = $model->name ; 
         $newUser->contact_no = $model->contact_no; 
         $newUser->email = $model->email; 
         $newUser->password = $model->password; 
         if($newUser->save()) { 
         $this->_identity = new UserIdentity($newUser->email,$newUser->password); 
         if($this->_identity->authenticate()) 
         Yii::app()->user->login($this->_identity); 
         $this->redirect(Yii::app()->user->returnUrl); 
         } 
         } 
        } 
    $this->render('register',array('model'=>$model)); 
        } 
    } 
    

    지금 당신은 당신의 검증을위한 모델 파일이 필요합니다. RegisterForm.php

    클래스 RegisterForm 확장 CFormModel { public $ contact_no; public $ name; public $ email; public $ password; public $ retypepassword;

    공개 함수 tableName() { return 'user'; }

    공공 기능 규칙()) '필요'{ 반환 배열 ( 배열 ('이름, contact_no, 이메일, 비밀번호, retypepassword', 배열 ('이름, 이메일, 비밀번호, retypepassword', ' 배열 ('contact_no', 'length', 'max'= '200') 배열 ('전자 메일', '전자 메일', '메시지'=> '유효한 전자 메일을 넣으십시오'), 배열 ('password', 'compare', 'compareAttribute'=> 'retypepassword'), 배열 ('문자열', '필수', ' id, name, contact_no, email ','safe ','on '=>'search '), ); } }

당신은 또한 당신의 보호/구성 요소 디렉토리에 UserIdentity.php 파일을 변경해야합니다.

class UserIdentity extends CUserIdentity 
{ 
    private $_id ; 

    public function authenticate() 
    { 
    $email = strtolower($this->username); 
    $user = User::model()->find('LOWER(email)=?',array($email)); 
    if($user === null) 
    $this->errorCode = self::ERROR_USERNAME_INVALID ; 
    else if(!$user->password === $this->password) 
    $this->errorCode = self::ERROR_PASSWORD_INVALID ; 
    else { 
    $this->_id = $user->id ; 
    $this->username = $user->email ; 
    $this->errorCode = self::ERROR_NONE ; 
    } 
    return $this->errorCode == self::ERROR_NONE ; 
    return !$this->errorCode; 
    } 

    public function getId() { 
    return $this->_id ; 
    }  
} 

그게 전부입니다.

전체 등록 양식을 설정 했으므로 직접 작성했습니다. 먼저 타이머가있는 경우

는, 당신은 어떻게 컨트롤러 작업 방법 컨트롤러는 뷰와 모델과 상호 작용을 이해할 필요가있다. 이미 익숙하다면 등록 시스템을 쉽게 만들 수 있습니다.

여기에 yii를 이해하는 데 도움이되는 링크가 있습니다.

http://www.yiiframework.com/screencasts/

감사합니다.

관련 문제