Zend Framework - 访问自定义配置文件

时间:2012-03-12 18:48:04

标签: php zend-framework

我创建了一个名为config.xml的自定义XML配置文件,并将其放在Zend Framework的configs目录中。我想使用Zend_Config_Xml在我的一个控制器中使用它。我所拥有的不起作用,它说“发生错误。应用程序错误”。如何从控制器读取自定义XML配置文件?到目前为止,这就是我在控制器中所拥有的:

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $config = new Zend_Config_Xml('config.xml', 'staging');
        echo $config->host;
    }
}

2 个答案:

答案 0 :(得分:3)

可能只是你需要修复的路径:

$config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/config.xml', 'staging');

如果没有,请检查错误日志以查看实际的错误消息。

编辑:要在引导程序中执行此操作,最简单(尽管可能不是最好)的方法是添加新的资源方法并将配置对象存储在注册表中。将其添加到您的引导类:

protected function _initCustomConfig()
{
    $config = new Zend_Config_Xml('config.xml', 'staging');
    Zend_Registry::set('config', $config);

    return $config;
}

然后您可以使用以下方式访问它:

$config = Zend_Registry::get('config');

答案 1 :(得分:1)

如果您在本地调试此问题,请首先在application.ini的开发部分中添加以下命令,以启用更好的错误报告:

phpSettings.error_reporting         = E_ALL
phpSettings.display_startup_errors  = 1
phpSettings.display_errors          = 1

默认情况下,zend框架不会显示内部错误。

如果您要加载Zend_Config文件,最好使用绝对路径加载它。

public function indexAction()
    {
        // action body
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/config.xml', 'staging');
        echo $config->host;
    }