<?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>LAMPlights &#187; zendframework</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/zendframework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Absolute URL&#8217;s In The View</title>
		<link>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/</link>
		<comments>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 00:22:56 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[factorymethod]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=374</guid>
		<description><![CDATA[We recently had a project at work that involved replacing all the relative URL's from the application with absolute URL's. In the past, developers had just hard-coded an absolute URL only when they need to force the browser over to https. Now we are using multiple subdomains, so this approach is no longer sufficient. We [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had a project at work that involved replacing all the relative URL's from the application with absolute URL's.  In the past, developers had just hard-coded an absolute URL only when they need to force the browser over to https.  Now we are using multiple subdomains, so this approach is no longer sufficient.  We also wanted a way to easy rotate assets through multiple CDN URL's to speed up the time it takes a user's web browser to load all the content.<br />
<span id="more-374"></span><br />
There are two requirements:</p>
<ol>
<li>Prefix a relative url path with a host.</li>
<li>Rotate a set of relative url's through a given number of cdn hosts.</li>
</ol>
<p>We currently use Zend Framework at work, so it is only logical we use as much of the framework as possible.&nbsp; The straight forward approach is to extend Zend_View_Helper_Abstract and create a few functions to fill the requirements.&nbsp; There are few problems with this approach.&nbsp; The first is function creep.&nbsp; We know we need to prefix http://www.hautelook.com and https://www.hautelook.com, but there may be more later.&nbsp; We may decide to use something like https://ssl.hautelook.com or require another subdomain like http://ftp.hautelook.com.&nbsp; This would require us to create another function, write some unit tests for it and finally send it through QA.&nbsp; This issue is even more compounded with the cdn rotation function.</p>
<p>Another problem is that we need absolute url's in some of our business logic.&nbsp; We have webservices that serve up XML or JSON that contain locations to such as images or a catalog.&nbsp; We want these services to take advantage of the absolute url logic too.&nbsp; If we implement a view helper, then the service layer becomes coupled to the view in order to reuse the logic.</p>
<p>Enter PHP 5.3, <a class="zem_slink" href="http://en.wikipedia.org/wiki/Functional_programming" title="Functional programming" rel="wikipedia">functional programming</a> and an inspirational post from <a href="http://eliw.wordpress.com/2010/03/10/an-intriguing-use-of-lambda-functions/">Eli White</a>.&nbsp; We can use the <a class="zem_slink" href="http://en.wikipedia.org/wiki/Factory_method_pattern" title="Factory method pattern" rel="wikipedia">factory method</a> pattern and closures to meet all the requirements and isolate the parts that change.&nbsp; Read <a href="http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/">these</a> <a href="http://www.hermanradtke.com/blog/php-5-3-is-the-new-javascript-almost/">posts</a> if you are not familiar with functional programming in PHP.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Crimson_Url
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> absolute<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$host}</span><span style="color: #006699; font-weight: bold;">{$path}</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> rotate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subdomain</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotations</span><span style="color: #339933;">,</span> <span style="color: #000088;">$protocol</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$current</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subdomain</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotations</span><span style="color: #339933;">,</span> <span style="color: #000088;">$protocol</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: #000088;">$current</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$rotations</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</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;">$current</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$protocol}</span>://<span style="color: #006699; font-weight: bold;">{$subdomain}</span><span style="color: #006699; font-weight: bold;">{$current}</span>.<span style="color: #006699; font-weight: bold;">{$host}</span><span style="color: #006699; font-weight: bold;">{$path}</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The absolute function demonstrates how simple, yet powerful, closures can be.&nbsp; The absolute function is creating a function that concatenates to strings together: a host name and a relative url.&nbsp; Here is how we would use this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$www</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">absolute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ssl</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">absolute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$www</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ssl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/secure.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>Output:</p>
<pre>http://www.example.com/items.php

https://www.example.com/secure.php
</pre>
<p>Take this one step further and think about how you can use the HTTP_HOST or HTTP_REFERER values from $_SERVER to make absolute URL generation almost completely automatic.</p>
<p>The rotate function is slightly more complex.&nbsp; The $current variable is being declared in the factory method and passed by reference.&nbsp; We must explicitly pass this by reference otherwise PHP will pass it by value and the value of $current will be 1 everytime.&nbsp; Here is how we would use the rotater:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cdn'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hautelook.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sslCdn</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cdn'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hautelook.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'https'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bar.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/baz.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bob.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sslCdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sslCdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bar.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>Output:</p>
<pre>http://cdn1.hautelook.com/foo.jpg

http://cdn2.hautelook.com/bar.jpg

http://cdn3.hautelook.com/baz.jpg

http://cdn1.hautelook.com/bob.jpg

https://cdn1.hautelook.com/foo.jpg

https://cdn2.hautelook.com/bar.jpg
</pre>
<p>Notice how there are no problems with static variable conflicts.  Each function is independent and can be used for completely different tasks.</p>
<p>The complete source code, with tests and documentation, can be found here: <a href="http://github.com/hradtke/crimson/tree/master/Crimson_Url">http://github.com/hradtke/crimson/tree/master/Crimson_Url</a></pre>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/b602316b-6c98-4996-aba2-9daac249577a/" title="Reblog this post [with Zemanta]"><img style="border: medium none; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=b602316b-6c98-4996-aba2-9daac249577a" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP goes functional in version 5.3</title>
		<link>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/</link>
		<comments>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 08:25:17 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[li3]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=363</guid>
		<description><![CDATA[It has been said that all languages, over time, implement a dialect of lisp.  PHP appears to be no exception.  In PHP 5.3 lambda (or anonymous) functions and closures were added to the language.  These give PHP somewhat of a functional feel and can be very useful tools for solving problems.  One great example is [...]]]></description>
			<content:encoded><![CDATA[<p>It has been said that all languages, over time, implement a dialect of lisp.  <a class="zem_slink" title="PHP" rel="homepage" href="http://www.php.net/">PHP</a> appears to be no exception. <br />
<span id="more-363"></span><br />
In PHP 5.3 lambda (or anonymous) functions and closures were added to the language.  These give PHP somewhat of a functional feel and can be very useful tools for solving problems.  One great example is the <a href="http://rad-dev.org/lithium">Lithium</a> framework use of closures to implement filters.  Filters are an implementation of <a class="zem_slink" title="Aspect-oriented programming" rel="wikipedia" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">aspect oriented programming</a> (AOP).  Other frameworks, like <a class="zem_slink" title="Zend Framework" rel="homepage" href="http://framework.zend.com/">Zend Framework</a>, are looking at these new functional tools to streamline complex and/or heavy components.  I strongly believe that these new functional features will be the new "hotness" for PHP in the coming year.  Expect quite a few conference sessions and tutorials devoted this very topic.</p>
<p>All this talk of <a class="zem_slink" title="Functional programming" rel="wikipedia" href="http://en.wikipedia.org/wiki/Functional_programming">functional language</a> features is great, but Lithium is still in development, Zend Framework 2.0 is a ways away, you have an upcoming project deadline and you want to know how this stuff can help you now.  Easy: go read or watch JavaScript examples.  Seriously.  Watching <a class="zem_slink" title="Douglas Crockford" rel="homepage" href="http://crockford.com/">Douglas Crockford</a>'s videos on YUI theatre or reading through <a class="zem_slink" title="Ext (JavaScript library)" rel="homepage" href="http://extjs.com/">ExtJS</a> source code can give you really good insight into the power of functional language features.  The syntax may be different, but the core ideas are the same.</p>
<p>The new functional feel of PHP seems to have started a trend towards a unified constructor.  This has been a common practice in the JavaScript community for some time.  The great thing is that the PHP array is very similar to the JavaScript object.  In fact, the <a class="zem_slink" title="JSON" rel="homepage" href="http://json.org">JSON</a> extension to PHP makes them completely interchangeable.  Here is a quick example of one unified constructor implementation in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hi'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'adder'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'c'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'d'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> base <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #000000; font-weight: bold;">extends</span> base <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I have a bunch of ideas (some great, some maybe not so great) that I plan on implementing using lambda functions and closures.  I will be adding new components to my <a href="http://github.com/hradtke/crimson">Crimson</a> library and discussing them here.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/276ba760-1b29-4059-bb14-8c6143cf67fc/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=276ba760-1b29-4059-bb14-8c6143cf67fc" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Mocking Zend Framework&#8217;s Row and Rowset objects</title>
		<link>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/</link>
		<comments>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 05:32:54 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[unittest]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=339</guid>
		<description><![CDATA[If you separate your business logic from your data access logic, the last thing you want to do is make your business logic unit tests reliant on the database.  This is normally not a big deal: retrieve the data, store it in an array and pass it off to the class with the business logic.  [...]]]></description>
			<content:encoded><![CDATA[<p>If you separate your business logic from your data access logic, the last thing you want to do is make your business logic unit tests reliant on the database.  This is normally not a big deal: retrieve the data, store it in an array and pass it off to the class with the business logic.  Mocking the data for the unit test simply requires you to hardcode from array information in the test.  However, I recently ran into a case where I wanted to pass Zend_Db_Table_Row and Zend_Db_Table_Row objects to the business logic and mocking them was not so easy.</p>
<p><span id="more-339"></span>I first attempted to mock Zend_Db_Table_Row using PHPUnit's _getMock method.  This proved to be an exercise in futility.  I did not want the class to connect to the database to verify whether or not the columns were valid.  After a few frustrating hours, I started to wonder how Zend was unit testing Zend_Db_Table_Row.  So I downloaded the full version of the latest Zend Framwork and started poking around.  I stumbled upon My_ZendDbTable_Row_TestMockRow hiding away in ZendFramework-1.9.5/tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php.  I will not go into what was done to make it a usable mock, but I will show you how to use it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework/TestCase.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestMockRowTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testRowHasIdValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'first_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Herman'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'last_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Radtke'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'herman@example.com'</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_ZendDbTable_Row_TestMockRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Herman'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Radtke'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'herman@example.com'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Creating a mock row object is much like hardcoding an array.   Define an array that has a single key 'data' that contains an array as a value.   Inside this array, the database column name is the array key and the database column value is the array value.</p>
<p>That class works great for mocking a single row object, but I still needed a solution for multiple row objects.  I expected to find a class similar to My_ZendDbTable_Row_TestMockRow for the purposes of testing Zend_Db_Table_Rowset, but none existed.  Fortunately, it took only a few minutes to create my own.  All one has to do is specify the name of the row class for the rowset class to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework/TestCase.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'library/Zend/Db/Table/Rowset/Abstract.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ZendDbTableMockRowset <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Rowset_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_rowClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'My_ZendDbTable_Row_TestMockRow'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> RowsetTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$rowset</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZendDbTableMockRowset<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'data'</span><span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123456</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123457</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123458</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123459</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123460</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123456</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rowset</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> instanceof My_ZendDbTable_Row_TestMockRow<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$id</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>

<p>The array passed to the mock rowset has a similar structure to the one we used above.   The only difference is that we have multiple arrays, inside the 'data' array, each representing one row.   For testing purposes, I created an 'id' field.   I normally would not ever use an artificial key field inside a business logic class since that value is very dependent on the database.</p>
<p>I created a ZendDbTableMockRowset class in my projects test/mocks directory so I can use it in multiple test files.  I also copied My_ZendDbTable_Row_TestMockRow into the test/mocks directory so I would not be dependent on the external tests from Zend Framework.  Now mocking Row or Rowset objects is just as fast as mocking arrays.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Speaking at Jan. 20th LA LAMP meetup</title>
		<link>http://www.hermanradtke.com/blog/speaking-at-jan-20th-la-lamp-meetup/</link>
		<comments>http://www.hermanradtke.com/blog/speaking-at-jan-20th-la-lamp-meetup/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 19:16:52 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[lamp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[usergroup]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=269</guid>
		<description><![CDATA[I will be giving a short presentation on HauteLook's architecture at the LA LAMP meetup on January 20th.  I have been meaning to join a user group for some time, so this worked out perfectly.  Any developers in the LA area on that day should come, even if it is just for the free food [...]]]></description>
			<content:encoded><![CDATA[<p>I will be giving a short presentation on HauteLook's architecture at the <a title="LA LAMP website" href="http://www.meetup.com/lalamp/">LA LAMP meetup</a> on January 20th.  I have been meaning to join a user group for some time, so this worked out perfectly.  Any developers in the LA area on that day should come, even if it is just for the free food and beer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/speaking-at-jan-20th-la-lamp-meetup/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Zend_Soap_AutoDiscover and eAccelerator</title>
		<link>http://www.hermanradtke.com/blog/zend_soap_autodiscover-and-eaccelerator/</link>
		<comments>http://www.hermanradtke.com/blog/zend_soap_autodiscover-and-eaccelerator/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 05:14:13 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eaccelerator]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=255</guid>
		<description><![CDATA[The Zend Framework ships with SOAP functionality and one especially neat class called Zend_Soap_AutoDiscover.  This class uses a comment docblock to auto-generate a WSDL at runtime.  I won't go into the details how it works here, but you can check the Zend Framework documentation for an example.  When using this class at work, I noticed [...]]]></description>
			<content:encoded><![CDATA[<p>The <a class="zem_slink" title="Zend Framework" rel="homepage" href="http://framework.zend.com/">Zend Framework</a> ships with <a class="zem_slink" title="SOAP" rel="wikipedia" href="http://en.wikipedia.org/wiki/SOAP">SOAP</a> functionality and one especially neat class called Zend_Soap_AutoDiscover.  <a href="http://framework.zend.com/"><img class="alignright" title="Zend Framework Logo" src="http://framework.zend.com/images/logo_small.gif" alt="" width="123" height="23" /></a>This class uses a comment docblock to auto-generate a <a class="zem_slink" title="Web Services Description Language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> at runtime.  I won't go into the details how it works here, but you can check the <a href="http://framework.zend.com/manual/en/zend.soap.autodiscovery.html">Zend Framework documentation</a> for an example.  When using this class at work, I noticed the WSDL would not always generate correctly.  After a lot of digging around, I found the cause: eAccelerator.</p>
<p><span id="more-255"></span></p>
<p>eAccelerator is an opcode cache.  An opcode cache saves the compiled version of a script so <a class="zem_slink" title="PHP" rel="homepage" href="http://www.php.net/">PHP</a> does not have to parse it again.  eAccelerator also<a href="http://eaccelerator.net/ticket/229"> strips out comments</a> from the script by default.  That means once your script gets cached, the Zend_Soap_AutoDiscover class has no way of correctly auto-generating the WSDL.</p>
<p><a href="http://eaccelerator.net/"><img class="alignnone" title="eAccelerator logo" src="http://eaccelerator.net/chrome/common/eaccelerator.png" alt="" width="247" height="33" /></a></p>
<p>Fortunately, there are some easy solutions to this problem.  The easiest is to configure eAccelerator 0.9.6 to not strip out the comments.  When compiling eAccelerator, specify the --with-eaccelerator-doc-comment-inclusion switch in the configure script.  Install the new compiled version and make sure to <em>clean</em> out your existing cache directory.</p>
<p>Now if you are running eAccelerator 0.9.5, like me, and are unable to upgrade there is still hope.  eAccelerator comes with some ini settings that allow us to not cache scripts.  Using the eaccelerator.filter setting, we can tell eAccelerator to ignore scripts by filename.  You can set it in your php.ini file or just specify it at runtime:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'eaccelerator.filter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'!foo/Bar.php !baz.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With comments preseved, the Zend_Soap_AutoDiscover class will be able to properly generate the WSDL.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/a0294b42-5e52-4c18-a54b-c96c3d9d7afa/"><img class="zemanta-pixie-img" style="border: medium none ; float: left;" src="http://img.zemanta.com/reblog_e.png?x-id=a0294b42-5e52-4c18-a54b-c96c3d9d7afa" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/zend_soap_autodiscover-and-eaccelerator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Why I am not running to Solar</title>
		<link>http://www.hermanradtke.com/blog/why-i-am-not-running-to-solar/</link>
		<comments>http://www.hermanradtke.com/blog/why-i-am-not-running-to-solar/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 03:30:29 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[solar]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=188</guid>
		<description><![CDATA[Some of my thoughts on Paul M. Jones post about Solar and Zend Framework.  This is less of a defense of Zend Framework and more of a commentary on Paul's framework ideas. I favor design by contract so I can properly type-hint method parameters.  A framework should be written in a way I can safely [...]]]></description>
			<content:encoded><![CDATA[<p>Some of my thoughts on <a href="http://paul-m-jones.com/?p=1113">Paul M. Jones post about Solar and Zend Framework</a>.  This is less of a defense of Zend Framework and more of a commentary on Paul's framework ideas.</p>
<p>I favor design by contract so I can properly type-hint method parameters.  A framework should be written in a way I can safely extend the crap out of.  This is the exact use case of interfaces.</p>
<p>Universal constructors make the code harder to read.  I see this as a sign that inheritance is being used way too much.  Compose people, compose!</p>
<p>The registry pattern is not any better than a singleton.  They both have global scope.</p>
<p>I do like that there are so many ways to inject dependencies.  The MVC framework is also pretty nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/why-i-am-not-running-to-solar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More Reliable Authentication in Zend Framework</title>
		<link>http://www.hermanradtke.com/blog/more-reliable-authentication-in-zend-framework/</link>
		<comments>http://www.hermanradtke.com/blog/more-reliable-authentication-in-zend-framework/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 05:10:08 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=91</guid>
		<description><![CDATA[Stefan Esser gave a presentation on Secure Programming with the Zend Framework at the 2009 Dutch PHP Conference. While the presentation was good, one thing that bothered me was the way authentication was being handled. Note that this a very minimal example of how I handle authentication. Please make sure to completely implement, document and [...]]]></description>
			<content:encoded><![CDATA[<p>Stefan Esser gave a presentation on <a href="http://www.suspekt.org/2009/06/16/dutch-php-conference-the-slides/">Secure Programming with the Zend Framework at the 2009 Dutch PHP Conference</a>.  While the presentation was good, one thing that bothered me was the way authentication was being handled.</p>
<p><span id="more-91"></span></p>
<p><strong>Note that this a very minimal example of how I handle authentication.  Please make sure to completely implement, document and test before putting this on a production server.</strong></p>
<p>Stefan's suggested implementation is to extend the Zend_Controller_Action class to create a custom controller class and use the init() method to determine if the user is logged in.  Developers are expected to then extend this new class to create specific controllers.  The obvious problem with this approach, as Stefan mentions, is that anyone who overloads the init() method must make sure to call the parent's init() method.  If security relies on an individual developer remembering to call the correct methods, then the system is not very secure.  Stefan makes a comment similar to this in the XSS section of his presentation.</p>
<p>I think the better solution to handle the authentication is to create a controller plugin.  The authentication controller plugin is registered in one spot, the bootstrap, thus making it much more reliable.  There really isn't much more work to do to use a plugin either.</p>
<p>Start by extending the Zend_Controller_Plugin_Abstract class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Authentication <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<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: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">setDispatched</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// handle unauthorized request...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the bootstrap, make sure to register the plugin before calling the front controller's dispatch() method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$controller</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: #000088;">$controller</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> My_Authentication<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;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above method works fine if your login page is outside your MVC routing.  If you use a login controller, there is some more work to do.  A white-list of controllers and actions must be maintained to signal the authentication adapter that authentication is not required.  I keep the white-list as a hard-coded array inside the authentication plugin class itself.  Our class then becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Authentication <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_whitelist</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> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_whitelist <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'index/login'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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> preDispatch<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: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getControllerName</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;">$action</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionName</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;">$route</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$controller</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$action</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_whitelist<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">setDispatched</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// handle unauthorized request...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/more-reliable-authentication-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

