<?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>Rakiz Random Ramblings</title>
	<atom:link href="http://www.rakiz.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rakiz.com</link>
	<description>Archieve of Generally Useless but Potentially Useful Information</description>
	<lastBuildDate>Wed, 15 Jul 2009 05:02:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Layouts Per Module In Zend</title>
		<link>http://www.rakiz.com/topics/layouts-per-module-in-zend/</link>
		<comments>http://www.rakiz.com/topics/layouts-per-module-in-zend/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 13:40:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.rakiz.com/?p=3</guid>
		<description><![CDATA[After the introductory Zend Quickstart Tutorial, one common initial request is to add support for modules which then quickly morphs into the need for per module layouts.
Views From the Hill has an excellent tutorial to do exactly that. Building upon these two resources with further improvement your directory structure might look like this:
/application
/config
/layouts
/scripts
/models
/modules
/default
/controllers
/views
/admin
/controllers
/layouts
/scripts
/views
/stats
/controllers
/views
The goal is [...]]]></description>
			<content:encoded><![CDATA[<p>After the introductory <a href="http://framework.zend.com/docs/quickstart">Zend Quickstart Tutorial</a>, one common initial request is to add support for modules which then quickly morphs into the need for per module layouts.</p>
<p><a href="http://dustint.com/archives/28">Views From the Hill</a> has an excellent tutorial to do exactly that. Building upon these two resources with further improvement your directory structure might look like this:</p>
<blockquote style="color:#444444;"><p>/application<br />
<span style="padding-left: 2em;">/config</span><br />
<span style="padding-left: 2em;">/layouts</span><br />
<span style="padding-left: 4em;">/scripts</span><br />
<span style="padding-left: 2em;">/models</span><br />
<span style="padding-left: 2em;">/modules</span><br />
<span style="padding-left: 4em;">/default</span><br />
<span style="padding-left: 6em;">/controllers</span><br />
<span style="padding-left: 6em;">/views</span><br />
<span style="padding-left: 4em;">/admin</span><br />
<span style="padding-left: 6em;">/controllers</span><br />
<span style="padding-left: 6em;">/layouts</span><br />
<span style="padding-left: 8em;">/scripts</span><br />
<span style="padding-left: 6em;">/views</span><br />
<span style="padding-left: 4em;">/stats</span><br />
<span style="padding-left: 6em;">/controllers</span><br />
<span style="padding-left: 6em;">/views</span></p></blockquote>
<p>The goal is to load a module specific layout only if it is available and revert to the default global layout in all other cases.</p>
<p>In our case only the admin module will have its own layout. To accomplish this we need to make some changes to the bootstrap.</p>
<p>Add the following to your bootstrap:</p>
<blockquote>
<div style="width: 600px;"><code><span style="color: #000000"><br />
<span style="color: #FF8000">// Setup Module Path<br/></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'MODULES_PATH'</span><span style="color: #007700">, </span><span style="color: #0000BB">APPLICATION_PATH</span><span style="color: #007700">.</span><span style="color: #DD0000">'/modules'</span><span style="color: #007700">);</p>
<p></span><br />
</span></code></div>
</blockquote>
<p>You might prefer to define the modules path in a config file and store the information in the Zend_Registry. But for the purposes of brevity we will use a simple define statement here.</p>
<p>Next we setup the front controller and layout systems:</p>
<blockquote>
<div style="width: 600px;"><code><span style="color: #000000"><br />
<span style="color: #FF8000">// Setup Front Controller</p>
<p></span><span style="color: #0000BB">$frontController </span><span style="color: #007700">= </span><span style="color: #0000BB">Zend_Controller_Front</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();<br />
</span><span style="color: #0000BB">$frontController</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addModuleDirectory</span><span style="color: #007700">(</span><span style="color: #0000BB">MODULES_PATH</span><span style="color: #007700">);<br />
</span><span style="color: #0000BB">$frontController</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setParam</span><span style="color: #007700">(</span><span style="color: #DD0000">'env'</span><span style="color: #007700">, </span><span style="color: #0000BB">APPLICATION_ENVIRONMENT</span><span style="color: #007700">);</p>
<p></span><span style="color: #FF8000">// Setup Layout</p>
<p></span><span style="color: #0000BB">Zend_Layout</span><span style="color: #007700">::</span><span style="color: #0000BB">startMvc</span><span style="color: #007700">(array(<br />
</span><span style="color: #DD0000">'layoutPath' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">APPLICATION_PATH </span><span style="color: #007700">. </span><span style="color: #DD0000">'/layouts/scripts'</span><span style="color: #007700">,<br />
</span><span style="color: #DD0000">'pluginClass' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Custom_Layout_Plugin'</p>
<p></span><span style="color: #007700">));<br />
</span><br />
</span><br />
</code></div>
</blockquote>
<p>Finally we add our Layout Plugin class:</p>
<blockquote>
<div style="width: 600px;"><code><span style="color: #000000"><br />
<span style="color: #FF8000">/**<br />
* Layout Plugin to enable per module layout scripts<br />
*/</p>
<p></span><span style="color: #007700">class </span><span style="color: #0000BB">Custom_Layout_Plugin </span><span style="color: #007700">extends<br />
</span><span style="color: #0000BB; padding-left: 8em;">Zend_Layout_Controller_Plugin_Layout<br />
</span><span style="color: #007700">{</span><span style="color: #007700; padding-left: 3em;"><br />
<br/>public function </span><span style="color: #0000BB">preDispatch</span><span style="color: #007700">(<br/></span><span style="color: #0000BB; padding-left: 3em;">Zend_Controller_Request_Abstract $request</span><span style="color: #007700">)<br />
{</p>
<p></span><span style="color: #0000BB; padding-left: 2em;">$module_name </span><span style="color: #007700">= </span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getModuleName</span><span style="color: #007700">();<br />
</span><span style="color: #0000BB; padding-left: 2em;">$layout_path </span><span style="color: #007700">= </span><span style="color: #0000BB">MODULES_PATH </span><span style="color: #007700">. </span><span style="color: #DD0000">"/{$module_name}/layouts/scripts"</span><span style="color: #007700">;</p>
<p></span><span style="color: #FF8000; padding-left: 2em;">// Check if layout directory exists<br />
</span><span style="color: #007700; padding-left: 2em;">if ( </span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$layout_path</span><span style="color: #007700">) )<br />
</span><span style="color: #007700; padding-left: 2em;">{<br />
</span><span style="color: #0000BB; padding-left: 3em;">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getLayout</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setLayoutPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$layout_path</span><span style="color: #007700">);<br />
</span><span style="color: #007700; padding-left: 2em;">}</span><span style="color: #007700"></p>
<p>}</p>
<p>}<br />
</span><br />
</span><br />
</code></div>
</blockquote>
<p>And we are done!</p>
<p>When ever a module has a /modules/module_name/layouts/scripts directory the system will look for layout files there, otherwise it reverts to the global application/layouts/scripts.</p>
<p>We can delete the layout folder inside a module directory and the system will move to the default layout with no further configuration and vice versa.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rakiz.com/topics/layouts-per-module-in-zend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
