Sergio_HW
Sergio_HW

Reputation: 155

yii toggle maintenance mode ON/OFF

I am trying to make a maintenance mode in my website, I want to put a button that only the admin can see to toggle the maintenance mode ON/OFF.

This allow to admin continue seeing the web but the rest of the visitors not.

I have read about catchAllRequest, creating a file and renaming it when is necessary, but I cant make it work.

I have tried underconstruct extension, but I dont know how to toggle ON/OFF.

This is what I have at the moment.

CONTROLLER:

public function actionIndex()
    {
        $location = Yii::app()->request->baseUrl.'/protected/config/maintenanceON.txt';
        $new = Yii::app()->request->baseUrl.'/protected/config/maintenanceOFF.txt';
        Yii::app()->session['secret'] = "password";
        rename($location,$new);
        $this->renderPartial("index");
    }

CONFIG/MAIN.PHP :

'catchAllRequest'=>file_exists(
        dirname(__FILE__).'/maintenanceON.txt') && 
            !(isset(Yii::app()->session['secret']) && Yii::app()->session['secret']=="password") ?
                array('maintenance/index') : null,

Thanks!

Upvotes: 3

Views: 2910

Answers (1)

Samuel Liew
Samuel Liew

Reputation: 79073

Put this in components/Controller.php:

public function beforeAction() {
    if(Yii::app()->params['maintenance'] == true && Yii::app()->user->id != 1) {
        throw new CHttpException(404, 'Under Maintenance');
    }
}

Put this in config/main.php:

'params' => array(
    'maintenance' => true,
),

Upvotes: 7

Related Questions