{"id":14,"date":"2013-05-09T17:13:16","date_gmt":"2013-05-09T17:13:16","guid":{"rendered":"http:\/\/aurelio.audero.it\/blog\/?p=14"},"modified":"2016-06-04T00:21:23","modified_gmt":"2016-06-04T00:21:23","slug":"how-to-change-the-default-view-in-zend-framework-1","status":"publish","type":"post","link":"https:\/\/www.audero.it\/blog\/2013\/05\/09\/how-to-change-the-default-view-in-zend-framework-1\/","title":{"rendered":"How to Change the Default View in Zend Framework 1"},"content":{"rendered":"<p>In this article I&#8217;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&#8217;ll explain how you can change the default view of a given action.<br \/>\n<!--more--><br \/>\n<a href=\"http:\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner.jpg\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"30\" data-permalink=\"https:\/\/www.audero.it\/blog\/2013\/05\/09\/how-to-change-the-default-view-in-zend-framework-1\/zend-framework-banner\/\" data-orig-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner.jpg\" data-orig-size=\"415,150\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Zend Framework banner\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner.jpg\" src=\"\/\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner.jpg\" alt=\"Zend Framework banner\" width=\"415\" height=\"150\" class=\"aligncenter size-full wp-image-30\" srcset=\"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner.jpg 415w, https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-banner-300x108.jpg 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/a><br \/>\nThanks 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&#8217;t a good practice to repeat the same code in different files. The latter is usually referred as <abbr title=\"Write Everything Twice\">WET<\/abbr>. In this article I&#8217;ll explain how you can reuse a given view to avoid the repetition and apply the <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> principle.<\/p>\n<p>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&#8217;s a good thing to reuse the code.<\/p>\n<h2>A Brief Recall<\/h2>\n<p>During this article, I&#8217;ll refer to some basic concepts of Zend Framework. For those of you that need a recall, here it is:<\/p>\n<ul>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>ViewRenderer: It is a <em>helper<\/em> that automate the process of setting up the view object in your controllers and rendering views.<\/li>\n<\/ul>\n<h2>The Problem<\/h2>\n<p>Let that we&#8217;ve a controller called <code>ProductController<\/code> who has two actions: <code>addAction<\/code> and <code>editAction<\/code>. 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&#8217;ll be using is the one of <code>addAction<\/code>. Without the use of modules, its relative path is <code>\/application\/views\/scripts\/product\/add.phtml<\/code>. Because the default behavior of the framework is to use as view the one that is found under <code>controllerName\/actionName.phtml<\/code>, the <code>add<\/code> action doesn&#8217;t need extra code. On the other hand, the method <code>editAction<\/code> needs additional lines of code, which are actually&#8230;just one! Awesome, not?<\/p>\n<h2>Solutions<\/h2>\n<p>In the following paragraphs I&#8217;ll show three different contexts where you want to use this tip.<\/p>\n<h3>Change the View for an Action in the <em>Same<\/em> Controller<\/h3>\n<p>The first case I&#8217;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:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$this-&gt;_helper-&gt;viewRenderer('add');\r\n<\/pre>\n<h3>Change the View for an Action in a <em>Different<\/em> Controller<\/h3>\n<p>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 <code>deleteAction<\/code> which is in a controller called <code>CategoryController<\/code>. To set the different view you have to write in the action <code>editAction<\/code>of the <code>ProductController<\/code> this line:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$this-&gt;_helper-&gt;viewRenderer('category\/delete', null, true);\r\n<\/pre>\n<p>As you can see, I added two new parameters to the viewRenderer method. The second parameter is for &#8220;named segments&#8221;. Citing the official documentation:<\/p>\n<blockquote cite=\"http:\/\/framework.zend.com\/manual\/en\/zend.controller.response.html#zend.controller.response.namedsegments\"><p>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.<\/p><\/blockquote>\n<p>The third parameter is a boolean. If the value is <code>true<\/code>, the framework will <strong>not<\/strong> 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 <code>product\/category\/delete.phtml<\/code>.<\/p>\n<h3>Disable the Default View<\/h3>\n<p>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:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ Disable the view\r\n$this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender();\r\n\/\/ Disable the layout $this-&gt;_helper-&gt;layout()-&gt;disableLayout();\r\n<\/pre>\n<h3>Final Note<\/h3>\n<p>As final note I want to underline the fact that in none of the cases shown I wrote the extension of the views (<code>.phtml<\/code>). The Zend Framework will do this job for us, automatically. It&#8217;s really important to omit it, otherwise you&#8217;ll have an application error. This happens because the framework will search for a view called, for example, <code>add.phtml.phtml<\/code>.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article we&#8217;ve seen how to change the default view of an action in different situations. Reuse the views and apply the <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> 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&#8217;ll pay a high cost along your way.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;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&#8217;ll explain how you can change the default view of a given action. <\/p>\n","protected":false},"author":1,"featured_media":26,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[47,11,13],"class_list":["post-14","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php","tag-web","tag-zend-framework"],"jetpack_featured_media_url":"https:\/\/www.audero.it\/blog\/wp-content\/uploads\/2013\/05\/zend-framework-logo.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9Or4e-e","jetpack-related-posts":[{"id":273,"url":"https:\/\/www.audero.it\/blog\/2014\/02\/05\/why-people-think-php-sucks\/","url_meta":{"origin":14,"position":0},"title":"Why People Think PHP Sucks!","author":"Aurelio De Rosa","date":"February 5, 2014","format":false,"excerpt":"A long time has been passed since the first release of PHP. Over the years the language has been improved a lot, in terms of performance and features, thanks to the work of many contributors. At the same time, also its adoption among developers is grown dramatically. Looking at the\u2026","rel":"","context":"In &quot;Discussions &amp; Opinions&quot;","block_context":{"text":"Discussions &amp; Opinions","link":"https:\/\/www.audero.it\/blog\/category\/discussions-opinions\/"},"img":{"alt_text":"PHP elephants","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/02\/php-elephants.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/02\/php-elephants.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2014\/02\/php-elephants.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":197,"url":"https:\/\/www.audero.it\/blog\/2013\/12\/23\/enhancing-the-abbr-element-on-mobile\/","url_meta":{"origin":14,"position":1},"title":"Enhancing the abbr Element on Mobile","author":"Aurelio De Rosa","date":"December 23, 2013","format":false,"excerpt":"Today's world runs fast, really fast. When writing, a lot of people use abbreviations and acronyms to save time that we can find spread all over the web. They are so used that an HTML tag, <abbr>, was created to identify them and help the semantic of web pages. In\u2026","rel":"","context":"In &quot;CSS&quot;","block_context":{"text":"CSS","link":"https:\/\/www.audero.it\/blog\/category\/css\/"},"img":{"alt_text":"The abbr tag unstyled","src":"https:\/\/i0.wp.com\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/12\/abbr-unstyled.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":5,"url":"https:\/\/www.audero.it\/blog\/2013\/05\/13\/learning-curve-what-it-is-and-how-it-applies-to-information-technology\/","url_meta":{"origin":14,"position":2},"title":"Learning Curve: What it is and How it Applies to Information Technology","author":"Aurelio De Rosa","date":"May 13, 2013","format":false,"excerpt":"The terms \"learning curve\" and \"steep learning curve\" are often talked and written in Computer Science. At the time I could not understand deeply those terms, every time I found them I felt like I was missing something that all programmers except me already knew. After, I realised that there\u2026","rel":"","context":"In &quot;Discussions &amp; Opinions&quot;","block_context":{"text":"Discussions &amp; Opinions","link":"https:\/\/www.audero.it\/blog\/category\/discussions-opinions\/"},"img":{"alt_text":"Tic Tac Toe learning curve","src":"https:\/\/i0.wp.com\/aurelio.audero.it\/blog\/wp-content\/uploads\/2013\/05\/tic-tac-toe-learning-curve.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":553,"url":"https:\/\/www.audero.it\/blog\/2016\/08\/03\/power-simplicity-code\/","url_meta":{"origin":14,"position":3},"title":"The power of simplicity in code","author":"Aurelio De Rosa","date":"August 3, 2016","format":false,"excerpt":"A\u00a0few days ago I found an online test\u00a0featuring six exercises on JavaScript. For fun, whishing to challenge myself with something tricky, I decided to take it.\u00a0The test proved to be very simple, so it isn't worth a discussion. It's what happened next that inspired this article. I was talking with\u2026","rel":"","context":"In &quot;Discussions &amp; Opinions&quot;","block_context":{"text":"Discussions &amp; Opinions","link":"https:\/\/www.audero.it\/blog\/category\/discussions-opinions\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/code-simplicity.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/code-simplicity.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/code-simplicity.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":626,"url":"https:\/\/www.audero.it\/blog\/2016\/12\/05\/monkey-patching-javascript\/","url_meta":{"origin":14,"position":4},"title":"Monkey patching in JavaScript","author":"Aurelio De Rosa","date":"December 5, 2016","format":false,"excerpt":"When working on a project, we often use libraries that implement methods that aren't built-in in the programming language in use but we need. These libraries don't cover all the possible methods, so they might lack one or more crucial features we need. When this happens, we have two choices:\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/www.audero.it\/blog\/category\/javascript\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/12\/monkey-patching.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":524,"url":"https:\/\/www.audero.it\/blog\/2018\/05\/16\/event-delegation-in-javascript\/","url_meta":{"origin":14,"position":5},"title":"Event delegation in JavaScript","author":"Aurelio De Rosa","date":"May 16, 2018","format":false,"excerpt":"One of the most common tasks a web developer deals with is to add event listeners to the elements of a page. Event listeners are employed to perform one or more actions when a given event occurs on one or more elements. For example, by using an event listener we\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/www.audero.it\/blog\/category\/javascript\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/event-delegation.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/event-delegation.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/event-delegation.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.audero.it\/blog\/wp-content\/uploads\/2016\/08\/event-delegation.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":15,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":465,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/media\/26"}],"wp:attachment":[{"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.audero.it\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}