Custom Zend_Helper_Navigation_* helper

4 messages Options
Embed this post
Permalink
petewilliams1983

Custom Zend_Helper_Navigation_* helper

Reply Threaded More More options
Print post
Permalink
I'm trying to create a custom helper for rendering menus, called My_View_Helper_Navigation_CustomMenu. However, I can't work out how to register and use it.

I've tried putting this in the bootstrap:

$view = Zend_Layout::getMvcInstance()->getView();
// Add custom view helper path $view->addHelperPath(LIBRARY_PATH . '/My/View/Helper', 'My_View_Helper');

This works fine for normal helpers, but not for ones within Zend_View_Helper_Navigation. Calling the following in a view just throws an exception:

$this->navigation()->customMenu()->render();

Do I have to register the helper separately with Zend_View_Helper_Navigation or something? If so, how do I do this?

Thanks,

Pete
petewilliams1983

Re: Custom Zend_Helper_Navigation_* helper

Reply Threaded More More options
Print post
Permalink
Sorry, some of my code got mangled, it should be:

$view = Zend_Layout::getMvcInstance()->getView();
$view->addHelperPath(LIBRARY_PATH . '/My/View/Helper', 'My_View_Helper');

Pete

petewilliams1983 wrote:
I'm trying to create a custom helper for rendering menus, called My_View_Helper_Navigation_CustomMenu. However, I can't work out how to register and use it.

I've tried putting this in the bootstrap:

$view = Zend_Layout::getMvcInstance()->getView();
// Add custom view helper path $view->addHelperPath(LIBRARY_PATH . '/My/View/Helper', 'My_View_Helper');

This works fine for normal helpers, but not for ones within Zend_View_Helper_Navigation. Calling the following in a view just throws an exception:

$this->navigation()->customMenu()->render();

Do I have to register the helper separately with Zend_View_Helper_Navigation or something? If so, how do I do this?

Thanks,

Pete
Jurian Sluiman

Re: Custom Zend_Helper_Navigation_* helper

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
The big problem is Zend adds the helper path. Because all ZF helpers are inside Zend_Helper_Navigation_* and not Zend_Helper_*, the framework adds the namespace for these elements itself.


This is in most cases done *after* you added the namespaces youself (e.g. in application.ini). Therefore, Zend will always find its own helpers first (because it searches inside the most recent added namespaces first). If you create your own Navigation view helper and add your own My_View_Helper_* namespace, it all works fine.


As an example, I added my Navigation view helper here: http://pastie.org/671879. This works fine for all our Soflomo_View_Navigation_* helpers.


Regards, Jurian
--
Jurian Sluiman
Soflomo.com


Op Tuesday 27 October 2009 17:54:41 schreef petewilliams1983:
> Sorry, some of my code got mangled, it should be:
>
> $view = Zend_Layout::getMvcInstance()->getView();
> $view->addHelperPath(LIBRARY_PATH . '/My/View/Helper', 'My_View_Helper');
>
> Pete
>
> petewilliams1983 wrote:
> > I'm trying to create a custom helper for rendering menus, called
> > My_View_Helper_Navigation_CustomMenu. However, I can't work out how to
> > register and use it.
> >
> > I've tried putting this in the bootstrap:
> >
> > $view = Zend_Layout::getMvcInstance()->getView();
> > // Add custom view helper path $view->addHelperPath(LIBRARY_PATH .
> > '/My/View/Helper', 'My_View_Helper');
> >
> > This works fine for normal helpers, but not for ones within
> > Zend_View_Helper_Navigation. Calling the following in a view just throws
> > an exception:
> >
> > $this->navigation()->customMenu()->render();
> >
> > Do I have to register the helper separately with
> > Zend_View_Helper_Navigation or something? If so, how do I do this?
> >
> > Thanks,
> >
> > Pete



signature.asc (204 bytes) Download Attachment
petewilliams1983

Re: Custom Zend_Helper_Navigation_* helper

Reply Threaded More More options
Print post
Permalink
Ah thank you, that's done it.

I actually tried something very similar, creating a custom Navigation class and changing the its NS constant. However, because the findHelper() method is the same as the parent class I hadn't copied it across to my new class. Of course thinking about that means that the parent findHelper() method was being called, and it was using the parent::NS constant, completely bypassing the change I'd made.

Thanks,

Pete

Jurian Sluiman wrote:
The big problem is Zend adds the helper path. Because all ZF helpers are
inside Zend_Helper_Navigation_* and not Zend_Helper_*, the framework adds the
namespace for these elements itself.

This is in most cases done *after* you added the namespaces youself (e.g. in
application.ini). Therefore, Zend will always find its own helpers first
(because it searches inside the most recent added namespaces first). If you
create your own Navigation view helper and add your own My_View_Helper_*
namespace, it all works fine.

As an example, I added my Navigation view helper here:
http://pastie.org/671879. This works fine for all our
Soflomo_View_Navigation_* helpers.

Regards, Jurian
--
Jurian Sluiman
Soflomo.com