Registering Helpers with Views

6 messages Options
Embed this post
Permalink
Abraham Block

Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
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?
weierophinney

Re: Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
-- Abraham Block <[hidden email]> 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            | [hidden email]
Zend Framework          | http://framework.zend.com/
Hector Virgen

Re: Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
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 <[hidden email]> wrote:
-- Abraham Block <[hidden email]> 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            | [hidden email]
Zend Framework          | http://framework.zend.com/


Abraham Block

Re: Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
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 <[hidden email]> 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 <[hidden email]> wrote:
-- Abraham Block <[hidden email]> 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            | [hidden email]
Zend Framework          | http://framework.zend.com/



weierophinney

Re: Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
In reply to this post by Hector Virgen
-- Hector Virgen <[hidden email]> wrote
(on Wednesday, 28 October 2009, 01:20 PM -0700):

> 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.

Please create a feature request in the tracker for this. Bonus points if
you attach a patch (including unit tests!), as I'll be more likely to
include it sooner. :)


> On Wed, Oct 28, 2009 at 6:04 AM, Matthew Weier O'Phinney <[hidden email]>
> wrote:
>
>     -- Abraham Block <[hidden email]> 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            | [hidden email]
>     Zend Framework          | http://framework.zend.com/
>
>
>

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
Hector Virgen

Re: Registering Helpers with Views

Reply Threaded More More options
Print post
Permalink
Created issue #ZF-8177

Putting together a patch and unit test now.

--
Hector


On Thu, Oct 29, 2009 at 4:55 AM, Matthew Weier O'Phinney <[hidden email]> wrote:
-- Hector Virgen <[hidden email]> wrote
(on Wednesday, 28 October 2009, 01:20 PM -0700):
> 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.

Please create a feature request in the tracker for this. Bonus points if
you attach a patch (including unit tests!), as I'll be more likely to
include it sooner. :)


> On Wed, Oct 28, 2009 at 6:04 AM, Matthew Weier O'Phinney <[hidden email]>
> wrote:
>
>     -- Abraham Block <[hidden email]> 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            | [hidden email]
>     Zend Framework          | http://framework.zend.com/
>
>
>

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/