Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework Zend Framework-从控制器读取Application.ini值_Zend Framework_Web Applications_Configuration - Fatal编程技术网

Zend framework Zend Framework-从控制器读取Application.ini值

Zend framework Zend Framework-从控制器读取Application.ini值,zend-framework,web-applications,configuration,Zend Framework,Web Applications,Configuration,我正在尝试将我的Google Maps API密钥存储在application.ini文件中,我希望能够在需要时从控制器中读取它。如何从控制器读取application.ini中的值 谢谢 通过FrontController或 // bootstrap $front->setParam('GMapsApiKey', 123456); // controller $this->getFrontController()->getParam('GMapsApiKey'); 或注册

我正在尝试将我的Google Maps API密钥存储在application.ini文件中,我希望能够在需要时从控制器中读取它。如何从控制器读取application.ini中的值


谢谢

通过FrontController或

// bootstrap
$front->setParam('GMapsApiKey', 123456);

// controller
$this->getFrontController()->getParam('GMapsApiKey');
或注册处:

// bootstrap
Zend_Registry::set('GMapsApiKey', '123456');

// controller
Zend_Registry::get('GMapsApiKey');
但是,由于对Google Maps API的访问是在模型中发生的,因此您的控制器不需要知道API密钥。

这篇文章可能会对您有所帮助。作者解释了如何从application.ini文件中检索gmail连接参数

您可能要尝试:

$bootstrap = $this->getInvokeArg('bootstrap');
$aConfig = $bootstrap->getOptions();
googleMapsKey = $aConfig['your_resource']['your_key'];

我倾向于在引导中这样做,然后就像Gordon提到的那样,将它扔到注册表中。然后,您可以从注册表中的任何位置获取它

$this->config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
Zend_Registry::set('config', $this->config);

此外,在他看来,控制器可能是错误的位置,除非您使用多个键访问API,我想,但即使这样,您也应该能够将逻辑放在模型中,从注册表中选择一个键。

有没有办法在需要时读取它?我试图避免在每个请求的引导过程中都这样做,因为它只在一小部分请求中需要pages@Ryanbootstrap和application.ini无论如何都会在每个请求上被调用,因此不这样做可能只会获得一微秒。如果按需加载application.ini,则会有一个额外的I/O请求。那慢多了。谢谢戈登,这很有道理。不过,我对模型中的情况感到困惑。我曾计划为谷歌地图创建一个提供者(又名“模型”),但最终我需要它在视图中,以便通过JavaScript API请求将其传递给谷歌。@Ryan我还没有使用过任何谷歌API或Zend_GData类,但这里有一个屏幕广播和示例代码:-可能会有帮助。但它们定义了控制器中的键。如果您只需要在视图中使用API键,也可以在初始化视图时将其设置为视图。如果您在前端控制器中,这是一个很好的解决方案。这里有一整套选项:
$this->config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
Zend_Registry::set('config', $this->config);