rdo
rdo

Reputation: 3982

Is there zend bootstrap analog in yii?

I want to try db connection to check is db available. In zend I can place my code in boostrap file and wrap it in try catch.

How to implement this in yii? Is in yii analog of zend boostrap?

UPD: db is mongo, yii extention for working with db is a directmongosuite

Upvotes: 0

Views: 304

Answers (2)

rdo
rdo

Reputation: 3982

Seems that I find appropriate solution:

  1. Need to prohibit auto connect in config file:

    'components' => array( 'edms' => array( 'class' => 'EDMSConnection', 'dbName' => 'homeweb', 'server' => 'mongodb://localhost:27017', 'options' => array('connect' => false) ) )

  2. all controllers should extend one custom controller (BaseController for example).

  3. Need to write own public function beforeAction method where I can add boostrap code.

    class BaseController extends CController
    {
        public $layout = '//layouts/main';
        public $navigationMenu = array();
        public $breadcrumbs = array();
    
        public function beforeAction($action)
        {
            try {
                Yii::app()->edmsMongo()->connect();
            } catch (Exception $e) {
                die('Cannot connect to the database server. Please Try again later.');
            }
            $isGuest = Yii::app()->user->isGuest;
            $this->navigationMenu = $this->_getNavigationMenu($isGuest);
            return parent::beforeAction($action);
        }
    
  4. In the beforeAction method need to add return true or execute parent's method.

Upvotes: 1

Blizz
Blizz

Reputation: 8408

The bootstrap in yii is pretty much the index.php file under public_html or the yiic.php file (for command line applications).

You will probably have to separate the creating of the application instance and running it (by default it does both on 1 line), so you can do your try/catch between the calls.

Just try to fetch the app component, the mongo plugin will throw an exception if it can't open the connection:

try
{
    Yii::app()->mongoDb;
}
...

or Yii::app()->getComponent('mongoDb');

Upvotes: 0

Related Questions