Hide menu if not logged in

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

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

http://www.yiiframework.com/doc-2.0/guide-structure-filters.html

Thank you hrnair for the answer :)

I guess I should learn how to use filters…