Add Yii2 Application in Maintenance Mode or serve all requests to Yii application using a single action

When you want to add a Yii application in maintenance mode or you want every route of your application to be served by a single controller action, you can use the catchAll property. You can specify this property in your web.php config file as follows:

[
    'catchAll' => [
        'offline/notice',
        'param1' => 'value1',
        'param2' => 'value2',
    ],
]

The configuration is an array whose first element specifies the route of the action. The rest of the array elements (key-value pairs) specify the parameters to be bound to the action.

Note: The Debug panel on development environment will not work when this property is enabled.

If you want to avoid some URL’s from the maintenance so that you can test certain functionality, you may do so in the following way. Write the below code in your web.php file.

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                 'site/test'=>'site/test',
                 'site/test2/param_name'=>'site/test2',
                 'site/test3/param_name'=>'site/test3',
                 '<url:(.*)>'=>'site/maintenance',
            ],
        ],

The above code sets the redirect rules. Here, all the URL’s except for the lines written above '<url:(.*)>'=>'site/maintenance', will be redirected to site/maintenance. The three lines above '<url:(.*)>'=>'site/maintenance', will continue to function as intended.

Note: You should add exceptions to the rules sections such calls made through AJAX or JavaScript otherwise they won’t work. e.g. 'site/test3/param_name'=>'site/test3', this was the call made through AJAX on the view rendered by 'site/test'.

Running low on budget? Get the best Shared hosting at an Affordable Rate!