Home | About | Web Stories | View All Posts

23 Apr 2022

How to enable the maintenance mode in the Yii2 framework

There is general requirement in Yii2 framework - How to enable maintenance mode in Yii2? There are many way for enabling the maintenance mode in Yii2.

maintenance mode in Yii2

Yii2 provides a simplest way for enabling maintenance mode using catchAll property configuration in the Yii2 config file.

Yii2 maintenance mode tutorial with examples

While doing the work of web development, many times the developer has to do a live site update.

In the course of work, there is a possibility of showing broken pages on the website which can distract the user. To avoid this type of problem, we should always show a user friendly page on the website in which the user can get the correct information that the maintenance work is being done on the website.

Let's see and learn how we can do this in Yii2 framework using editing the configuration, controller and view file.

The Yii2 Maintenance Configuration

The "catchAll" property(yii\web\Application::$catchAll) take an array whose first element specifies a route to handle all incoming requests via a single action. We will create an action "actionOffline" in the Yii2 framework default controller - "SiteContorller". We use the following code in the Yii2 application configuration file.

File Path(sample) - C:\xampp\htdocs\yii2default\common\config\main-local.php
<?php
return [
   // "site" is the controller name, "offline" is the action name :: You can create your own controller and action.
    'catchAll' => ['site/offline'],
    'components' => [
        'db' => [
            ----------------
            ----------------
        ],
        'mailer' => [
            ----------------
            ----------------
        ],
    ],
];

The Yii2 Maintenance Controller

SiteController file path - C:\xampp\htdocs\yii2default\frontend\controllers\SiteController.php

With the above configuration, the action "actionOffline" will be used to handle all incoming requests. The following is the action code in the SiteController file -
  <?php
  ---------------
  ---------------
  class SiteController extends Controller
    {
        -----------------------
        -----------------------
        -----------------------
	 /**
     * Displays Maintenance Mode file.
     *
     * @return mixed
     */
    public function actionOffline()
    {
        return $this->render('maintenance');

    }
    }
We render or pass the maintenance view page in the action function actionOffline().

The Yii2 Maintenance View

The maintenance view page file path - C:\xampp\htdocs\yii2default\frontend\views\site\maintenance.php

The following html content code has been written for showing the site maintenance or offline message -
<?php
use yii\helpers\Html;

/** @var yii\web\View $this */

$this->title = 'WE ARE DOWN FOR MAINTENANCE';
?>
<div class="site-maintenance">

     <h1><?= Html::encode($this-">title); ?></h1>

     <p>
        Sorry, for inconveinience. We'll be back soon.<br /><small>If you have any questions, feel free to <a href="#">contact us</a></small>.
     </p>
</div>
Html content has some style also that has been written in the below code. All styles are written in the site css file at the below path -
Css File path - C:\xampp\htdocs\yii2default\frontend\web\css\site.css
  --------------
  --------------
  --------------
.nav > li > form > button.logout:focus {
    outline: none;
}

.site-maintenance{text-align: center;}
  --------------
  --------------
  --------------

Accessing the Yii2 maintenance page

With site navigation and footer strip

You need to check the site for maintenance page activation. In my case I used the below url -

http://localhost/yii2project/frontend/web/index.php?r=site/about

We got the below screenshot after hitting the above url in the browser.



You can click at the nav links - Home, Contact, Signup to check the maintenace page activation. The maintenance page will be shown constantly even you can click at the different nav link url. It is due to "catchall" entry in the Yii2 application configuration. It is simply passing all incoming requests via a single action - "acitonOffline" which has maintenance view contents.

Without site navigation and footer strip

We can show the maintenance page without top navigation and footer strip using the layouting assignment setting in the controller file for a specific action in the Yii2 framework. The following code snippet represent the layout assignment in Yii2 -
 // "blank" is the layout or template file name.
$this->layout = 'blank';
We will use Yii2 inbuilt blank template file for showing the maintenance page without header, navigation and footer content. The Yii2 framework provides a blank layout file in the BACKEND of the application. Below is the layout file path -

Blank layout file path - C:\xampp\htdocs\yii2default\backend\views\layouts\blank.php

We will copy this file into FRONTEND layout folder at below path -

C:\xampp\htdocs\yii2default\frontend\views\layouts\blank.php

Now we will add the Yii2 layout assignment entry into the action "actionOffline", like below -
  <?php
  ---------------
  ---------------
  class SiteController extends Controller
    {
        -----------------------
        -----------------------
        -----------------------
	 /**
     * Displays Maintenance Mode file.
     *
     * @return mixed
     */
    public function actionOffline()
    {
        // layout assignment code
        $this->layout = 'blank'; // "blank" is the template file name
        return $this->render('maintenance');

    }
    }
Now, refresh above page or enter the below url in the browser for checking the new maintenance page without header, navigation and footer -

http://localhost/yii2project/frontend/web/index.php?r=site/index

The new maintenance page will be shown without header, navigation and footer as like screen below -



Info: The debug toolbar in development environment will not work when the "catchAll" property is enabled. Also there will be repeation of the maintenance content. So you can get two view of the offline action.


Best Books for Web Development in PHP


1

Programming PHP : Creating Dynamic Web Pages

Programming PHP : Creating Dynamic Web Pages

Book Description

It's the new and updated version of this book - Fourth Edition - that teaches you everything you need to know to build effective web applications using the latest features in PHP 7.4.

The book explains language syntax, programming techniques, and other details using examples that illustrate both correct usage and common idioms.

For those with a working knowledge of HTML, the book contains many style tips and practical programming advice in a clear and concise manner to help you become a top PHP programmer.

The book teaches about fundamentals of the language including data types, variables, operators and flow control statements. It explores about functions, strings, arrays and objects.

It teaches to apply common web application techniques, such as form processing, data validation, session tracking, and cookies.

It teaches to interact with relational databases such as MySQL or NoSQL databases such as MongoDB. It also teaches to generate dynamic images, creating PDF files, and parsing XML files.

You can learn about secure scripts, error handling, performance tuning and other advanced topics in this book.

You can get a quick reference to PHP core functions and standard extensions in this book.

Book details

Format: Kindle Edition, Paperback
Rating: 4.6 out of 5
Author: Kevin Tatroe, Peter Macintyre
Print Length: 540 pages
Publication Date: 27 March 2020
Publisher: O′Reilly, 4th edition
Kindle Price: Rs. 1,567.50*
Paperback Price: Rs. 3,614.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


2

PHP Web Development with Laminas

PHP Web Development with Laminas

Book Description

This book teaches how to build fully secure and functional e-commerce applications with PHP using the next generation Zend Framework-Laminas. You can learn to develop modern object-oriented applications with PHP by using Test-Driven Development (TDD) and Behavior-Driven Development (BDD) aided by mature reusable components.

This book provides a practical approach to equip you with the knowledge of the Laminas framework needed to start building web applications based on reuse of loosely coupled components.

You will learn how to build the basic structure of a PHP web application divided into layers. You can understand the MVC components of Laminas and be able to take advantage of the Eclipse platform as a method to develop with Laminas.

Books teach to explore how object-relational mapping is implemented with Laminas-DB, behavior-driven development concepts to sharpen your skills, how to build complete models and reusable components, practice testing How to Create HTML Forms With Laminas-Forms.

By the end of this web development book, you will be able to build completely secure MVC applications in PHP language using Laminas.

Book details

Format: Kindle Edition
Author: Flávio Gomes da Silva Lisboa
Text-to-Speech: Enabled
Enhanced typesetting: Not Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Publication Date: 9 December 2022
Publisher: Packt Publishing
Kindle Price: Rs. 750.74*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


3

Getting started with Laravel 9, master the most popular PHP framework

Getting started with Laravel 9, master the most popular PHP framework

Book Description

This book is for all those who want to build their first application in Laravel 9. This book provides a step-by-step introduction to the writing framework, gets to know its most relevant aspects and focuses above all on practice.

Using this book you will be able to build any basic application with the framework. There are total 19 chapters in this book. Using this book, you will be able to know what are the required software to install Laravel for different operating systems.

In this book you can learn - project creation, database configuration, routing, view controllers, redirection, directive and templating engines in the form of blades, model building, CRUD applications etc.

You can learn to perform common eloquent operations that can be applied to databases using query builders. You can learn how to generate test data using classes.

You can also learn the file upload process. You can learn how to use REST APIs through CRUD type applications in VU3 using Axios requests and web components with Oruga UI.

You can also learn how to configure Browsersync with Laravel to automatically reload applications. You can learn how to protect an app in Vue with the login required to access its various modules using SPA authentication or Laravel Sanctum tokens.

Book details

Format: Kindle Edition
Rating: 1 out of 5
Author: Andrés Cruz Yoris
Print Length: 453 pages
Publication Date: 8 May 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 449.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


4

Learning Drupal as a framework: Your guide to custom Drupal 9. Full code included

Learning Drupal as a framework: Your guide to custom Drupal 9. Full code included

Book Description

This book uses PHP> 7.4. This course teaches you about the advanced concepts of Drupal 9, object-oriented PHP and Symfony components.

After the course, you will be able to build a variety of robust and scalable software solutions.

This book discusses advanced topics such as custom entities, entity forms, access controls, events, caching, workflows, and more when building real software.

It gives you powerful and ready-to-use snippets for your next Drupal project with +2400 lines of custom code.

Book details

Format: Kindle Edition
Rating: 5 out of 5
Author: Stef Van Looveren
Print Length: 282 pages
Publication Date: 17 July 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 449.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


5

Getting started with CodeIgniter 4

Getting started with CodeIgniter 4

Book Description

This book is for anyone who wants to build their first applications in CodeIgniter 4 a popular PHP framework, this writing offers a step-by-step introduction to the framework, knowing the most relevant aspects of it, and is focused above all on practice.

The book is aimed at those people who want to learn something new, learn about a framework that has very little documentation, who want to improve a skill in web development, who want to grow as a developer, and who want to continue scaling their path with other frameworks superior to this one.

This book has a total of 15 chapters and consists of explanations and practices. It teaches you how to run the framework, how to configure a database, how to create the first components, how to use of migrations for table management, working with the MVC, how to prepare CRUD application, how to use the routes, grouped routes, their options, and the different types.

You can learn about the use of the session and also of the flash session to save data and present it to the user. You can learn to manage views in a reusable way. You can learn about how to work with HTML forms and apply validations from the server side in CodeIgniter. You can learn about the authentication module with the login interface, and how to build a Rest Api type CRUD that can be consumed with JSON or XML.

You can also learn about generating test data with seeders, how to handle the relational schema of the database, how to do uploading files in the application, how to use libraries and help functions, how to integrate the PayPal platform, etc.

Book details

Format: Kindle Edition
Author: Andres Cruz
Print Length: 328 pages
Publication Date: 13 May 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 319.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


Tags : ,
Aashutosh Kumar Yadav

By Aashutosh Kumar Yadav

He is a PHP-based UI/Web designer and developer by profession and very interested in technical writing and blogging. He has been writing technical content for about 10 years and has proficient in practical knowledge and technical writing.
@www.infotokri.in

0 comments:

Post a Comment