Let's say I have a simple website where users can log in. When they are logged in, I want to show them a different message than users who are not (guests). This message should render in a placeholder, by appending the message to it.
Where should this be done? I was thinking of having my controller check whether the user is logged in or not, and then append to the placeholder via $this->view->placeholder("sidebar")->append()
Why not just put it in the layout itself?
For example, I often have the following situation that affect my layout: if the user is logged in, I want to display his username, a link to view/edit his profile, and a link to logout. If he is not logged in, then I show him a link to login and a link to register.
The code to handle all this uses Zend_Auth::hasIdentity()
, Zend_Auth::getIdentity()
, and the url()
view-helper. To keep the layout code a little leaner, I often push all this into my own view-helper called something like authLinks()
.
A better solution might be to switch the layout based on the authentication status of the current user. This could be done with a plugin in preDispatch, or in the preDispatch within your controller. By placing the display logic in the view layer, you don't have to update lower level code if you decide to change the message, or remove it all together.
I would personally opt for it being in a controller plugin since it abstracts the concern of checking authentication status and updating the view away from controllers, and prevents you from having to worry about putting the appropriate code in any controllers you create in the future.
That is a matter of personal preference. I always delegate that responsibility to the view, so in my mind yes it should be handled by the view.