訪問應用程序的資源,你需要訪問您Zend_Application,你在你的index.php文件創建的一個的引導對象。你可以將它們中的任何一個存儲在任何可以從任何地方訪問的地方,比如Zend_Registry。 假設這是你的公共/ index.php文件:
// suppose you have defined the APPLICATION_PATH constant to the application
// directory of your project. and the APPLICATION_ENV defines your environment
// like production, development or testing.
// create a config for your application configurations, defining your resources
$config = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/application.ini',
APPLICATION_ENV
);
// this is the application object
$application = new Zend_Application(APPLICATION_ENV, $config);
// now resources are bootstrapping
$application->bootstrap();
$bootstrap = $application->getBootstrap();
// store them somewhere in registry, so you could access them from everywhere
// like controllers, models, etc.
Zend_Registry::set('application', $application);
在控制器
現在(或任何其他代碼,如模型,視圖等),你可以使用訪問該應用程序對象:
$application = Zend_Registry::get('application'):
/*@var $application Zend_Application*/
$bootstrap = $application->getBootstrap();
// now you could access any resources from the $bootstrap object
$db = $bootstrap->getResource('db');
$log = $bootstrap->getResource('log);
// now $db is a Zend_Db object bootstrapped from your application config
這是訪問的應用程序引導的一般方式。但是在你的控制器中有另一種方法來訪問引導對象(以及資源),這就是使用控制器對象的'''getInvokeArg('bootstrap')''。因此,在你的控制器,你可以這樣做:
$bootstrap = $this->getInvokeArg('bootstrap');
$db = $bootstrap->getResource('db');
// now $db is a Zend_Db object bootstrapped from your application config