<?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>Ryan's Blog &#187; Thin Controllers</title>
	<atom:link href="http://www.rmauger.co.uk/tags/thin-controllers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rmauger.co.uk</link>
	<description>Randomness will get you everywhere.</description>
	<lastBuildDate>Fri, 03 Sep 2010 13:02:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Why the Zend Framework Actionstack is Evil</title>
		<link>http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/</link>
		<comments>http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 11:14:34 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Action Stack]]></category>
		<category><![CDATA[Dispatch Loop]]></category>
		<category><![CDATA[Fat Models]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Thin Controllers]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/?p=45</guid>
		<description><![CDATA[The action stack seems to be a useful component to some people when starting out with the Zend Framework. This component is a seemingly un-needed part of the framework, as there really is no use-case for it which cannot be simplified with the use of a partial view, which reads data directly from the model, [...]]]></description>
			<content:encoded><![CDATA[<p>The action stack seems to be a useful component to some people when starting out with the <a href="http://framework.zend.com/" target="_blank">Zend Framework</a>. This component is a seemingly un-needed part of the framework, as there really is no use-case for it which cannot be simplified with the use of a partial view, which reads data directly from the model, possibly with a view helper alongside to provide some additional logic function, such as deciding on which view partial to use.</p>
<p>This part of the Framework causes the dispatch to loop. This is a costly process, as it involves quite alot. It also adds some further issues to your application design, such as where you should put code. for example if you have some code in your predispatch, and your looping through two actions in that controller, that code will be run twice. This is obviously, not good, and quite un-needed. Further complications can be added when it comes time to add ACL or authentication.</p>
<p><span id="more-45"></span></p>
<p>There is an even worse part of this feature, the <a title="The Action View Helper" href="http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.action">Action View Helper</a>. This helper basically creates an additional dispatch, copying the request object, and creating a loop-within-a-loop . The setting up of the dispatch process is a costly one, anyone who has profiled their code will have seen just how much of the process of a <a title="Zend Framework" href="http://framework.zend.com/" target="_blank">Zend Framework</a> application this eats up. Creating a whole extra dispatch must be a <strong>bad</strong> idea, even the <a title="Increasing performance of Zend Framework Applications" href="http://framework.zend.com/manual/en/performance.html" target="_blank">Zend Framework Performance Guide</a> notes this fact</p>
<p>So, lets go through some reasons for this being bad.</p>
<h2>Why its bad</h2>
<h3>Performance</h3>
<p>Every time you go through the dispatch process, there are a number of things which are done, which will be completly un-nessicary for your action, namely calling preDispatch and postDispatch for each action which is called. This often results in questions in #zftalk of &#8220;Why is my ACL being called twice?&#8221;. If you have an ACL being built in your preDispatch (as many people quite rightly do) this means if you have 3 calls to different actions, your ACL will be built, and queried 3 times, and if you haven&#8217;t cached your ACL object, this may incur extra overhead from calls to database queries.</p>
<p>This is even more apparent if you use the action helper, as the request is copied, and a new dispatch created (profile your code and see just how much overhead this involves, its a lot!).</p>
<h3>Unnecessary Complexity</h3>
<p>Trying to follow the application flow through an application which utilises the action stack adds a level of complexity which need not be there.</p>
<p>If you want to see what&#8217;s going on at a URL, you should be able to go to that action, and see which model methods are being invoked, and what is being passed to the view.</p>
<h3>Design Issues</h3>
<p>This is the biggest reason for not using the action stack. If you are using the action stack, you are almost certainly a long way from a true &#8220;MVC&#8221; design. The units of code you are using in your actions, probably belongs in the model.</p>
<p>Your model should encapsulate all the code to extract data, insert data, and manipulate objects, so all your controller action should include is a bunch of calls to the model to fetch data, or insert/update it, but in the form of single method calls only. remember you should be directing the data to the model, not manipulating it.</p>
<h2>Why it is good</h2>
<h3>Illusion of DRY coding</h3>
<p>The action stack can lead you into a false sense of security that your writing nice &#8220;DRY&#8221; code. You are in fact writing dry code, but usually in the wrong place, in fact, if your code is not abstracted into your model, it will end up no-longer being dry, as you will end up with two actions with overlapping functionality, and as such, overlapping code!</p>
<h2>The way forward</h2>
<h3>Fat Models, Thin Controllers</h3>
<p>Your controllers should be minimalist. they exist only to direct data, and to provide some interface to HTTP actions such as redirects.</p>
<p>The bulk of the code in your application should be in your model. For example, if you are writing a blog application, and the action you are writing produces a list of posts. This should require in the action, nothing more than a call to $model-&gt;getPosts(); possibly passing one or more paramaters from the URL (such as year and month) as parameters to the method.</p>
<p>This not only allows you to do away with the action stack, but also allows your code to be portable throughout the application, in true DRY style.</p>
<p>There is an upcomming book, which has a very good overview of how these kinds of models should be written. You can read it <a title="The Zend Framework and Writing Models" href="http://www.survivethedeepend.com/zendframeworkbook/en/1.0/the.model">here</a></p>
<h3>View Helpers</h3>
<p>View helpers can be useful for items which you frequently need, or for items which are simply too small to justify writing a whole view script for them. there is a good example in the <a title="Writing a view helper to replace an action() call" href="http://framework.zend.com/manual/en/performance.view.html#performance.view.action.model">performance guide</a>.</p>
<p>When you want to perform some logic before rendering a view script, a view helper can also be useful to help keep your layout or view free of large switches or if statements. For example this can be something like rendering a login form when a user is not logged in (which would require no view script, only a Zend_Form object), and a menu when they are, or be useful to set up some context for a view partial before rendering it, so that your controller does not have to pass a variable to the view when it would be some default value.</p>
<h3>(Partial) Views</h3>
<p>Partial views do the leg work of replacing the actionstack. In your partial views, you should call read-only methods of the model, to build items such as menus. this could be something like $model-&gt;getSideBarMenuItems(); which would return a dataset to loop through and render.</p>
<h2>Some other notes</h2>
<p>There has been an issue raised in the Zend Framework JIRA (<a title="Zend Framework Jira Issue tracker" href="http://framework.zend.com/issues/browse/ZF-5840">ZF-5840</a>) with regard to removing the action view helper for ZF version 2.0. This is somewhere you may wish to post your opinion on this subject (keep it specific!), or vote to have it removed <img src='http://www.rmauger.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>
