Yii Framework Forum: Hide menu if not logged in - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Hide menu if not logged in

#1 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 12 December 2014 - 03:43 AM

Hi all,

I wanted to make login page as homePage and hide menu section if not logged in.
I did the following:

main.php (layout)

 <?php if(!Yii::app()->user->isGuest): ?>
   <div id="mainMbmenu">
     ...menu...
   </div><!-- mainmenu -->
 <?php endif;?>


In SiteController.php

	public function actionIndex()
	{
		if(!Yii::app()->user->isGuest){
		   $this->render('index');
		}
		else 
		   $this->redirect(array('site/login'));
	}


        public function actionLogin()
	{
		$model=new LoginForm;
		....
			if($model->validate() && $model->login())
				$this->redirect(array('site/index'));
		....
	}


This works fine (as I wanted) but my question is:
Is this the correct way to do it?

Thanks
0

#2 User is offline   hrnair 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 233
  • Joined: 09-December 12

Posted 12 December 2014 - 03:52 AM

You can see the use of this in default controller code.

http://www.yiiframew...re-filters.html
0

#3 User is offline   erand 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 10-December 14

Posted 12 December 2014 - 05:12 AM

Thank you hrnair for the answer :)
I guess I should learn how to use filters...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users