2013-12-15 65 views
0

我一直在尋找遍及互聯網,我似乎無法找到解決方案,只是通過頭簡單的登錄。我嘗試了各種方式。我現在遇到的問題只是在每次登錄時重新加載。只有當我去?r =網站/登錄它會讓我登錄。是否有某種頭部控制器!?!不知道什麼是錯的了。yii模態對話框登錄

現在我有這個在我的頭:

<?php $this->beginWidget(
'bootstrap.widgets.TbModal', 
array('id' => 'login') 
); ?> 

<?php 
$model=new LoginForm; 
$this->renderPartial('//site/login', array('model'=>$model)); 
?> 

<?php $this->endWidget(); ?> 

//stuff here 
      <?php $this->widget(
     'bootstrap.widgets.TbButton', 
     array(
      'label' => 'Signin', 
      'type' => 'warning', 
      'htmlOptions' => array(
       'data-toggle' => 'modal', 
       'data-target' => '#login', 
      ), 
     ) 
    ); 
?> 

然後在網站/登錄:

<div class="spacing"> 
<div class="form"> 
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form', 
    'enableClientValidation'=>true, 
    'clientOptions'=>array(
     'validateOnSubmit'=>true, 
    ), 
)); ?> 
<div class="form" id="login-form"> 
<div class="login-container"> 
    <div class="login-header"> 
    <h1 title="login">Sign In</h1> 
    </div> 
    <div class="login-form"> 
    <div class="input-position"> 
     <?php echo $form->labelEx($model,'Email',array('class'=>'span3','style'=>'text-align:left')); ?> 
     <?php echo $form->textField($model,'email', array('class'=>'span3')); ?> 
     <?php echo $form->error($model,'email'); ?> 
    </div> 

    <div class="input-position"> 
     <?php echo $form->labelEx($model,'password',array('class'=>'span3','style'=>'text-align:left')); ?> 
     <?php echo $form->passwordField($model,'password',array('class'=>'span3',)); ?> 
     <?php echo $form->error($model,'password'); ?> 
    </div> 

    <div class="input-position "> 
     <div class="rememberMe"> 
     <?php echo $form->checkBox($model,'rememberMe'); ?> 
     <?php echo $form->label($model,'rememberMe'); ?> 
     <?php echo $form->error($model,'rememberMe'); ?> 
    </div> 
     </div> 

     <div class="row buttons"> 
     <?php echo CHtml::submitButton('Login'); ?> 
     </div> 
     <?php $this->endWidget(); ?> 

    </div> 

    </div> 
    </div> 
</div> 
</div> 

控制器

public function actionLogin() 
{ 
    $model=new LoginForm; 

    // if it is ajax validation request 
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') 
    { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 

    // collect user input data 
    if(isset($_POST['LoginForm'])) 
    { 
     $model->attributes=$_POST['LoginForm']; 
     // validate user input and redirect to the previous page if valid 
     if($model->validate() && $model->login()) 
      $this->redirect(Yii::app()->user->returnUrl); 
    } 
    // display the login form 
    $this->render('login',array('model'=>$model)); 
} 
+0

你能更具體地描述你的問題嗎?很難準確理解你的問題。 – dev1234

+0

我只是想通過使用模態的標題登錄。可能有很多方法可以做到,但我認爲我所擁有的是不正確的。 – jaime

回答

0

按我的理解,請保持您的在這樣的循環內登錄腳本,以避免每次重新加載。

if(Yii::app()->user->isGuest and (Yii::app()->controller->id=='site' and Yii::app()->controller->action->id = 'login')){ 
    $this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'login')); 
    --- 
    $this->endWidget(); 
}else{ 
    // keep some log out or other script here. 
} 
+0

仍然無法登錄,但不能重新加載。 – jaime

+0

哦!請附上您的視圖/網站/ login.php與HTML表單標籤。我的意思是$ form = $ this-> beginWidget('CActiveForm',array(...))和$ this-> endWidget()。 OR

---

+0

好吧我更新了它... – jaime

0

下面是我對莫代爾登錄風格實現登錄:

組件/頭:

<?php 
Yii::import('zii.widgets.CPortlet'); 

class Header extends CPortlet 
{ 
    protected function renderContent() 
{ 

    $this->render('Header'); 
    } 
} 

組件/視圖/ header.php文件

<?php if(Yii::app()->user->isGuest){?> 
    <?php echo TbHtml::link('<i class="sign-in"></i>Sign in', '#', array(
             'data-toggle' => 'modal', 
             'data-target' => '#loginModal', 
             ));?> 
<?php } else{ echo 'Hi!'.$user; } ?> 

在視圖/佈局/ Main.php

<?php 
$model = new LoginForm; 
$this->widget('bootstrap.widgets.TbModal', array(
'id' => 'loginModal', 
'header' => '<h3 id="loginModal">Sign In</h3>', 
'content'=> $this->renderPartial('userlogin',array('model'=>$model),true), 
'remote' => $this->createUrl('site/userlogin'), 
'onShow'=>'function(){$("#login-form")[0].reset(); }', 
'show'=>false,)); ?> 

查看/網站/ Userlogin.php

<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'login-form', 
    //'helpType'=>'help-none', 
     'action'=>Yii::app()->createUrl('userlogin'), 
     //'enableAjaxValidation' => false, 
'enableClientValidation'=>true, 
'clientOptions' => array(
     'validateOnSubmit' => true, 
     'validateOnChange'=>false, 
     'afterValidate' => 'js:function(form, data, hasError) { 
      if (!hasError){ 
       str = $("#login-form").serialize() + "&ajax=login-form"; 

       $.ajax({ 
        type: "POST", 
        url: "' . Yii::app()->createUrl('site/userlogin') . '", 
        data: str, 
        dataType: "json", 
        beforeSend : function() { 
         $("#login").attr("disabled",true); 
        }, 
        success: function(data, status) { 
         if(data.authenticated) 
         { 
          window.location = data.redirectUrl; 
         } 
         else 
         { 
          $.each(data, function(key, value) { 
           var div = "#"+key+"_em_"; 
           $(div).text(value); 
           $(div).show(); 
          }); 
          $("#login").attr("disabled",false); 
         } 
        }, 
       }); 
       return false; 
      } 
     }', 
    ), 
));?> 
<?php echo $form->textFieldControlGroup($model,'username',array('span'=>5,'maxlength'=>255,'required'=>true)); ?> 
    <?php echo $form->passwordFieldControlGroup($model,'password',array('span'=>5,'maxlength'=>255,'required'=>true)); ?> 
<?php echo TbHtml::submitbutton('Sign In',array('class'=>'btn btn-primary')); ?> 
<?php $this->endWidget(); ?> 

這將通過Ajax

SiteController驗證:

$model=new LoginForm; 

       // if it is ajax validation request 
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') 
    { 
     $errors = CActiveForm::validate($model); 
     if ($errors != '[]') 
         { 
          echo $errors; 
          Yii::app()->end(); 
         } 
    } 
if(isset($_POST['LoginForm'])) 
    { 
     $model->attributes=$_POST['LoginForm']; 

     // validate user input and redirect to the previous page if valid 
     if ($model->validate() && $model->login()) { 
           if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') { 
$user_id = Yii::app()->user->id; 
            $arr = array(); 
            $record = Users::model()->findByPk($user_id); 
if ($record) { 
echo CJSON::encode(array('authenticated' => true, 
'redirectUrl' => CController::createUrl('users/trainerprofile'), 
"id" => $record->id,)); 
    Yii::app()->end(); 
    } 
}