我有一个基于quick-start设置的Zend Framework应用程序。
我已经让演示工作了,现在正在实例化一个新的模型类来做一些真正的工作。在我的控制器中,我想将配置参数(在application.ini中指定)传递给我的模型构造函数,如下所示:
class My_UserController extends Zend_Controller_Action
{
public function indexAction()
{
$options = $this->getFrontController()->getParam('bootstrap')->getApplication()->getOptions();
$manager = new My_Model_Manager($options['my']);
$this->view->items = $manager->getItems();
}
}
上面的示例允许访问选项,但似乎非常圆。有没有更好的方法来访问配置?
答案 0 :(得分:47)
我总是将以下init方法添加到我的引导程序中,以将配置传递到注册表中。
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions(), true);
Zend_Registry::set('config', $config);
return $config;
}
这会稍微缩短你的代码:
class My_UserController extends Zend_Controller_Action
{
public function indexAction()
{
$manager = new My_Model_Manager(Zend_Registry::get('config')->my);
$this->view->items = $manager->getItems();
}
}
答案 1 :(得分:22)
从版本1.8开始,您可以在Controller中使用以下代码:
$my = $this->getInvokeArg('bootstrap')->getOption('my');
答案 2 :(得分:6)
或者,您也可以创建一个包含所有应用程序信息的单例Application类,而不是使用Zend_Registry,其中包含允许您访问相关数据的公共成员函数。您可以在下面找到包含相关代码的代码段(它不会按原样运行,只是为了让您了解它是如何实现的):
final class Application
{
/**
* @var Zend_Config
*/
private $config = null;
/**
* @var Application
*/
private static $application;
// snip
/**
* @return Zend_Config
*/
public function getConfig()
{
if (!$this->config instanceof Zend_Config) {
$this->initConfig();
}
return $this->config;
}
/**
* @return Application
*/
public static function getInstance()
{
if (self::$application === null) {
self::$application = new Application();
}
return self::$application;
}
/**
* Load Configuration
*/
private function initConfig()
{
$configFile = $this->appDir . '/config/application.xml';
if (!is_readable($configFile)) {
throw new Application_Exception('Config file "' . $configFile . '" is not readable');
}
$config = new Zend_Config_Xml($configFile, 'test');
$this->config = $config;
}
// snip
/**
* @param string $appDir
*/
public function init($appDir)
{
$this->appDir = $appDir;
$this->initConfig();
// snip
}
public function run ($appDir)
{
$this->init($appDir);
$front = $this->initController();
$front->dispatch();
}
}
你的引导程序看起来像这样:
require 'Application.php';
try {
Application::getInstance()->run(dirname(dirname(__FILE__)));
} catch (Exception $e) {
header("HTTP/1.x 500 Internal Server Error");
trigger_error('Application Error : '.$e->getMessage(), E_USER_ERROR);
}
如果要访问配置,请使用以下命令:
$var = Application::getInstance()->getConfig()->somevar;
答案 3 :(得分:3)
在大多数ZF应用程序中,应用程序对象在全局范围内声明(请参阅使用public/index.php
创建的应用程序中的ZFW_DISTRIBUTION/bin/zf.sh
)。
这不完全是ZF方式,但您可以使用$GLOBALS['application']
访问该对象。
这有点像作弊,但如果你在表演之后,这可能是最快的选择。
$manager = new My_Model_Manager($GLOBALS['application']->getOption('my'));
答案 4 :(得分:1)
$this->getInvokeArg('bootstrap')->getOptions();
// or
$configDb = $this->getInvokeArg('bootstrap')->getOption('db');
答案 5 :(得分:0)
我在boostrap的开头我在require_once()的某个地方定义了一个简短的手:
function reg($name, $value=null) {
(null===$value) || Zend_Registry::set($name, $value);
return Zend_Registry::get($name);
}
在引导程序中我有一个:
protected function _initFinal()
{
reg('::app', $this->getApplication());
}
然后我可以通过使用来获取Application实例:
$app = reg('::app');
答案 6 :(得分:0)
访问配置选项的一种非常简单的方法是直接访问全局定义的 $ application 变量。
class My_UserController extends Zend_Controller_Action {
public function indexAction() {
global $application;
$options = $application->getOptions();
}
}