Is it possible and advisable to move the creation of a MySQL database object from the index.php (FrontController) to a Bootstrap class in PHP frameworks like Zend?
Moving the creation of a MySQL database object from the index.php (FrontController) to a Bootstrap class in PHP frameworks like Zend is both possible and advisable. By doing so, you can centralize the database connection logic and make it easier to manage and reuse throughout your application. This approach follows the principle of separation of concerns and helps in keeping your codebase clean and organized.
// Bootstrap class in Zend Framework
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDb()
{
$config = $this->getOption('db');
$db = Zend_Db::factory($config['adapter'], $config['params']);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
return $db;
}
}
// index.php
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
Keywords
Related Questions
- How can PHP and JavaScript be effectively combined to provide a seamless experience for website visitors in terms of design selection based on screen resolution?
- What is the purpose of using bind_param in a MySQLi login query in PHP?
- What are the advantages and disadvantages of using array indexes to access data from a database query result in PHP?