php 如何从控制器访问 Zend Framework 应用程序的配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1680939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access the configuration of a Zend Framework application from a controller?
提问by Adam Franco
I have a Zend Framework application based on the quick-startsetup.
我有一个基于快速启动设置的 Zend Framework 应用程序。
I've gotten the demos working and am now at the point of instantiating a new model class to do some real work. In my controller I want to pass a configuration parameter (specified in the application.ini) to my model constructor, something like this:
我已经让演示工作了,现在正在实例化一个新的模型类来做一些实际的工作。在我的控制器中,我想将一个配置参数(在 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();
}
}
The example above does allow access to the options, but seems extremely round-about. Is there a better way to access the configuration?
上面的示例确实允许访问选项,但似乎非常迂回。有没有更好的方法来访问配置?
回答by Stefan Gehrig
I always add the following init-method to my bootstrap to pass the configuration into the registry.
我总是将以下 init-method 添加到我的引导程序中以将配置传递到注册表中。
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions(), true);
Zend_Registry::set('config', $config);
return $config;
}
This will shorten your code a little bit:
这将稍微缩短您的代码:
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();
}
}
回答by yarson
Since version 1.8 you can use the below code in your Controller:
从 1.8 版开始,您可以在控制器中使用以下代码:
$my = $this->getInvokeArg('bootstrap')->getOption('my');
回答by wimvds
Alternatively, instead of using Zend_Registry you could also create a singleton Application class that will contain all application info, with public member functions that allow you to access the relevant data. Below you can find a snippet with relevant code (it won't run as is, just to give you an idea how it can be implemented) :
或者,除了使用 Zend_Registry,您还可以创建一个包含所有应用程序信息的单例 Application 类,以及允许您访问相关数据的公共成员函数。您可以在下面找到带有相关代码的片段(它不会按原样运行,只是为了让您了解如何实现它):
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();
}
}
Your bootstrap would look like this :
你的引导程序看起来像这样:
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);
}
When you want to access the configuration you would use the following :
当您想访问配置时,您将使用以下内容:
$var = Application::getInstance()->getConfig()->somevar;
回答by Chris Trahey
In most ZF apps, the application object is declared in the global scope (see public/index.phpin apps created with ZFW_DISTRIBUTION/bin/zf.sh).
在大多数 ZF 应用程序中,应用程序对象是在全局范围内声明的(请参阅public/index.php使用 创建的应用程序ZFW_DISTRIBUTION/bin/zf.sh)。
It's not exactly the ZF way, but you can access the object with $GLOBALS['application'].
It kinda feels like cheating, but if you're after performance, this will likely be the quickest option.
这不完全是 ZF 方式,但您可以使用$GLOBALS['application']. 这有点像作弊,但如果你追求表演,这可能是最快的选择。
$manager = new My_Model_Manager($GLOBALS['application']->getOption('my'));
回答by SLIM
$this->getInvokeArg('bootstrap')->getOptions();
// or
$configDb = $this->getInvokeArg('bootstrap')->getOption('db');
回答by Milan
A really simple way to access the configuration options is by directly accessing the globally defined $applicationvariable.
访问配置选项的一种非常简单的方法是直接访问全局定义的$application变量。
class My_UserController extends Zend_Controller_Action {
public function indexAction() {
global $application;
$options = $application->getOptions();
}
}
回答by XUE Can
I've define a short hand in some place I require_once() in the beginning of boostrap:
我在 boostrap 开头的某个地方定义了一个简写:
function reg($name, $value=null) {
(null===$value) || Zend_Registry::set($name, $value);
return Zend_Registry::get($name);
}
and in the bootstrap I have a:
在引导程序中,我有一个:
protected function _initFinal()
{
reg('::app', $this->getApplication());
}
then I can get the Application instance anywhere by use:
然后我可以通过使用在任何地方获取 Application 实例:
$app = reg('::app');

