Search code examples
yiiyii-extensionsyii-components

How can integrate login form of YII user module in layout/main.php


How can integrate login form of YII user module in layout/main.php

I would like to know how can use login form and registration form in Yii user module into layouts/main.php like facebook index page.If anyone can please give me the details..

Thanks in advance...


Solution

  • You need to use a widget. Here's a short crash course on making a widget:

    1. Create LoginFormWidget.php here: /protected/components/LoginFormWidget.php

    2. Create the view folder for your widgets here /protected/components/views/

    3. Create the view file for your LoginFormWidget, like so: /protected/components/views/loginFormWidget.php

    4. Put the following code in your LoginFormWidget.php file:

      class LoginFormWidget extends CWidget
      {
          /**
           * Is called when $this->beginWidget() is called
           */
          public function init()
          {
      
          }
      
          /**
           * Is called when $this->endWidget() is called
           */
          public function run()
          {
              $model = new LoginForm;
              $this->render('loginFormWidget', array('model'=>$model));
          }
      }
      
    5. Add teh view file, make sure that you specify which action will be used:

      <div class="form"> 
          <?php $form=$this->beginWidget('CActiveForm', array(
              'id'=>'login-form',
              'enableAjaxValidation'=>true,
              'clientOptions'=>array('validateOnSubmit'=>true),
              'htmlOptions'=>array('class'=>'customClass'),
              'action' => array('site/login'), // this is is the action that's going to process the data
          )); ?>
      
              <?php echo $form->labelEx($model,'email'); ?>
              <?php echo $form->textField($model,'email'); ?>
              <?php echo $form->error($model,'email'); ?>
      
              <?php echo $form->labelEx($model,'password'); ?>
              <?php echo $form->passwordField($model,'password'); ?>
              <?php echo $form->error($model,'password'); ?>
      
              <?php echo CHtml::submitButton('Log in!'); ?>
      
          <?php $this->endWidget(); ?>
      </div>
      
    6. Finally, call the widget in your main.php file, like so:

    <?php $this->widget('LoginFormWidget'); ?>

    Good luck!