Rendering layout parts in Zend Framework
My app is supposed to work like this. First, the default Action of the Default controller (according to URL) does it's job. For the purpose of this question lets name it MyController and indexAction.
Then it forwards ($this->_forward) to my LayoutController menuAction which renders navigation part (menu from data开发者_Go百科base) and forwards to footerAction, which then renders footer from database (things which are in db but must be visible in every single page).
Finally /views/scripts/my/index.phtml is rendered in the layout with $this->layout()->content.
Question is, how shall I make other parts of layout rendered in their appropriate places? Do I need additional files menu.phtml and footer.phtml which somehow would be rendered by LayoutController menuAction and footerAction and somehow inserted in the appropriate places inside layout.phtml? Or can I have just one layout.phtml script with all the html inside, and menuAction / footerAction just provides the text from DB to be inserted?
I'm interested in good programming practice side as well as technical "how to" if you can.
I think you should consider to use just one layout.phtml as your template. So your default action will render not the contents of the layout but the content of the page itself. For example, in a blog application, you'll have the sidebar and the footer, also you will have the post content. Your ->viewPostAction() will render the post, and a plugin with ->postDispatch() method will render the layout contents in every page.
You can take as basis this question: how to call multiple controller action from within any action in ZF?
精彩评论