1
我開發自定義Joomla 2.5後端組件。我想知道在我的組件範圍內進行變量全局的最佳實踐。我應該在組件主控制器中放置聲明嗎?Joomla 2.5變量全局組件範圍
class MyController extends JController
{
/**
*
* @var object External database object
*/
public $dbExternal;
function __construct($config = array())
{
$this->dbExternal = $this->_getDbInstance();
JTable::setDbo($this->dbExternal);
parent::__construct($config);
}
/**
* Display task
*
* @return void
*/
function display($cachable = false, $urlparams = false)
{
// set default view if not set
JRequest::setVar('view', JRequest::getCmd('view', 'My'));
// call parent behavior
parent::display($cachable);
// Set the submenu
MyHelper::addSubmenu('messages');
}
/**
* Getting instance of the database
*
* @return instance $dbExternal Return db instance on success
*/
private function _getDbInstance()
{
$params = $this->_getDbParams();
$dbExternal = JDatabase::getInstance($params);
if ($dbExternal->getErrorNum() > 0)
{
JError::raiseWarning(500, 'JDatabase::getInstance: '.
$dbExternal->getErrorMsg());
$this->isError = true;
}
return $dbExternal;
}
/**
* Getting external db parameters
*
* @return array $dbParamsArr Return db params on success
*/
private function _getDbParams()
{
$dbParamsArr = array();
$params = JComponentHelper::getParams('com_my');
$dbParamsArr['host'] = $params->get('db_host');
$dbParamsArr['user'] = $params->get('db_user');
$dbParamsArr['password'] = $params->get('db_password');
$dbParamsArr['database'] = $params->get('db_database');
$dbParamsArr['driver'] = $params->get('db_driver');
return $dbParamsArr;
}
}
所以我想在任何其他組件控制器或模型中達到$ dbExternal。有任何想法嗎?
我有一個想法,把它放到組件助手類,並在其他組件的地方使用它。 – Bounce