Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Inlcuding Log In Form In Main.php Layout Script

$
0
0
Hello,

I need to have Log in form available for guest users throughout my site on all pages that are exposed to public. I have implemented one solution, but I am concerned is it a good practice to include this kind of logic in main layout file. What I did is that I made a condition to display log in form if user is a guest. Before I started using MVC approach, I would create different main layout files. One would be rendered for guest user, one for admin and so on. But I am not sure what would be the best practice to achieve this separation of content based on permissions. Right now I have only one main layout file, but problem is that it has to be changed dynamically and I am not sure what is the best practice to do that? Here is the part of main.php that isrendering login form:

		&lt!-- LOG IN -->
		<?php 
		if (Yii::app()->user->isGuest)
		{
		?>
		<div id="login">
		
			<h4>Log in</h4>
			
			<?php $model = new LoginForm; ?>
			
			<div class="form">
			<?php $form=$this->beginWidget('CActiveForm', array(
				'id'=>'login-form',
				'action'=>array('site/login'),
				'enableClientValidation'=>true,
				'clientOptions'=>array(
					'validateOnSubmit'=>true,
				),
			)); 
			?>

				<div class="row">
					<?php echo $form->labelEx($model,'username'); ?>
					<?php echo $form->textField($model,'username'); ?>
					<?php echo $form->error($model,'username'); ?>
				</div>

				<div class="row">
					<?php echo $form->labelEx($model,'password'); ?>
					<?php echo $form->passwordField($model,'password'); ?>
					<?php echo $form->error($model,'password'); ?>

				</div>

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

			<?php $this->endWidget(); ?>
			</div>
			
		</div>
		
		<?php } ?>
		&lt!-- ENDS LOG IN -->	


So first I had to make this if condition, then load model and form action manually. Is there any better way to do all of this ?

Thanks

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>