0

I am new to ZF and I have problem with even simple tasks.

I would like to have dynamically generated menu on every page. To do that I should fill $this->view with data. OK, but to do that, I would have to fill view separately in every controller I made. This would lead to code duplication.

The most natural solution, that I see, is to create base controller class for all my controllers, but I read, that this is not a good practice in ZF. So how should I do that? Even if it is only one line of duplicated code (eg. $this->view->menu = $reusableObject->generateMenuData()), I don't like it.

What is the best practice for such a solution? How could you solve this problem?

I am using ZF 1.11.

EDIT: I would like to mention, that I would be happy to know how to do that using some kind of phtml file rather than concatinating html tags.

EDIT2: The point is, I am not really interested in only navigation links. Instead of menu with links that could be eg. list of latest post, but on every page, so in every controller. I am particularly interested in "how to this kind of stuff in ZF".

prostynick
  • 6,129
  • 4
  • 37
  • 61

2 Answers2

3

Zend Framework has a Navigation component:

Zend_Navigation is a component for managing trees of pointers to web pages. Simply put: It can be used for creating menus, breadcrumbs, links, and sitemaps, or serve as a model for other navigation related purposes.

It also has some ViewHelpers to render various navigational elements out of it:

  • Breadcrumbs, used for rendering the path to the currently active page.
  • Links, used for rendering navigational head links (e.g. )
  • Menu, used for rendering menus.
  • Sitemap, used for rendering sitemaps conforming to the » Sitemaps XML format.
  • Navigation, used for proxying calls to other navigational helpers.

To prevent code duplication, use a Controller plugin that configures the Zend_Navigation instance and sets it to View or use Zend_Application_Resource_Navigation to configure it from your application.ini, which will then automatically assign it to the View Helpers.

Re your EDITs

You can use Zend_Navigation for arbitrary menues, not just the main navigation. Just configure it as you see fit and then render it with the appropriate helper. And if none of the Navigation helpers are what you are looking for, just write a helper or a partial that does the required output.

Whether you use a Controller Plugin to configure and inject it into the View or use a Helper that queries the Model for your Blog Post in the View Layout is up to you. Both is equally fine and possible.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Pipped by 8 seconds :). You can globally configure the navigation using the application resource plugin. See the link in my answer. – Phil Nov 09 '10 at 22:56
  • @prosty I dont understand your edit. You won't concat any strings when using the suggested components above. It's all done with the Helpers and Zend_Navigation can be configured programmatically. – Gordon Nov 09 '10 at 23:01
  • I am not really thinking about menu, but rather various dynamic sidebars, so Navigation component it's not, what I need. I wrote a question about menu, but I am really thinking about other sidebars like for example: dynamic box with latest posts titles, dynamic box with latest comments etc. – prostynick Nov 09 '10 at 23:06
  • 1
    Thank you very much for your latest edit (with the link). I think that Plugin is what I need. I didn't know I could inject something to the view. I was thinking before about helpers, but I can't find how to use helpers without returning a string of html, but rather helper that uses separate phtml file (my first edit). Is that possible with helper? Thanks for your answer. – prostynick Nov 09 '10 at 23:47
  • @prostynick Have a look at something like the Menu view helper (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.menu). It allows you to set a view partial to use for rendering – Phil Nov 10 '10 at 02:22
  • @prosty I second Phil's suggestion: you can tell your ViewHelper to render a partial then. Returning strings is good practice though. It will make your helpers easier to UnitTest. Note that you can also return the output from a partial, so you could prepare your data, pass it to the partial, generate the markup and return it via the Helper. – Gordon Nov 10 '10 at 09:04
1

Use Zend_Navigation. You can even configure it from your configuration file using the application resource plugin.

Phil
  • 157,677
  • 23
  • 242
  • 245