<?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>BinaryKitten&#039;s Development Dropbox &#187; Zend</title>
	<atom:link href="http://binarykitten.com/tag/zend/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.com</link>
	<description>Curently a work in progress. Please be patient</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:49:50 +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>Active Module Based Config with Zend Framework</title>
		<link>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 22:41:41 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=177</guid>
		<description><![CDATA[I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead I bring to you How I managed to get Active Module Based Configuration within Zend Framework.</p>
<h2><strong>The Problem</strong></h2>
<p>The Concept I wanted to achieve was to have unique Configuration based upon the module that was active. The Issue with this is that the Bootstrap files and the _init functions for ALL modules are called with no bias as to which module is active. Thus if you created a 3 modules wanted to make menu alterations in one, those alterations will be applied to all. I also wanted to have a a system where if i added extra modules i could just add extra functions to the bootstrap file and it would work in a similar way.</p>
<p>With this in mind, I set about trying to figure out the solution.<br />
<span id="more-177"></span></p>
<h2><strong>New Version</strong></h2>
<p>Though this version still works and you should read through the code to see how to implement the plugin, there is a new version available at:<br />
<a href="http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html">http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html</a> <br />
Please refer to this as the latest version.. Thanks</p>
<p>&#8212;&#8211; Original Post &#8212;&#8211;</p>
<h2><strong>Solution 1 &#8211; Failure</strong></h2>
<p>With the Idea that no-body is perfect, including myself. My 1st attempt ended in failure. This attempt was to modify/extend the Bootstrap class to add extra functions to the resources list.. In the end I couldn&#8217;t determine if the Module bootstrap was the active one. ok So Attempt 1 was a failure, onto the next</p>
<h2>Solution 2 &#8211; Success!</h2>
<p>A quick conversation with Matthew Weier O&#8217;Phinney (<a href="http://twitter.com/weierophinney">@weierophinney</a>) pointed me in the direction of Controller plugins and the routeShutdown method, as after the route had been finished which module was active would be able to be discerned.<br />
At this point I must apologise to Pieter Kokx ( @kokxie )  who I had a small disagreement with in the #zftalk channel. Pieter had done his best to point me down this route to start with, though being a stubborn mule I am refused to see the quality and precision of his comments.<br />
Thank you both for your help here.</p>
<p>The way that this works is that is scans the active modules bootstrap for functions starting with <span style="text-decoration: underline">activeInit</span> or <span style="text-decoration: underline">modulenameInit</span> just like the <a href="http://is.gd/64s1y">_init</a> functions but these would only be called if the module is active.<br />
I was successfully able to create the plugin and trigger the functions, unfortunately it was triggering/calling them in a static sense.. which meant that standard _init style code wouldn&#8217;t work. Luckily with a little digging in the source of the framework i found a storage of the modules and their initiated bootstrap classes. Lucky Me! So finally we call the methods within the right context.</p>
<p>So Here it is the Final Code, Please do comment, I learn from people as I hope that others can learn from me.</p>
<p>I&#8217;ve used the &#8220;Namespace&#8221; of BinaryKitten here. If you want to use a different &#8220;Namespace&#8221; Replace BinaryKitten with what you want.<br />
The &#8220;Namespace&#8221; allows for the use of the BinaryKitten folder within the Libray Folder.<br />
Remember the Controller Plugin should go in the right place for your application, if you are using the autoloader you can add the &#8220;Namespace&#8221; to be autoloaded via your application.ini</p>
<pre class="brush: text">
autoloadernamespaces[] = &quot;BinaryKitten&quot;
</pre>
<p>First off we have the Controller Plugin.<br />
This should go into the &#8220;Namespace&#8221; folder within the Library Folder and should be called ModuleConfig.php</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_ModuleConfig extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap =  $frontController-&gt;getParam(&#039;bootstrap&#039;);
        $activeModuleName = $request-&gt;getModuleName();
        $moduleList = $bootstrap-&gt;modules;

        $moduleInitName = strtolower($activeModuleName).&quot;Init&quot;;
        $moduleInitNameLength = strlen($moduleInitName);

        if (isset($moduleList[$activeModuleName])) {
            $activeModule = $moduleList[$activeModuleName];

            $bootstrapMethodNames = get_class_methods($bootstrap);
            foreach ($bootstrapMethodNames as $key=&gt;$method) {
                $runMethod = false;
                $methodNameLength = strlen($method);
                if ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                    call_user_func(array($bootstrap,$method));
                }
            }
        } else {
            $activeModule = $bootstrap;
        }

        $methodNames = get_class_methods($activeModule);
        foreach ($methodNames as $key=&gt;$method) {
            $runMethod = false;
            $methodNameLength = strlen($method);
            if (10 &lt; $methodNameLength &amp;&amp; &#039;activeInit&#039; === substr($method, 0, 10)) {
                $runMethod = true;
            } elseif ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                $runMethod = true;
            }
            if ($runMethod) {
                call_user_func(array($activeModule,$method));
            }
        }
    }
}
</pre>
<p>Next we need to make sure the Controller Plugin is loaded.<br />
We can do this in one of two ways. Either in the Application Bootstrap via an _init function</p>
<pre class="brush: php">
public function _initControllerPlugins()
{
    $plugin = Zend_Controller_Front::getInstance()-&gt;registerPlugin(
        new BinaryKitten_ModuleConfig()
    );
}
</pre>
<p>*&#8211; Or &#8211;*</p>
<p>We can add a line to the application.ini</p>
<pre class="brush: text">
resources.frontController.plugins.BKModuleConfig = &quot;BinaryKitten_ModuleConfig&quot;
</pre>
<p>Finally some example init code from the module bootstrap.<br />
Please remember that the activeInit*() Functions need to be public for this to work properly</p>
<pre class="brush: php">
class Default_Bootstrap extends Zend_Application_Module_Bootstrap {
    public function activeInitMenus() {
        $layout = $this
                    -&gt;bootstrap(&#039;layout&#039;)
                    -&gt;getResource(&#039;layout&#039;);

        $view = $layout-&gt;getView();
        $config = new Zend_Config_Xml(APPLICATION_PATH.&#039;/configs/navigation_default.xml&#039;,&quot;menu&quot;);
        $navigation = new Zend_Navigation($config);
        $view-&gt;navigation($navigation);
    }
    public function activeInitDoSomethingElse() {
        /* some other code */
    }
    public function defaultInitSomething() {
    	/* more code */
    }
}
</pre>
<p>We can also add module inits to the application bootstrap like so:</p>
<pre class="brush: php">
    public function modulenameInitFunction() {
    	/* place code here */
    }
</pre>
<p>Where modulename is the lowercase version of the Modules name, eg if you have the Admin module, then you would use:</p>
<pre class="brush: php">
    public function adminInitFunction() {
    	/* place code here */
    }
</pre>
<p>Hopefully someone will find this code useful.</p>
<p>[ Edit January 5th 2010 ]<br />
Thanks to:<br />
	Matthew Weier O&#8217;Phinney for pointing out places for update.<br />
	Rob Allen (@Akrabat) for the info that the plugin could be loaded via the application.ini<br />
	Elizabeth Marie Smith and Matthew Turland for questioning the use of the Reflection.</p>
<ul>
<li>Updated the Bootstrap code to properly define the functions as Public</li>
<li>Removed the reflection as this wasn&#8217;t actually required any more.</li>
<li>Updated Post to make clean up the order of things and to properly designate that the BinaryKitten is the &#8220;Namespace&#8221;<br />
&#8220;Namespace&#8221; is used to reference that we&#8217;re not using PHP5.3 Namespaces, but the Namespaces within the Zend Framework.</li>
</ul>
<p>[ Edit January 11th 2010 ]<br />
Thanks to:<br />
	septem for pointing out a typo where i had $boostrap instead of $bootstrap<br />
	Gerard Roche for pointing out that by default, the default module doesn&#8217;t require a module bootstrap (in my code it has one)</p>
<ul>
<li>Updated to fix the typos</li>
<li>Added in a quick check to see if the module exists in the modules list of bootstraps</li>
<li>Removed the _ from the function name that it searches for, this should please the people who are adamant over the Zend Coding Standards</li>
<li>Added in the functionality to have $modulenameInit() functions as well in both active module bootstrap and the application bootstrap</li>
</ul>
<p>[ Edit February 13th 2010 ]<br />
Ran the code through codesniffer against the Zend Standard supplied.. updated so no errors found. 3 Warnings are left .. they are as follows:<br />
22 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
35 | WARNING | Line exceeds 80 characters; contains 84 characters<br />
38 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
Don&#8217;t think it&#8217;s worth the change for 3/4 characters </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Zend Framework – Getting Single Components</title>
		<link>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html#comments</comments>
		<pubDate>Sun, 22 Feb 2009 19:43:17 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=126</guid>
		<description><![CDATA[Recently i was looking at implementing some caching to recent websites.. 1 to improve load times as well as a possibility to improve the way that my &#8220;Framework&#8221; works. I had just recently looked over Rob Allen&#8217;s (@Akrabat http://akrabat.com/) PHP Nw 08 Conference Talk on &#8220;First Steps with Zend Framework&#8221;. As I had not been [...]]]></description>
			<content:encoded><![CDATA[<p>Recently i was looking at implementing some caching to recent websites.. 1 to improve load times as well as a possibility to improve the way that my &#8220;Framework&#8221; works.</p>
<p>I had just recently looked over Rob Allen&#8217;s (<a href="http://www.twitter.com/akrabat" target="_blank"><a href="http://twitter.com/Akrabat">@Akrabat</a></a> <a href="http://akrabat.com/" target="_blank">http://akrabat.com/</a>) <a href="http://conference.phpnw.org.uk/" target="_blank">PHP Nw 08 Conference</a> Talk on &#8220;First Steps with Zend Framework&#8221;. As I had not been really feeling all that great at the conference itself, I was glad to be able to watch teh recorded version on the web. During the 1st segment of the talk, Rob described how zend cache was his first foray into the Zend Framework and this lead me onto how I was thinking about how the best way to implement the caching on my site(s).</p>
<p>So, with this in mind, off I trotted to <a href="http://framework.zend.com" target="_blank">http://framework.zend.com</a> to grab the Zend Cache component that I wanted to look at. That&#8217;s where I hit a snag. It seemed impossible to actually get the components on their own. After about half and hour of scouring the site I still couldn&#8217;t find what I wanted, so I <a href="http://twitter.com/BinaryKitten/status/1237645252" target="_self">Tweeted about my annoyance</a>..   To which I got a couple of replies&#8230; the 1st <a href="http://twitter.com/rchiswell/status/1237680023" target="_blank">reply</a> came from Richard Chiswell (<a href="http://twitter.com/rchiswell" target="_blank">@rchiswell</a>) who said:</p>
<p><em><span class="status-body"><span class="entry-content">@<a href="http://twitter.com/BinaryKitten">BinaryKitten</a> You might find the Dependencies list at <a rel="nofollow" href="http://bit.ly/199FYR" target="_blank">http://bit.ly/199FYR</a> useful. You could try contacting @<a href="http://twitter.com/CalEvans">CalEvans</a> who wrote a book on ZF</span></span></em></p>
<p><span class="status-body"><span class="entry-content">The bit.ly link took me to: http://framework.zend.com/manual/en/requirements.html#requirements.dependencies  the listing of the components and their dependencies, Though this was </span></span><span class="status-body"><span class="entry-content">helpful it wasn&#8217;t really as helpful as the <a href="http://twitter.com/CalEvans/status/1237686345" target="_blank">reply</a> i got from Cal Evans (<a href="http://twitter.com/CalEvans" target="_blank">@CalEvans</a>):</span></span></p>
<p><em><span class="status-body"><span class="entry-content">@<a href="http://twitter.com/BinaryKitten">BinaryKitten</a> <a rel="nofollow" href="http://epic.codeutopia.net/pack/" target="_blank">http://epic.codeutopia.net/pack/</a></span></span></em></p>
<p><span class="status-body"><span class="entry-content">Short Sweet and Wham.. That is exactly what I needed.</span></span></p>
<p><span class="status-body"><span class="entry-content">Shortly afterwards <a href="http://blog.rac.me.uk/2009/02/22/php-getting-individual-packages-on-zend-framework/" target="_blank">Richard blogged</a> about this, which in turn made me feel like I should blog about it as well, and oooh look.. So I have.</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

