PapayaSoft - Phuket web development

Accessing Zend Framework Application Resources and Config Options in Module Bootstraps

| Comments

A quick post to document something I tripped over today. As usual, mostly for my own benefit since there is a decent chance that I’ll completely forget it the next time I need it.

I had a Zend Framework 1.11 app using Zend_Application and its bootstrap process. The application had several modules. So, in application/configs/application.ini:

resources.modules.front = "front"
resources.modules.auth  = "auth"
resources.modules.admin = "admin"

And each module bootstrap class extended Zend_Application_Module_Bootstrap. For example, the bootstrap class for the module named “front” looked like:

class Front_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initSomething()
    {
        // init something
    }
}

And there were some app-level application resources – like cachemanager, for example – that were defined in the usual way:

resources.cachemanager.feed.frontend.name = Core
resources.cachemanager.feed.frontend.options.lifetime = 21600
resources.cachemanager.feed.frontend.options.automatic_serialization = true
resources.cachemanager.feed.backend.name = File
resources.cachemanager.feed.backend.options.cache_dir = APPLICATION_PATH "/../data/cache"

All straightforward and working fine, as it has on many previous occasions.

In my module bootstraps, I was registering some front-controller plugins that needed to access those bootstrapped application resources and config data (the cachemanager resource and a feed url, respectively) that were all specified at app-level. So, I tried to access them in the module-level bootstrap using:

class Front_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initSomething()
    {
        // get some options, used later
        $options = $this->getOptions();

        // use built-in dependency tracking
        $this->bootstrap('cachemanager');

        // get the bootstrapped resource
        $cachemanager = $this->getResource('cachemanager');

        // do more, with the options and the cachemanager
    }
}

Result: The variable $options was merely an empty array and the app would die when trying to access the cachemanager resource.

Apparently since we configured/instantiated all these at the app-level (!), I need to access them through the app-level (!) bootstrap which I can get using $this->getApplication(). So the correct code is:

class Front_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initSomething()
    {
         // get the application-level bootstrap first
         $application = $this->getApplication();

        // get some options, used later
        $options = $application->getOptions();

        // use built-in dependency management
        $application->bootstrap('cachemanager');

        // get the bootstrapped resource
        $cachemanager = $application->getResource('cachemanager');

        // do more, with the options and the cachemanager
    }
}

In hindsight, and with all the explicit references I have made to the adjective ‘app-level”, it’s all pretty obvious. I will note that getApplication() does not return the Zend_Application instance (as the name kind-of implies), but rather the app-level bootstrap. As potentially misleading as the method name may be, I suppose it makes sense since the app-level bootstrap instance is usually more directly useful than the application instance.

With any luck, I’ll remember it next time. Until then, hope this helps someone else.

Comments