2009年10月28日星期三

Re: [fw-mvc] Registering Helpers with Views

As far as implementation goes, it doesn't look so hard either. Now, Zend_View::getHelper() delegates to Zend_View:_getPlugin() which instantiates the class, and adds it to the $_helper array. So addHelper, or whatever you'd call it, can just add your already instantiated class to that array. 

On Wed, Oct 28, 2009 at 4:20 PM, Hector Virgen <djvirgen@gmail.com> wrote:
Here's how I imagine this would look like:

// Adding a custom helper
$view = new Zend_View();
$helper = new MyCustomHelper();
$view->addHelper($helper, 'foo');
$view->foo(); // calls MyCustomHelper#foo()

// Overwriting a built-in helper
$myUrlHelper = new MyUrlHelper();
$view->addHelper($myUrlHelper, 'url');
$view->url(); // calls MyUrlHelper#url();

There's always a potential for a BC break when adding new methods to Zend_View_Abstract, but the chances are slim that someone created their own view helper named "AddHelper". Just something to keep in mind when picking the name for the method.

--
Hector



On Wed, Oct 28, 2009 at 6:04 AM, Matthew Weier O'Phinney <matthew@zend.com> wrote:
-- Abraham Block <atblock@gmail.com> wrote
(on Wednesday, 28 October 2009, 08:38 AM -0400):
> Why is there no way of registering a helper that you've instantiate yourself
> with an instance of Zend_View? Why is the only way to instantiate one through
> the plugin loader? What if I wanted to control the instantiation of a view
> helper (for DI purposes, let's say). Or what if I wanted to add a view helper
> which didn't conform to the Zend\Pear naming standards (let's say it used php
> 5.3 namespaces). Is there a reason the design is this way?

There is not a way to do this currently; it was not a part of the
original design specification.

If you would like to see this in a future version, please put in a
request on the issue tracker, or create a proposal indicating how you
would envision this working.

Thanks!

--
Matthew Weier O'Phinney
Project Lead            | matthew@zend.com
Zend Framework          | http://framework.zend.com/



没有评论: