<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Juozas devBlog &#187; views</title>
	<atom:link href="http://dev.juokaz.com/tag/views/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework tips and tricks</title>
		<link>http://dev.juokaz.com/php/zend-framework-tips-and-tricks</link>
		<comments>http://dev.juokaz.com/php/zend-framework-tips-and-tricks#comments</comments>
		<pubDate>Fri, 29 Jan 2010 19:38:45 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[globals]]></category>
		<category><![CDATA[include path]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[response]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[views]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=964</guid>
		<description><![CDATA[ It&#8217;s always good to use great tools, but you need to make sure that you use them correctly, not just trying to code &#8220;just for it to work&#8221;. For this reason I decided to write down my usual list of things I mention when taking over some legacy project or just consulting someone how [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-460" title="Zend Framework" src="http://dev.juokaz.com/wp-content/uploads/2009/04/logo-zend-framework.jpg" alt="Zend Framework" width="169" height="114" /> It&#8217;s always good to use great tools, but you need to make sure that you use them correctly, not just trying to code &#8220;just for it to work&#8221;. For this reason I decided to write down my usual list of things I mention when taking over some legacy project or just consulting someone how to start.</p>
<p>Most of the outlined problems and solutions are focused on testability, maintainability and other good code practices. If you are not familiar with them, I recommend read about them ASAP as there is big chance that you are doing those things described in this post and don&#8217;t even realize how wrong they are. Believe me, you will soon find yourself a way better developer.</p>
<h5>Separate logic</h5>
<p>This one is the most obvious one, but trust me, I have found cases of it in every single project I have worked before (more than 10 with Zend Framework in a past half year and counting). If it&#8217;s controller, don&#8217;t do business logic, if it&#8217;s model don&#8217;t base behavior on POST parameters etc. Same applies to forms, bootstrap, views, various helpers and many more other components &#8211; logic should be separate and have its place. </p>
<p>Move all logic from controller to a model or <a href="http://dev.juokaz.com/programming/service-layer-in-web-applications">service</a>. Use forms only to handle validation and filtering, not to actually process data and persist it in any fashion. Hide session and authentication handling in one place and provide API for other algorithms. I can go on, but I hope it&#8217;s starting to get pretty clear, or at least it will eventually when you start testing your code: when you need to setup frontController, request, cookie and mail server just to test a form you realize that something is really wrong.</p>
<h5>Globals</h5>
<p>If you haven&#8217;t seen <a href="http://www.youtube.com/watch?v=-FRm3VPhseI">this</a> video, please do it immediately &#8211; you won&#8217;t regret it. Global state makes testing problematic and is completely opposite to what OOP proposes. This applies for use of <em>$_SERVER, $_SESSION</em> etc. values and all of them are accessible via request object (look at <em>Zend_Controller_Request_Http</em>) methods or separate classes, like Zend_Session. </p>
<p>Yet again I&#8217;m going to mention testability, because that&#8217;s something you always need to keep in mind. Test should not modify global variables, but rather inject mocked request/other objects which just return expected values (like client IP), because Zend Framework does pretty good job abstracting access to all global variables. </p>
<h5>Use form values, not request</h5>
<p>This one is both security and ease of coding issue, let&#8217;s look at this code sample:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">populate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After form is validated (and hence values are filtered), raw values from request are still used from <em>$this->_request->getPost()</em>. Not only do you lose <em>Zend_Form</em> functions like ignored elements (for submit buttons for example) but also none of the filters are applied (which you use, yes?). Also I can pass pretty much anything I want and model needs to do way more validation. Hence <em>$form->getValues()</em> should always be used as it returns form data with respect to all defined rules and filters.</p>
<p>Form <em>populate()</em> method is also very overused. This method is designed to set default values for a form, from for example GB entry (when editing something) apart from that, you simply don&#8217;t need to use it as method <em>isValid()</em> sets values for all elements, so there is no need to apply default values too.</p>
<h5>Do not rely or use exit()/die()</h5>
<p>One of the first things I do is remove all these <em>exit()</em> calls and handle such cases with exceptions or <em>return</em> statements. There is only very very limited amount of situations where you actually need to use one of those functions. Just for example, imagine this controller action code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">userHasPermissions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_Add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">//submit form)</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// save with $form-&gt;getValues();</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// do something else</span></pre></div></div>

<p>First problem &#8211; thinking that <em>$this->_redirect()</em> will call <em>exit()</em> and hence nothing else will be executed. Even though this is true by default, this should be avoided in all cases. Not only it makes all <em>post*</em> events to not be fired, but also it makes testing impossible or incorrect. <em>Zend_Test</em> disables use of <em>exit()</em> in controller helpers, so in this case while testing you cannot test permissions checking as in all cases it will still execute later code. To fix this just add <em>return</em> in front of redirect (<em>return $this->_redirect(&#8216;/&#8217;)</em>) and you are safe.</p>
<p>Furthermore, second <em>exit()</em> is completely useless and makes code even more untestable. Again you can just use <em>return</em> to cancel later code (view will not be rendered as <em>viewRenderer</em> helper checks for redirection header and does nothing if detects such). From my experience, after saving entry, there is only some assignments to view in left code so you won&#8217;t suffer a lot if you just leave redirect, of course without <em>exit()</em>.</p>
<h5>Use a framework, not PHP</h5>
<p>This can sound wrong at first, but if you use a framework (Zend Framework in this case) don&#8217;t start throwing in hacks from 5 year old PHP apps. Like this (controller action):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Some_Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateImage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$image</span><span style="color: #339933;">;</span></pre></div></div>

<p>I don&#8217;t even know where to start&#8230; All this logic is incorporated in response object, so you can do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBody</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It might seem as same thing, but it&#8217;s not. Yet again you can actually test it, you are not working with global state (<em>header()</em> is global state function) and request dispatch process is left working as it should. Controllers do not output any data (hence no echo should be used) they just get request object and return response object, that&#8217;s it. In a similar fashion as <em>exit()</em> breaks dispatch process, outputting from controller does that too, so don&#8217;t forget to make sure that you preserve <a href="http://dev.juokaz.com/wp-content/uploads/2009/12/sequence_globale.jpg">this flow</a>.</p>
<h5>Some small ones</h5>
<p><em>Application.ini</em> has a property <em>includePaths</em>, which is used to add additional paths to include path. Even though it works great, I still recommend not to use it because it will add those paths every time you create new application instance (true for 1.9, can change in future). If you try to do some controllers testing, you are probably going to instantiate it before every single test and after some hundreds of tests you will notice that somehow it&#8217;s getting slower and slower. That took me a few hours to find, though fix was easy, but still keep that in mind.</p>
<p>If you are using <a href="http://jquery.org">jQuery</a> or any other javascript view helpers, take full power of them. By that I mean use functions like <em>addJavascriptFile()</em>, <em>addJavascript()</em>, <em>addStylesheet()</em>, <em>addOnload()</em> etc. to add some additional resources and code from views. If you add javascript straight to view everything will still work, but by using view helper container you will have all your code nicely placed in one place and not scattered all other the place. </p>
<h5>Conclusion</h5>
<p>This is just a small list of problems and issue I have seen in my work &#8211; there are way more to look at (you can share some tips too). I hope those will give you some idea how to work with Zend Framework in a clean fashion and you will soon find that your code is starting to look nicer and coding time is decreasing as everything is nicely separated and transparent to other code. </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-tips-and-tricks/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Zend Framework directory structure</title>
		<link>http://dev.juokaz.com/php/zend-framework-directory-structure</link>
		<comments>http://dev.juokaz.com/php/zend-framework-directory-structure#comments</comments>
		<pubDate>Sun, 22 Feb 2009 22:45:39 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[frontend]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[structure]]></category>
		<category><![CDATA[views]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=238</guid>
		<description><![CDATA[Currently I&#8217;m working on a new project which works on Zend Framework. I really like this framework and it has been my favourite for quite a long time &#8211; I especially like it&#8217;s standalone components and huge space for your personal programming style. But&#8230; I don&#8217;t know how to correctly implement modularity in Zend Framework.
I [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I&#8217;m working on a new project which works on <a href="http://framework.zend.com/">Zend Framework</a>. I really like this framework and it has been my favourite for quite a long time &#8211; I especially like it&#8217;s standalone components and huge space for your personal programming style. But&#8230; I don&#8217;t know how to correctly implement modularity in Zend Framework.</p>
<p>I need to have modular systems, which would allow downloading module as an archive and installing it would be as hard as unpacking it. Modules should work in their own folders and don&#8217;t require any system modifications. Also, modules needs to have different controllers and views for different website parts: backend and frontend.</p>
<p>Zend Framework <a href="http://framework.zend.com/manual/en/zend.controller.modular.html">manual</a> has examples for directory structures and suggests this modular structure:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">application<span style="color: #339933;">/</span>
    <span style="color: #339933;">*</span>module_name<span style="color: #339933;">*/</span>
        controllers<span style="color: #339933;">/</span>
            IndexController<span style="color: #339933;">.</span>php
            FooController<span style="color: #339933;">.</span>php
        models<span style="color: #339933;">/</span>
        views<span style="color: #339933;">/</span>
            scripts<span style="color: #339933;">/</span>
                index<span style="color: #339933;">/</span>
                foo<span style="color: #339933;">/</span>
            helpers<span style="color: #339933;">/</span>
            filters<span style="color: #339933;">/</span></pre></div></div>

<p>But how to distinguish between frontend and backend parts? Some solutions suggests having backend as <em>admin</em> module, but modular structure would be lost &#8211; adding new modules would require modifying <em>admin</em> module. So I decided to invent my own directories tree and came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">application<span style="color: #339933;">/</span>
    <span style="color: #339933;">*</span>module_name<span style="color: #339933;">*/</span>
        controllers<span style="color: #339933;">/</span>
	    admin<span style="color: #339933;">/</span>
                 IndexController<span style="color: #339933;">.</span>php
                 FooController<span style="color: #339933;">.</span>php
	    <span style="color: #000000; font-weight: bold;">public</span><span style="color: #339933;">/</span>
                 IndexController<span style="color: #339933;">.</span>php
                 FooController<span style="color: #339933;">.</span>php
        models<span style="color: #339933;">/</span>
        views<span style="color: #339933;">/</span>
            scripts<span style="color: #339933;">/</span>
                admin<span style="color: #339933;">/</span>
		     index<span style="color: #339933;">/</span>
		     foo<span style="color: #339933;">/</span>
                <span style="color: #000000; font-weight: bold;">public</span><span style="color: #339933;">/</span>
		     index<span style="color: #339933;">/</span>
		     foo<span style="color: #339933;">/</span>
            helpers<span style="color: #339933;">/</span>
            filters<span style="color: #339933;">/</span></pre></div></div>

<p>It works perfectly (requires small bootstrap modifications), but I do not like it. What I don&#8217;t like is high depth of folders &#8211; working with files browser is really painful. Also, multiple folders share same <em>admin</em> and<em> public</em> folders, but it&#8217;s not really right &#8211; <em>admin</em> and <em>public</em> are parts of website and they should have different controllers, not controllers have different parts. So I reversed whole tree:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">application<span style="color: #339933;">/</span>
    <span style="color: #339933;">*</span>module_name<span style="color: #339933;">*/</span>
        admin<span style="color: #339933;">/</span>
            controllers<span style="color: #339933;">/</span>
                 IndexController<span style="color: #339933;">.</span>php
                 FooController<span style="color: #339933;">.</span>php
            scripts<span style="color: #339933;">/</span>
                 index<span style="color: #339933;">/</span>
		 foo<span style="color: #339933;">/</span>
        <span style="color: #000000; font-weight: bold;">public</span><span style="color: #339933;">/</span>
            controllers<span style="color: #339933;">/</span>
                 IndexController<span style="color: #339933;">.</span>php
                 FooController<span style="color: #339933;">.</span>php
            scripts<span style="color: #339933;">/</span>
                 index<span style="color: #339933;">/</span>
		 foo<span style="color: #339933;">/</span>
        models<span style="color: #339933;">/</span>
        views<span style="color: #339933;">/</span>
            helpers<span style="color: #339933;">/</span>
            filters<span style="color: #339933;">/</span></pre></div></div>

<p>I haven&#8217;t tried implementing it, but it works in theory. Having <em>admin</em> and <em>public</em> as root folders also creates possibility to add, for example, part dependant forms more easily.  However, I still don&#8217;t know which structure to choose &#8211; they all have different pros and cons, though last one seems to be most &#8220;natural&#8221;.</p>
<p>Which structure you use?</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-directory-structure/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
