开发者

Developing/using a custom Resource Plugin in Zend Framework

We have used Zend_Log, which is configured in application.ini differently for different circumstances. We initialize it/get it in the bootstrap and store it in the registry:

$r = $this->getPluginResource('log');
$logger = $r->getLog();

But we've subclassed Zend_Log (say, Our_Log) to add customized features, and want to get it the same way. So then we have to make a new Resource Plugin. That seems quite easy - just copy Application/Resource/Log.php, rename the file to Ourlog.php, rename the class to class Zend_Application_Resource_Ourlog. For now, let's not worry about "Our_Log", the class -- just use the new Resource Plugin to get a Zend_Log, to reduce the variables.

So then, our new code in the bootstrap is:

$r = $this->getPluginResource('ourlog');
$logger = $r->getLog();

but of course this doesn't work, error applying method to non-object "r". According to the documentation,

"As long as you register the prefix path for this resource plugin, you can then use it in your application."

but how do you register a prefix path? That would have been helpful. But that shouldn't matter, I used the same prefix path as the default, and I know the file is being read because I "require" it.

Anyway, any guidance on开发者_StackOverflow社区 what simple step I'm missing would be greatly appreciated.


Thanks for the pointers -- so close, so close (I think). I thought I was getting it...

Okay, so I renamed the class Xyz_Resource_Xyzlog, I put it in library/Xyz/Resource/Xyzlog.php

Then, because I don't love ini files, in the bootstrap I put:

$loader=$this->getPluginLoader();
$loader->addPrefixPath('Xyz_Resource','Xyz/Resource/');
$r = $this->getPluginResource('xyzlog');
if (!is_object($r)) die ('Not an object!!');

Sudden Death. So, okay, do the ini:

pluginPaths.Xyz_Resource='Xyz/Resource/'

The same. No help. I believed that the basepaths of the plugin paths would include the PHP "include" paths. Am I mistaken in that? Any other ideas? I'll happily write up what finally works for me to help some other poor soul in this circumstance. Something to do with Name Spaces, maybe?


Plugin classes are resolved using the plugin loader, which works slightly differently to the autoloader; so just requiring the class in doesn't help you here. Instead, add this to your application.ini:

pluginPaths.Application_Resource = "Application/Resource"

you should then be able to use the class as normal. Since your path above will be checked before the default Zend one, you can also name your class 'Log' and still extend the Logger resource to override the standard functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜