Install this extension: https://github.com/brussens/yii2-maintenance-mode
Add to your config file:
'bootstrap' => ['log', 'maintenanceMode'],
...
'components' => [
'maintenanceMode' => [
'class' => 'brussens\maintenance\MaintenanceMode',
],
...
],
Modify according to your needs. Make sure to indicate an allowed user or role. Also create a default route for displaying maintenance mode text. Place in route.'maintenanceMode' => [
// Component class namespace
'class' => 'brussens\maintenance\MaintenanceMode',
// Mode status
'enabled' => true,
// Route to action
'route' => 'maintenance/index',
// Allowed user names
'users' => [
'BrusSENS',
],
Add to your console config file. This is useful when you cannot access your UI:
'bootstrap' => ['log', 'maintenanceMode'],
...
'components' => [
'maintenanceMode' => [
'class' => 'brussens\maintenance\MaintenanceMode',
],
...
],
Add buttons on UI. You can use session[‘maintenance’] to store current state.
echo '<span style = "color:white;font-weight:bold;">MAINTENANCE</span><br/>';
if (Yii::$app->session['maintenance'] == NULL){
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button']);
echo Html::a('OFF', ['/site/disable'], ['class'=>'btn btn-primary grid-button']);
}
if (Yii::$app->session['maintenance'] == 'OFF'){
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button']);
echo Html::a('OFF', ['/site/enable'], ['class'=>'btn btn-primary grid-button', 'disabled' => 'disabled']);
}else if (Yii::$app->session['maintenance'] == 'ON') {
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button', 'disabled' => 'disabled']);
echo Html::a('OFF', ['/site/disable'], ['class'=>'btn btn-primary grid-button']);
}
Add to controller. Make sure to change the state stored in maintenance when a function is called:
public function actionEnable()
{
Yii::$app->session['maintenance'] = 'ON';
Yii::$app->maintenanceMode->enable();
return $this->goHome();
}
public function actionDisable()
{
Yii::$app->session['maintenance'] = 'OFF';
Yii::$app->maintenanceMode->disable();
return $this->goHome();
}
Either run
php composer.phar require --prefer-dist brussens/yii2-maintenance-mode "*"
or add
"brussens/yii2-maintenance-mode": "*"
to the require section of your composer.json
file.
Add to your config file:
'bootstrap' => [
'brussens\maintenance\Maintenance'
],
...
'container' => [
'singletons' => [
'brussens\maintenance\Maintenance' => [
'class' => 'brussens\maintenance\Maintenance',
// Route to action
'route' => 'maintenance/index',
// Filters. Read Filters for more info.
'filters' => [
[
'class' => 'brussens\maintenance\filters\RouteFilter',
'routes' => [
'debug/default/toolbar',
'debug/default/view',
'site/login',
]
]
],
// HTTP Status Code
'statusCode' => 503,
//Retry-After header
'retryAfter' => 120 // or Wed, 21 Oct 2015 07:28:00 GMT for example
],
'brussens\maintenance\StateInterface' => [
'class' => 'brussens\maintenance\states\FileState',
// optional: use different filename for controlling maintenance state:
// 'fileName' => 'myfile.ext',
// optional: use different directory for controlling maintenance state:
// 'directory' => '@mypath',
]
]
]
You can use filters for allow excepts:
'container' => [
'singletons' => [
'brussens\maintenance\Maintenance' => [
'class' => 'brussens\maintenance\Maintenance',
// Route to action
'route' => 'maintenance/index',
// Filters. Read Filters for more info.
'filters' => [
//Allowed routes filter. Your can allow debug panel routes.
[
'class' => 'brussens\maintenance\filters\RouteFilter',
'routes' => [
'debug/default/toolbar',
'debug/default/view',
'site/login',
]
],
// Allowed roles filter
[
'class' => 'brussens\maintenance\filters\RoleFilter',
'roles' => [
'administrator',
]
],
// Allowed IP addresses filter
[
'class' => 'brussens\maintenance\filters\IpFilter',
'ips' => [
'127.0.0.1',
]
],
//Allowed user names
[
'class' => 'brussens\maintenance\filters\UserFilter',
'checkedAttribute' => 'username',
'users' => [
'BrusSENS',
],
]
],
]
]
]
You can create custom filter:
class MyCustomFilter extends Filter
{
public $time;
/**
* @return bool
*/
public function isAllowed()
{
return (bool) $this->time > 3600;
}
}
Add to your console or common config file:
'container' => [
'singletons' => [
'brussens\maintenance\StateInterface' => [
'class' => 'brussens\maintenance\states\FileState',
// optional: use different filename for controlling maintenance state:
// 'fileName' => 'myfile.ext',
// optional: use different directory for controlling maintenance state:
// 'directory' => '@mypath',
]
]
],
'controllerMap' => [
'maintenance' => [
'class' => 'brussens\maintenance\commands\MaintenanceController',
],
],
Now you can set mode by command:
php yii maintenance/enable
php yii maintenance/disable
0 Comments