Animal Rights
Animal Rights

Reputation: 9397

Zend Framework - Accessing custom configuration files

I created a custom XML configuration file called config.xml and placed it in the configs directory in the Zend Framework. I want to use it in one of my controllers using Zend_Config_Xml. What I have is not working, and it says "An error occurred. Application error". How do I read in a custom XML config file from a controller? This is what I have in my controller so far:

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;
    }
}

Upvotes: 1

Views: 1718

Answers (2)

Tim Fountain
Tim Fountain

Reputation: 33148

Probably just the path you need to fix:

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

if not, check the error log to see what the actual error message is.

Edit: To do this in the bootstrap, the easiest (although perhaps not best) way is to add a new resource method and store the config object in the registry. Add this to your bootstrap class:

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

    return $config;
}

you can then access it later using:

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

Upvotes: 3

aporat
aporat

Reputation: 5932

If you're debugging the issue locally, first enable better error reporting by adding these commands in your application.ini's development section:

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

By default, zend framework doesn't show internal errors.

If you're loading a Zend_Config file, it's always better to load it using an absolute path.

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

Upvotes: 1

Related Questions