Skip to content
Maintenance mode extension for Yii framework 2.0
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src remove print_r from isAllowed method Aug 3, 2019
LICENSE Codestyle & PhpDoc Nov 19, 2017
README.md Update README.md Jul 12, 2019
composer.json 1.0.0 merge Jul 12, 2019

README.md

Yii2 Maintenance mode component

Latest Stable Version Total Downloads License

Install

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\URIFilter',
                    'uri' => [
                        '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',
            'fileName' => 'myfile.ext'
            'directory' => '@mypath'
        ]
    ]
]

Filters

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 URIs filter. Your can allow debug panel URI.
                [
                    'class' => 'brussens\maintenance\filters\URIFilter',
                    'uri' => [
                        '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;
    }
}

Set maintenance mode by console or dashboard

Add to your console or common config file:

'container' => [
    'singletons' => [
        'brussens\maintenance\StateInterface' => [
            'class' => 'brussens\maintenance\states\FileState',
            'fileName' => 'myfile.ext',
            'directory' => '@mypath'
        ]
    ]
]

Create controller:

class MaintenanceController extends Controller
{
    /**
     * @var StateInterface
     */
    protected $state;
    
    public function __construct(string $id, Module $module, StateInterface $state, array $config = [])
    {
        $this->state = $state;
        parent::__construct($id, $module, $config);
    }
    
    public function actionEnable()
    {
        $this->state->enable();
    }
    public function actionDisable()
    {
        $this->state->disable();
    }
}

Now you can set mod by command:

php yii maintenance/enable
php yii maintenance/disable
You can’t perform that action at this time.