Generate menu in Zend Framework
I am ne开发者_运维技巧w 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".
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.
Use Zend_Navigation. You can even configure it from your configuration file using the application resource plugin.
精彩评论