In this article I’ll give you a little tip about a situation which often arises during the development of a website or a web application based on the Zend Framework 1. I’ll explain how you can change the default view of a given action.
Thanks to the native templating system of the framework chances there are that you wrote many times the same lines of code in different views. The templating system gives you the possibility to have a common layout and to write in your views just the content part of the whole page. As you may already know, it isn’t a good practice to repeat the same code in different files. The latter is usually referred as WET. In this article I’ll explain how you can reuse a given view to avoid the repetition and apply the DRY principle.
One of the main problems that arise when you have repetitions of code is when you have to refactor. Refactoring is very useful when you find a bug, when you find a performance issue and in many other situations. Because changing many files, especially for a large project, is really cumbersome, it’s a good thing to reuse the code.
During this article, I’ll refer to some basic concepts of Zend Framework. For those of you that need a recall, here it is:
- Model: The model manages the data and the state of the application. In the model there are the methods that allow a Controller to access the data.
- Controller: The controller receives commands from the user usually through the view. The controllers have the methods to manage the user requests and to act accordingly.
- View: The view manages the display of information to the user. Here you write the HTML code that shows to the user, in the way you like the most, the data sent by the controller.
- Action: An action is a special method contained in a controller. These methods may be requested via web. In it you can also choose which view to display to the user.
- ViewRenderer: It is a helper that automate the process of setting up the view object in your controllers and rendering views.
Let that we’ve a controller called
ProductController who has two actions:
editAction. Our goal is to use the same view independently of which of those have been requested by the application. To make the example as simple as possible, the view that we’ll be using is the one of
addAction. Without the use of modules, its relative path is
/application/views/scripts/product/add.phtml. Because the default behavior of the framework is to use as view the one that is found under
add action doesn’t need extra code. On the other hand, the method
editAction needs additional lines of code, which are actually…just one! Awesome, not?
In the following paragraphs I’ll show three different contexts where you want to use this tip.
The first case I’ll take into account is the one I described above and that is how to change the default view of an action in the same controller of the action we want to use the view. The code to write is quite simple:
In this case, the situation is a little bit different. The goal is to reuse the view of an action of a controller different from the one executed. For example, let that we want to use the view of an action called
deleteAction which is in a controller called
CategoryController. To set the different view you have to write in the action
ProductController this line:
$this->_helper->viewRenderer('category/delete', null, true);
As you can see, I added two new parameters to the viewRenderer method. The second parameter is for “named segments”. Citing the official documentation:
This [named segment] allows you to segregate body content into different segments and order those segments so output is returned in a specific order. Internally, body content is saved as an array, and the various accessor methods can be used to indicate placement and names within that array.
The third parameter is a boolean. If the value is
true, the framework will not search the path specified in the first parameter as a subdirectory of the current controller. This means that the framework will not search for the file at
The last case covered is when you want to not show a view. Neither the default nor any others. Usually, if you want this, you want to not display the layout too. To achieve this goal, you have to write:
// Disable the view $this->_helper->viewRenderer->setNoRender(); // Disable the layout $this->_helper->layout()->disableLayout();
As final note I want to underline the fact that in none of the cases shown I wrote the extension of the views (
.phtml). The Zend Framework will do this job for us, automatically. It’s really important to omit it, otherwise you’ll have an application error. This happens because the framework will search for a view called, for example,
In this article we’ve seen how to change the default view of an action in different situations. Reuse the views and apply the DRY principle is a good practice but remember that some part of your project can accidentally share the same code in a given moment. So, beware of the context and the reuse of the view, otherwise you’ll pay a high cost along your way.
There are many other things you can do to improve your code, probably more important than this. The point to understand here is learning to think before writing the code and optimize when you can.