<?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>Evilprofessor.co.uk &#187; module</title>
	<atom:link href="http://www.evilprofessor.co.uk/tag/module/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.evilprofessor.co.uk</link>
	<description>The website of Steven Lloyd Watkin</description>
	<lastBuildDate>Thu, 04 Aug 2011 09:56:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Zend Framework Per Module Layout Settings &#8211; Follow Up</title>
		<link>http://www.evilprofessor.co.uk/242-zend-framework-per-module-layout-settings-follow-up/</link>
		<comments>http://www.evilprofessor.co.uk/242-zend-framework-per-module-layout-settings-follow-up/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 20:48:44 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=242</guid>
		<description><![CDATA[As a follow up to my previous post on per module based layout settings for Zend Framework, I&#8217;ve updated the code to require less configuration then before (not that it required more that a few lines in your application configuration!). Again we make use of a Zend Controller Action Helper invoking it from the bootstrap [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my previous post on <a title="Per Module Based Layout Settings - Zend Framework" href="http://www.evilprofessor.co.uk/227-zend-framework-per-module-based-settings/" target="_blank">per module based layout settings</a> for <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">Zend Framework</a>, I&#8217;ve updated the code to require less configuration then before (not that it required more that a few lines in your application configuration!).<br />
<span id="more-242"></span><br />
Again we make use of a <a title="Zend Controller Action Helper" href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html" target="_blank">Zend Controller Action Helper</a> invoking it from the bootstrap as follows:</p>
<pre class="php" name="code">
    /**
     * Sets up <a href="http://www.evilprofessor.co.uk/269-naked-zend_layout-and-zend_view/" class="kblinker" title="More about layout script &raquo;">layout scripts</a> on a per-module basis
     */
    protected function _initLayoutHelper()
	{
	    $this-&gt;bootstrap('frontController');
	    $layout = Zend_Controller_Action_HelperBroker::addHelper(
	        new Pro_Controller_Action_Helper_SetLayoutPath(APPLICATION_PATH));
	}
</pre>
<p>Almost exactly the same as before except this time we pass the constant <strong>APPLICATION_PATH</strong> into the constructor as our base path.</p>
<p>Next the controller action helper itself now looks as follows:</p>
<pre class="php" name="code">
/**
 * Sets the layout path on a per-module basis
 *
 * @author     Lloyd Watkin
 * @since      16/02/2010
 * @package    Pro
 * @subpackage Controller_Action_Helper
 */

/**
 * Sets the layout path on a per-module basis
 *
 * @author     Lloyd Watkin
 * @since      16/02/2010
 * @package    Pro
 * @subpackage Controller_Action_Helper
 */
class Pro_Controller_Action_Helper_SetLayoutPath
    extends Zend_Controller_Action_Helper_Abstract
{
	/**
	 * Base path
	 *
	 * @var string
	 */
	protected $_path;

	/**
	 * Construct
	 *
	 * @param string $path
	 */
	public function __construct($path)
	{
	    $this-&gt;setBasePath($path);
	}

	/**
	 * Set base path
	 *
	 * @param string $path
     */
	public function setBasePath($path)
	{
		if (!is_string($path) || empty($path)) {
			throw new Exception('Excepted string for base path');
		}
		$this-&gt;_path = $path;
	}

	/**
	 * Get the base path
	 *
	 * @return string
	 */
	protected function _getBasePath()
	{
		if (is_null($this-&gt;_path)) {
			if (!defined('APPLICATION_PATH')) {
				throw new Exception('Base path can not be determined');
			}
			$this-&gt;_path = APPLICATION_PATH;
		}
		return $this-&gt;_path;
	}

    /**
     * Sets layout path based on module
     */
    public function preDispatch()
    {
    	$module = preg_replace(
    	    '/[^A-Z]/i', '', $this-&gt;getRequest()-&gt;getModuleName()
    	);

	    if ($bootstrap = $this-&gt;getActionController()
	                       -&gt;getInvokeArg('bootstrap')) {

	        $view = $bootstrap-&gt;getResource('view');
	        $layoutPath = $this-&gt;_getBasePath() .
	            "/modules/{$module}/layouts/scripts/";

	        /* If layout directory exists then apply it, otherwise just fall
	         * back on the default
	         */
	        if (is_dir($layoutPath)) {
	            $this-&gt;getActionController()
	                 -&gt;getHelper('layout')
	                 -&gt;setLayoutPath($layoutPath);
	            $view-&gt;headLink()-&gt;appendStylesheet(
	                "/styles/{$module}/style.css"
	            );
	        }
    	}
    }
}
</pre>
<p>Note the passing of our base path (cf. APPLICATION_PATH) in the constructor, we also have a getter and setter for the base path. Unlike the previous version of the code if the layouts path does not exist then the code will fall back the default layout path. If the base path is not set it will try to determine the base path from the constant APPLICATION_PATH, otherwise an exception is thrown. I&#8217;ve also cheekily thrown in a module-based style sheet for good measure</p>
<p>The module based layout is determined from whether the layout script path exists (obviously this method can be changed easily). This does add a little overhead from the application.ini setup (however if the layout path exists <a href="http://www.php.net" class="kblinker" target="_blank" title="More about PHP &raquo;">PHP</a> will cache the result for performance*) but in return you gain the <em>added convenience</em> of more easily dropping in new modules.</p>
<p>That&#8217;s it, not much different, but you don&#8217;t need to add a new configuration entry each time you wish to add a new module :)</p>
<p>*To clear this cache (if required) use the <a href="http://uk.php.net/manual/en/function.clearstatcache.php" target="_blank">clearstatcache</a> function.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F242-zend-framework-per-module-layout-settings-follow-up%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/242-zend-framework-per-module-layout-settings-follow-up/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Framework Per-Module based settings</title>
		<link>http://www.evilprofessor.co.uk/227-zend-framework-per-module-based-settings/</link>
		<comments>http://www.evilprofessor.co.uk/227-zend-framework-per-module-based-settings/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 22:40:16 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[layoutPath]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=227</guid>
		<description><![CDATA[I&#8217;ve created a followup to this post which requires less configuration, please see Module Based Layout &#8211; Zend Framework. When using the zend framework with modules, its obvious that if you&#8217;re running various (sub-)sites off the same application you don&#8217;t necessarily want the same layout scripts for each part. I decided to go with the [...]]]></description>
			<content:encoded><![CDATA[<p><em>I&#8217;ve created a followup to this post which requires less configuration, please see <a href="http://www.evilprofessor.co.uk/242-zend-framework-per-module-layout-settings-follow-up/">Module Based Layout &#8211; Zend Framework</a>.</em></p>
<p>When using the <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">zend framework</a> with modules, its obvious that if you&#8217;re running various (sub-)sites off the same application you don&#8217;t necessarily want the same <a href="http://www.evilprofessor.co.uk/269-naked-zend_layout-and-zend_view/" class="kblinker" title="More about layout script &raquo;">layout scripts</a> for each part. I decided to go with the following site structure:</p>
<pre>/Application
    /controllers
        ...
    /models
    /modules
        /default
            /controllers
            /layout
                /scripts
            /views
                /scripts
        /anotherModule
            ...
    /scripts
</pre>
<p>The problem was setting up the layout scripts on a per-module basis. The answer came through using an Action Helper. Setting up the layouts on a per-module basis involves three steps:</p>
<ol>
<li>Application.ini (or similar configuration setup):<br/>
<pre name="code" class="ini">admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts"
default.resources.layout.layoutPath = APPLICATION_PATH "/modules/default/layouts/scripts"
member.resources.layout.layoutPath = APPLICATION_PATH "/modules/member/layouts/scripts"
affiliate.resources.layout.layoutPath = APPLICATION_PATH "/modules/affiliate/layouts/scripts"</pre>
</li>
<li>Create your Action Helper:<br/>
<pre name="code" class="php">&lt;?php
/**
 * Sets the layout path on a per-module basis
 *
 * @author Lloyd Watkin &lt;lloyd@evilprofessor.co.uk&gt;
 * @since  2010-01-01
 */
class Pro_Controller_Action_Helper_SetLayoutPath
    extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Sets layout path based on module
     */
    public function preDispatch()
    {
    	$module = $this->getRequest()->getModuleName();

	    if ($bootstrap = $this->getActionController()
	                       ->getInvokeArg('bootstrap')) {

	        $config = $bootstrap->getOptions();

	        if (isset($config[$module]['resources']['layout']['layoutPath'])) {
	            $layoutPath =
	                 $config[$module]['resources']['layout']['layoutPath'];
	            $this->getActionController()
	                 ->getHelper('layout')
	                 ->setLayoutPath($layoutPath);
	        }
    	}
    }
}</pre>
</li>
<li>And lastly boostrap the action helper:<br/>
<pre name="code" class="php">...
    /**
     * Sets up layout scripts on a per-module basis
     */
    protected function _initLayoutHelper()
	{
	    $this->bootstrap('frontController');
	    $layout = Zend_Controller_Action_HelperBroker::addHelper(
	        new Pro_Controller_Action_Helper_SetLayoutPath());
	}
...</pre>
</li>
</ol>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F227-zend-framework-per-module-based-settings%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/227-zend-framework-per-module-based-settings/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
