views:

66

answers:

1

I'm using Zend Framework 1.8/1.9's Zend_Application and the resource system to initilize a bunch of resources. I would like to only load certain .ini files based on the module being requested – for example, loading "help.ini" if (and only if) the CMS module is requested ("/cms"). Trouble is, I'm not sure how to access the request object in a Zend_Application_Resource_ResourceAbstract subclass.

In one of my resources (for initializing custom routes for the CMS), I use a hacky little util to get the module (below), and then I add custom routes if it matching the "cms" module name:

/**
 * Grab the module name without a request instance
 *
 * @return string  The module name
 */
public static function getModuleName()
{
    $uri = ltrim($_SERVER["REQUEST_URI"], "/");
    $module = substr($uri, 0, strpos($uri, "/"));
    return $module;
}


$module = Typeoneerror_Util_Strings::getModuleName();

// -- only attach certain routes if using cms module
if ($module == Typeoneerror_Constants::CMS_MODULE)
{
    ...

I'd like this to be more "Zend-y", i.e. like a controller plugin, where a request object is passed to the class:

public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $router = $this->__front->getRouter();
    ...

Any ideas?

+2  A: 

To directly answer your question: From your ResourceAbstract subclass, you can retrieve the front controller like so:

 $bootstrap = $this->getBootstrap();
 $bootstrap->bootstrap('frontController'); // make sure it's initialized
 $fc = $bootstrap->getResource('frontController');
 // Then I'm sure you know how to get the request from there.

However, the request object is typically not constructed until the dispatch() method of Zend_Controller_Front is called. So, in short, the request object you are looking for does not yet exist. So, you could manually construct an instance of Zend_Controller_Request_Http then pass it along to your front controller.

But, nothing has been routed yet either, so none of the params you're used to seeing the request object have been populated.

Basically, anything that happens in Zend_Application is way too early in your application's life cycle to do what you want.

I would highly recommend doing your module-specific loading in the routeShutdown hook of a front controller plugin.

jason
"routeShutdown() is called after the router finishes routing the request." - Exactly what I was looking for! Guess I should've done more research on the "life-cycle" of the controller plugin events. Well done!
Typeoneerror