Getting route / Login Controller and View

Hi all, i’m working on implementing a login controller & view for my website, which uses the Yii Framework. I want to make sure all users do login whenever they access the site.

I wasn’t sure where to add the code to check this, so i did put it in components/Controller.php within the constructor. [b]My first question would be is this is the right place for checking if a user is logged in or not (meaning that this verification should affect the entire site: all controllers/views)

[/b]

Now, since i had to use components/Controller.php i wasn’t within any controller where i could call $this->getRoute() to know which route i was in, so i did something like this:




// Redirect to login if the user hasn't authenticated yet

if (Yii::app()->getUser()->getIsGuest() == true) {

	// We aren't within any Controller yet so i can't use CController::getRoute to know which 

	// route we are following.

	if ($_GET['r'] != 'login2') {

		$this->redirect('/?r=login2');

	}

}



Is it “legal” to use $_GET[‘r’] to know which route we are in? I’m thinking about what would happen in the future if for whatever reason, the route stops being called ‘r’.

Thanks in advance

best regards

Fernando Gabrieli

No, you can use the accessControl filter in your controller, see documentation for details.