Alfs blog

Swirling thoughts from my mindsphere

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.