<?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>vandenbos.org &#187; plugins</title>
	<atom:link href="http://blog.vandenbos.org/tag/plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.vandenbos.org</link>
	<description>Matthijs van den Bos&#039; thoughts on web development topics</description>
	<lastBuildDate>Mon, 08 Feb 2010 08:11:47 +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: module specific frontcontroller plugins</title>
		<link>http://blog.vandenbos.org/2009/09/03/zend-framework-module-specific-frontcontroller-plugins/</link>
		<comments>http://blog.vandenbos.org/2009/09/03/zend-framework-module-specific-frontcontroller-plugins/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 11:33:08 +0000</pubDate>
		<dc:creator>Matthijs</dc:creator>
				<category><![CDATA[zend framework]]></category>
		<category><![CDATA[frontcontroller]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[Zend_Application]]></category>

		<guid isPermaLink="false">http://blog.vandenbos.org/?p=158</guid>
		<description><![CDATA[Here is another situation I have run into while trying to build a modular application: sometimes you need to run a frontcontroller plugin ONLY when a specific module is requested. An example could be authentication. Say I have a public site and an admin section. The most obvious solution is to enable the authentication plugin [...]]]></description>
			<content:encoded><![CDATA[<p>Here is another situation I have run into while trying to build a modular application: sometimes you need to run a frontcontroller plugin ONLY when a specific module is requested. An example could be authentication. Say I have a public site and an admin section. The most obvious solution is to enable the authentication plugin only when the admin section is requested. This is something that is not possible with Zend_Application and the frontcontroller resource. So, as I did for <a href="http://blog.vandenbos.org/2009/07/19/zend-framework-module-specific-layout/">loading module specific layouts</a>, I wrote a frontcontroller plugin for registering module specific frontcontroller plugins: the Rexus_Controller_Plugin_RequestedModulePluginLoader. It works together with a Zend_Application resource called Rexus_Application_Resource_Moduleplugins. Let&#8217;s get started.</p>
<p><span id="more-158"></span></p>
<h2>Usage Example</h2>
<p>
First we load the frontcontroller plugin Rexus_Controller_Plugin_RequestedModulePluginLoader in the main application.ini:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">resources<span style="color: #339933;">.</span>frontController<span style="color: #339933;">.</span>plugins<span style="color: #339933;">.</span>pluginloader <span style="color: #339933;">=</span> Rexus_Controller_Plugin_RequestedModulePluginLoader<span style="color: #339933;">;</span></pre></div></div>

<p>Then we use the Zend_Application resource Rexus_Application_Resource_Moduleplugins in our config file to load the the authentication plugin only for our module.<br />
If you use my ModuleSetup resource as described in <a href="http://blog.vandenbos.org/2009/07/07/zend-framework-module-config/">module config</a>, you can set the module plugin options in the module.ini for that module :</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">resources<span style="color: #339933;">.</span>moduleplugins<span style="color: #339933;">.</span>auth <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Rexus_Controller_Plugin_Authentication&quot;</span></pre></div></div>

<p>or, you can set the module plugins config in your application.ini normally, as described in the Zend Framework reference:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">modulename<span style="color: #339933;">.</span>resources<span style="color: #339933;">.</span>moduleplugins<span style="color: #339933;">.</span>auth <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Rexus_Controller_Plugin_Authentication&quot;</span></pre></div></div>

<p>The result is that the frontcontroller plugin Rexus_Controller_Plugin_Authentication is only loaded if the module &#8216;modulename&#8217; is requested.</p>
<h2>Code</h2>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Rexus_Controller_Plugin_RequestedModulePluginLoader 
    <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$_modulePlugins</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerFrontControllerPlugin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pluginName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#41;</span> 
                        <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#91;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#91;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pluginName</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;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#91;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> routeShutdown<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#91;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$frontController</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_modulePlugins<span style="color: #009900;">&#91;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$pluginName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$frontController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPlugin</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$pluginName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Just drop this class in your library and register the plugin with the frontcontroller as shown above to use it.</p>
<p>Note that this plugin does its work on routeShutdown. On routShutDown is the earliest hook in the dispatch loop where the request has been parsed and routed. This is important because all plugins we register with this plugin can subsequently only be registered with the frontcontroller as early as the next step after the one on which this plugin is executed. </p>
<p>Next we need the code for the Zend_Application resource Rexus_Application_Resource_Moduleplugins:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Rexus_Application_Resource_Moduleplugins 
    <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$bootstrap</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$bootstrap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'frontcontroller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$front</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bootstrap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'frontcontroller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pluginLoader</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$front</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPlugin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Rexus_Controller_Plugin_RequestedModulePluginLoader'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$pluginName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$pluginLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerFrontControllerPlugin</span><span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBootstrap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</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;">$pluginName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.vandenbos.org/2009/09/03/zend-framework-module-specific-frontcontroller-plugins/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
