Hey Nick. I was asking myself the same question the other night.

While it doesn't hurt to pass in your dependencies from within the
controller, it can quickly become tedious. I ended up going with a lazy-load
approach so keep my models unit-testable:

public function getCache()
{
    if (null === $this->_cache) {
        $this->_cache =
Zend_Controller_Front::getInstance()->getParam('bootstrap')->
getResource('Cache');
    }
    return $this->_cache;
}

This would allow you to easily create a setter method that your unit tests
can use to stub in a black hole cache.

--
Hector


On Mon, Nov 16, 2009 at 11:55 PM, Саша Стаменковић <[email protected]>wrote:

> Hi.
>
> Zend_Controller_Front::getInstance()->getParam('bootstrap')->
> getResource('Cache');
>
> But I like
>
> $cache = Zend_Registry::get('Zend_Cache);
>
> it's shorter.
>
> Regards,
> Saša Stamenković
>
>
>
> On Tue, Nov 17, 2009 at 8:34 AM, Nick Pack <[email protected]> wrote:
>
>> This is probably a dumb question, but I'm looking for a way to call a
>> bootstrap resource from a model (in this case is a Zend_Cache object).
>>
>> Bootstrap method:
>>
>>    protected function _initCache()
>>    {
>>        $frontendOptions = array(
>>           'lifetime' => 7200, // cache lifetime of 2 hours
>>           'automatic_serialization' => true
>>        );
>>
>>        $cachedir = realpath(/path/to/cache');
>>        $backendOptions = array(
>>            'cache_dir' => $cachedir
>>        );
>>
>>        $cache = Zend_Cache::factory('Core',
>>                             'File',
>>                             $frontendOptions,
>>                             $backendOptions);
>>        Zend_Registry::set('Zend_Cache',$cache);
>>        return $cache;
>>    }
>>
>> Obviously this is set into the registry so I can access it that way, but I
>> was wondering if this is the best way to do it, or whether there was some
>> way of calling it like you do in a controller:
>>
>> $this->_cache = $this->getInvokeArg('bootstrap')->getResource('Cache');
>>
>> or maybe even passing it directly over to the __construct of the model.
>>
>> Any advice greatly appreciated
>>
>
>

Reply via email to