How to check is DB Connection befor run any query

I working with yii in an integrator.

Some mistakes started to happen.

Looked at what was happening, got to that i had shut down the database the day before.

The problem is that Yii only populates \Yii::$app->db when the first command execution is done to the database

( i test this ).

I need to check if the database is available before anything is done, so i can notify the requestor that can not attend to it, due to internal problems.

How i do that?

thanks in advance.


try {

  // DB requests

} catch (\yii\db\Exception $e) {

  // notify requestor, shut down

}

Ok.

But in my case is before call the run method in app.

With the knowledge that EVERY request for a page would run this check, I would look into the php function for db connections.

I remember in a Yii1.1 app I had to do some stuff between App instantiation and running. code looked something like this




// instantiate and configure the application

$myApp = (new yii\web\Application($config));

// ---do stuff---

$myApp->run();



There are some things that are available from the $myApp-> methods, not sure what. You could do your check if the db server/table is available, then possibly redirect to a "we are closed" route if not available. This would give you the ability easily have the page look like the rest of the site. Maybe change the layout for no NavBar.

Just some thoughts.