Yii: How to set up a non-public site (ie. users must log in to see content)
Posted by krussedull on September 15, 2009
I’m making several small web pages containing sensitive information that should only be accessed by registered users. This is how I did it using the PHP based Yii framework. My example starts right after you have run through the Creating First Yii Application tutorial.
First you have to make a new controller called BaseController (or whatever you prefer), so create the file protected/components/BaseController.php:
class BaseController extends CController {
protected function beforeAction($action) {
if (Yii::app()->user->isGuest && $this->id.'/'.$action->id !== 'site/login') {
Yii::app()->user->loginRequired();
}
return true;
}//function
}//class
Then in the file protected/controllers/SiteController.php you want to change the class that SiteController extends from CController to BaseController so that you end up with a class that starts like this:
class SiteController extends BaseController {
...
That’s it!
Note! Remember to make all of your future controllers extend BaseController and not CController.