Question

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...

Was it helpful?

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!

OTHER TIPS

If you have a separate login form then you use that in your Cmenu. You can also make a widget for that but you can also render form. If you want to render form then you can do like this

$form=$this->renderPartial('loginForm');

Now in your Cmenu widget you have to change the property 'encodeLable' to false like

$this->widget('zii.widgets.CMenu', array(
             'encodeLabel'=>false,
    'items'=>array(
        array('label'=>$form, 'url'=>array('site/index')),
    ),
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top