<?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>Rob Allen&#039;s DevNotes &#187; Zend Framework</title>
	<atom:link href="http://akrabat.com/category/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://akrabat.com</link>
	<description>Developing PHP software in the Real World, by Rob Allen</description>
	<lastBuildDate>Fri, 03 Feb 2012 11:20:24 +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>Jason Grimes: Using Doctrine 2 in Zend Framework 2</title>
		<link>http://akrabat.com/zend-framework/jason-grimes-using-doctrine-2-in-zend-framework-2/</link>
		<comments>http://akrabat.com/zend-framework/jason-grimes-using-doctrine-2-in-zend-framework-2/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 08:10:41 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1689</guid>
		<description><![CDATA[Jason Grimes has posted an article showing how to use Doctrine 2 with Zend Framework 2. He uses my tutorial as the starting point which enables him to concentrate on the Doctrine integration rather than the irrelevant details about setting a ZF2 application which is excellent. He walks through 6 steps in order to do [...]]]></description>
			<content:encoded><![CDATA[<p>Jason Grimes has posted an article showing <a href="http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/">how to use Doctrine 2 with Zend Framework 2</a>. </p>
<p>He uses my <a href="/getting-started-with-zend-framework-2/">tutorial</a> as the starting point which enables him to concentrate on the Doctrine integration rather than the irrelevant details about setting a ZF2 application which is excellent.</p>
<p>He walks through 6 steps in order to do the integration:</p>
<blockquote><p><em>This article shows how to set up and use Doctrine 2 in Zend Framework 2, by extending Rob’s Getting Started tutorial to use Doctrine instead of Zend_Db.</em></p>
<ul>
<li><em>Start with Akrabat’s tutorial</em></li>
<li><em>Install Doctrine modules</em></li>
<li><em>Configure the Album module to use Doctrine</em></li>
<li><em>Create the Album entity</em></li>
<li><em>Update the Album controller to use Doctrine instead of Zend_Db</em></li>
<li><em>That’s it!</em></li>
</ul>
</blockquote>
<p>I highly recommend having a read if you're at all interested in using Doctrine 2 with Zend Framework 2.</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1689&amp;md5=5338e3d8552c41ef0f8bda91e58ed6f0" title="Flattr" target="_blank"><img src="http://akrabat.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/jason-grimes-using-doctrine-2-in-zend-framework-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework 2 Beta 2 released</title>
		<link>http://akrabat.com/zend-framework/zend-framework-2-beta-2-released/</link>
		<comments>http://akrabat.com/zend-framework/zend-framework-2-beta-2-released/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 19:58:22 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1664</guid>
		<description><![CDATA[Zend Framework 2, Beta 2 has been released! The key new features are: Refactored Mail component Refactored Cache component MVC updates Check out Matthew's blog post for the full details. I've also updated my tutorial. This is a good time to get involved, try it out and let us know what you like/dislike.]]></description>
			<content:encoded><![CDATA[<p><a href="http://framework.zend.com/zf2">Zend Framework 2</a>, Beta 2 has been released!</p>
<p>The key new features are:</p>
<ul>
<li>Refactored Mail component</li>
<li>Refactored Cache component</li>
<li>MVC updates</li>
</ul>
<p>Check out Matthew's <a href="http://framework.zend.com/zf2/blog/entry/Zend-Framework-2-0-0beta2-Released">blog post</a> for the full details.</p>
<p>I've also updated my <a href="http://akrabat.com/zf2t">tutorial</a>. This is a good time to get involved, try it out and let us know what you like/dislike.</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1664&amp;md5=934f4f7485b8c822b85879be0cbbccef" title="Flattr" target="_blank"><img src="http://akrabat.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/zend-framework-2-beta-2-released/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Updated tutorial for Zend Framework 2 beta 1</title>
		<link>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-2-beta-1/</link>
		<comments>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-2-beta-1/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 15:00:12 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1649</guid>
		<description><![CDATA[With the announcement of Zend Framework beta 1, I have updated my venerable tutorial to work with it! Getting started with Zend Framework 2 (beta1), creates the same application as my ZF1 tutorial, so it should be very familiar, but this time, it's in the context of Zend Framework 2. As usual, it's a PDF [...]]]></description>
			<content:encoded><![CDATA[<p>With the announcement of <a href="http://devzone.zend.com/article/17798-Zend-Framework-2.0.0beta1-Released">Zend Framework beta 1</a>, I have updated my venerable tutorial to work with it!</p>
<p><a href="http://akrabat.com/zf2t/">Getting started with Zend Framework 2 (beta1)</a>, creates the same application as my ZF1 tutorial, so it should be very familiar, but this time, it's in the context of Zend Framework 2. As usual, it's a PDF too.</p>
<p>Please download it, try it out and let me know if you find any typos!</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-2-beta-1/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Zend_Config_Ini and a string</title>
		<link>http://akrabat.com/zend-framework/zend_config_ini-and-a-string/</link>
		<comments>http://akrabat.com/zend-framework/zend_config_ini-and-a-string/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 14:09:52 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1565</guid>
		<description><![CDATA[One thing that is different between Zend_Config_Xml and Zend_Config_Ini is that with Zend_Config_Xml you can pass in an XML string as the first parameter of the constructor and it will work. This doesn't work with Zend_Config_Ini as we use parse_ini_file() under the hood. With PHP 5.3 however there is is a new function called parse_ini_string() [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that is different between Zend_Config_Xml and Zend_Config_Ini is that with Zend_Config_Xml you can pass in an XML string as the first parameter of the constructor and it will work. This doesn't work with Zend_Config_Ini as we use parse_ini_file() under the hood.</p>
<p>With PHP 5.3 however there is is a new function called parse_ini_string() which will allow us to load arbitrary ini string into Zend_Config objects. This can't go into Zend Framework 1 though due to our PHP 5.2.4 minimum version requirement. </p>
<p>As I needed this for a project, I extended Zend_Config_Ini to support this feature, which means simply overloading a single method</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;App_Config_Ini&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Config_Ini
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Load&nbsp;the&nbsp;INI&nbsp;file&nbsp;from&nbsp;disk&nbsp;using&nbsp;parse_ini_file().&nbsp;Use&nbsp;a&nbsp;private&nbsp;error
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;handler&nbsp;to&nbsp;convert&nbsp;any&nbsp;loading&nbsp;errors&nbsp;into&nbsp;a&nbsp;Zend_Config_Exception
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$filename
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@throws&nbsp;Zend_Config_Exception
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">protected&nbsp;function&nbsp;</span><span style="color: #0000BB">_parseIniFile</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$this</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'_loadFileErrorHandler'</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #DD0000">'.ini'</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iniArray&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iniArray&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parse_ini_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">restore_error_handler</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Check&nbsp;if&nbsp;there&nbsp;was&nbsp;a&nbsp;error&nbsp;while&nbsp;loading&nbsp;file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_loadFileErrorStr&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@see&nbsp;Zend_Config_Exception
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'Zend/Config/Exception.php'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Config_Exception</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_loadFileErrorStr</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$iniArray</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>The actual change is to see if the last 4 characters of the filename are ".ini" and if they aren't then use parse_ini_string() instead of parse_ini_file(). The rest of the code is just error handling.</p>
<p>This is one area where I really like it when a class implements methods that done just one thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/zend_config_ini-and-a-string/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Exploring Zend_Paginator</title>
		<link>http://akrabat.com/zend-framework/exploring-zend-paginator/</link>
		<comments>http://akrabat.com/zend-framework/exploring-zend-paginator/#comments</comments>
		<pubDate>Wed, 04 May 2011 07:42:37 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1508</guid>
		<description><![CDATA[One area of displaying lists on web pages that I've generally disliked doing is pagination as it's a bit of a faff. Recently, I needed to do just this though as I couldn't delegate it as my colleague was too busy on other work. As a result, I thought that I should look into Zend_Paginator [...]]]></description>
			<content:encoded><![CDATA[<p>One area of displaying lists on web pages that I've generally disliked doing is pagination as it's a bit of a faff. Recently, I needed to do just this though as I couldn't delegate it as my colleague was too busy on other work. As a result, I thought that I should look into <a href="http://framework.zend.com/manual/en/zend.paginator.html"><tt>Zend_Paginator</tt></a> this time. Turns out that it's really easy to use and the documentation is great too.</p>
<p>The really useful thing about <tt>Zend_Paginator</tt> is that it uses adapters to collect its data. There are a variety of adapters, including array, dbSelect, dbTableSelect and iterator. The interesting ones for me being dbSelect and dbTableSelect as I use <tt>Zend_Db</tt> based data access layers. </p>
<p>This is how I used it with a Zend_Db based data mapper within TodoIt.</p>
<h3>Setting up the paginator</h3>
<p>My current method looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;Application_Model_TaskMapper
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">fetchOutstanding</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getDbAdapter</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">from</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_tableName</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'date_completed&nbsp;IS&nbsp;NULL'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">order</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'due_date&nbsp;ASC'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'id&nbsp;DESC'</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rows&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">$select</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$rows&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$task&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application_Model_Task</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$task</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;etc
</span>
</span></code></pre>
<p>This is pretty standard code for a data mapper. We select the data from the database and convert it to an array of entities. For the paginator to do its stuff though, we have to pass it the select object so that it can set the <tt>limit()</tt> on the select object.</p>
<p>The code therefore becomes:</p>
<pre class="phpcode"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">fetchOutstanding</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getDbAdapter</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">from</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_tableName</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'date_completed&nbsp;IS&nbsp;NULL'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">order</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'date_completed&nbsp;DESC'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'id&nbsp;DESC'</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$adapter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Paginator_Adapter_DbSelect</span><span style="color: #007700">(</span><span style="color: #0000BB">$select</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$paginator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Paginator</span><span style="color: #007700">(</span><span style="color: #0000BB">$adapter</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$paginator</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}</span>
</span></code></pre>
<p>As you can see, we create an instance of <tt>Zend_Paginator_Adapter_DbSelect</tt> which takes the <tt>$select</tt> object and the instantiate a <tt>Zend_Paginator</tt> and return it. The <tt>Zend_Paginator</tt> object implements Interator, so you can use it exactly like an array in a <tt>foreach</tt> loop and hence, in theory, your view script doesn't need to change.</p>
<p>However, the code that consumes <tt>TaskMapper</tt> expects an array of <tt>Task</tt> objects, not an array of arrays. To tell the paginator to create our objects, we extend <tt>Zend_Paginator_Adapter_DbSelect</tt> and override <tt>getItems()</tt> like this:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;Application_Model_Paginator_TaskAdapter&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Paginator_Adapter_DbSelect
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Returns&nbsp;an&nbsp;array&nbsp;of&nbsp;items&nbsp;for&nbsp;a&nbsp;page.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;integer&nbsp;$offset&nbsp;Page&nbsp;offset
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;integer&nbsp;$itemCountPerPage&nbsp;Number&nbsp;of&nbsp;items&nbsp;per&nbsp;page
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">getItems</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$itemCountPerPage</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rows&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">getItems</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$itemCountPerPage</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks&nbsp;</span><span style="color: #007700">=&nbsp;array();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$rows&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$task&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application_Model_Task</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$task</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>Here, we've used the entity-creation code that was in our original implementation of <tt>fetchOutstanding()</tt> and placed it in <tt>getItems()</tt>. </p>
<p>Obviously we have to update <tt>fetchOutstanding()</tt> to use our new adapter, so we replace</p>
<pre class="phpcode"><span style="color: #0000BB">$adapter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Paginator_Adapter_DbSelect</span><span style="color: #007700">(</span><span style="color: #0000BB">$select</span><span style="color: #007700">);</span>
</span></code></pre>
<p> with
<pre class="phpcode"><span style="color: #0000BB">$adapter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application_Model_Paginator_TaskAdapter</span><span style="color: #007700">(</span><span style="color: #0000BB">$select</span><span style="color: #007700">);</span>
</span></code></pre>
<p>Now, when we iterate over the pagination object, we get instances of <tt>Task</tt> and all is well with the world.</p>
<h3>Using the paginator</h3>
<p>Now that we have a paginator in place, we need to use it. Specifically we need to tell the paginator which page number we want to view and how many items are on a page. Within TodoIt, this is done in the ServiceLayer object and looks something like this:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;Application_Service_TaskService
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">fetchOutstanding</span><span style="color: #007700">(</span><span style="color: #0000BB">$page</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$numberPerPage&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">25</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$mapper&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application_Model_TaskMapper</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mapper</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchOutstanding</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCurrentPageNumber</span><span style="color: #007700">(</span><span style="color: #0000BB">$page</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setItemCountPerPage</span><span style="color: #007700">(</span><span style="color: #0000BB">$numberPerPage</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$tasks</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...</span>
</span></code></pre>
<p>Clearly the <tt>$page</tt> parameter comes via the URL at some point, so the controller looks something like this:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$page&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_getParam</span><span style="color: #007700">(</span><span style="color: #DD0000">'page'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$taskService&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application_Service_TaskService</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">outstandingTasks&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$taskService</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchOutstanding</span><span style="color: #007700">(</span><span style="color: #0000BB">$page</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$messenger&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_helper</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">flashMessenger</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">messages&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$messenger</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessages</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//...</span>
</span></code></pre>
<p>and then the view uses a <tt>foreach</tt> as you'd expect.</p>
<h3>Adding the paging controls</h3>
<p>Finally, to complete a paged list, we have to provide the user a mechanism to select the next and previous pages along with maybe jumping to a specific page. This is done using a separate view script that you pass to the paginator. In your view script, you put something like:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">paginationControl</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;&nbsp;</span><span style="color: #0000BB">outstandingTasks</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Sliding'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'pagination_control.phtml'</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span></code></pre>
<p>The first parameter is your paginator object. The second is the 'scrolling style' to use. There are four choices documented in the <a href="http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.rendering">manual</a>: <em>All</em>, <em>Elastic</em>, <em>Jumping</em> and <em>Sliding</em>. Personally, I have chosen to not display the page numbers themselves, so it doesn't matter which one  I pick. The last parameter is the partial view script that you want to be rendered. This allows you to have complete customisation of the HTML. </p>
<p>Here's what I'm using which is based heavily on and example in the documentation:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageCount</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&lt;div&nbsp;class="pagination-control"&gt;
&lt;!--&nbsp;Previous&nbsp;page&nbsp;link&nbsp;--&gt;
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">previous</span><span style="color: #007700">)):&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&nbsp;&nbsp;&lt;a&nbsp;href="<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">url</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'page'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">previous</span><span style="color: #007700">));&nbsp;</span><span style="color: #0000BB">?&gt;</span>"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;Previous
&nbsp;&nbsp;&lt;/a&gt;&nbsp;|
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">else:&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&nbsp;&nbsp;&lt;span&nbsp;class="disabled"&gt;&amp;lt;&nbsp;Previous&lt;/span&gt;&nbsp;|
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;
</span>
&lt;!--&nbsp;Next&nbsp;page&nbsp;link&nbsp;--&gt;
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">next</span><span style="color: #007700">)):&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&nbsp;&nbsp;&lt;a&nbsp;href="<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">url</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'page'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">next</span><span style="color: #007700">));&nbsp;</span><span style="color: #0000BB">?&gt;</span>"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;Next&nbsp;&amp;gt;
&nbsp;&nbsp;&lt;/a&gt;
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">else:&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&nbsp;&nbsp;&lt;span&nbsp;class="disabled"&gt;Next&nbsp;&amp;gt;&lt;/span&gt;
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&lt;span&nbsp;class="pagecount"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;Page&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;</span>&nbsp;of&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageCount</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&lt;/span&gt;
&lt;/div&gt;
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span></code></pre>
<p>And that's it; I now have paginated tasks in TodoIt and as you can see, <tt>Zend_Paginator</tt> is very easy to use and, more importantly, simple to customise to your own needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/exploring-zend-paginator/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>A Zend Framwork compound form element for dates</title>
		<link>http://akrabat.com/zend-framework/a-zend-framwork-compount-form-element-for-dates/</link>
		<comments>http://akrabat.com/zend-framework/a-zend-framwork-compount-form-element-for-dates/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 08:18:49 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1467</guid>
		<description><![CDATA[A while ago I needed to ask a user for their date of birth on a Zend_Form. The design showed three separate select elements to do this: A little bit of googling found this site http://codecaine.co.za/posts/compound-elements-with-zend-form which has not unfortunately disappeared, so the code in this article owes a lot of the author of that [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I needed to ask a user for their date of birth on a Zend_Form. The design showed three separate select elements to do this:</p>
<p><img src="http://akrabat.com/wp-content/uploads/2011-03-Screen-shot-2011-03-08-at-20.45.03.png" alt="Screen shot of a 3 select boxes for a date on a form" border="0" width="261" height="52" style="border: 1px solid black"/></p>
<p>A little bit of googling found this site <a href="http://codecaine.co.za/posts/compound-elements-with-zend-form">http://codecaine.co.za/posts/compound-elements-with-zend-form</a> which has not unfortunately disappeared, so the code in this article owes a lot of the author of that article.</p>
<p>It turns out to be remarkably simple to create a single Zend Form element that is rendered as multiple form elements. We create an element object and a view helper object and we're done. Usage then looks like:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Application_Form_Details&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">init</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addPrefixPath</span><span style="color: #007700">(</span><span style="color: #DD0000">'App_Form'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'App/Form/'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;other&nbsp;elements&nbsp;before&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'date'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'date_of_birth'</span><span style="color: #007700">,&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'label'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Date&nbsp;of&nbsp;birth:'
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;other&nbsp;elements&nbsp;after

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'submit'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Go'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>Obviously, this form lives in <tt>application/forms/Detail.php</tt> and is rendered as usual in a view script. In our form definition, we have added an element called 'date' and with the addition of the <tt>addPrefixPath</tt> call have told the form that in addition to using the standard Zend Framework form elements, also look in <tt>library/App/Form</tt>. (Incidentally, we can also now override any supplied form element by simply dropping a replacement into the <tt>libraryApp/Form</tt> folder.)</p>
<p>The <tt>date</tt> form element lives in library/App/Form/Element/Date.php as Zend_Form knows to look in a subfolder for <tt>App/Form</tt> called <tt>Elements</tt> for any element objects and will look in the <tt>Decorator/</tt> sub folder for decorator objects.</p>
<p>The Date element looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB"></span><span style="color: #FF8000">//&nbsp;Based&nbsp;on&nbsp;code&nbsp;from
//&nbsp;http://codecaine.co.za/posts/compound-elements-with-zend-form
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">App_Form_Element_Date&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Xhtml
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$helper&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'formDate'</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">isValid&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'year'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">'-'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'month'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">'-'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'day'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'--'</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_value</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_value</span><span style="color: #007700">[</span><span style="color: #DD0000">'year'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">'-'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_value</span><span style="color: #007700">[</span><span style="color: #DD0000">'month'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">'-'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_value</span><span style="color: #007700">[</span><span style="color: #DD0000">'day'</span><span style="color: #007700">];

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'--'</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">getValue</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;}

}</span>
</span></code></pre>
<p>There's quote a lot going on here, but it should be fairly clear. Firstly we specify the name of the view helper to use when rendering this element to be <tt>formDate</tt> which we will write. We know this element is going to consist of three select boxes and so these will end up being an array of posted data for day, month and year. As a result, we need to override <tt>isValid()</tt> to turn our array back into a string and then call up to the parent's <tt>isValid()</tt> in order to do the actual validation required. We also need to override getValue() in the same way to ensure that it is also a string. Again we call up to the parent's <tt>getValue()</tt> as that does filtering.</p>
<p>That's all there is to the element itself, so now we turn out attention to the view helper that will render the element. This lives in <tt>library/App/View/Helpers/FormDate.php</tt> and as per <a href="/viewhelpers">my view helpers post</a>, we need to tell the view about that folder via application.ini:</p>
<pre class="phpcode"><span style="color: #0000BB">autoloadernamespaces</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"App_"
</span><span style="color: #0000BB">resources</span><span style="color: #007700">.</span><span style="color: #0000BB">view</span><span style="color: #007700">.</span><span style="color: #0000BB">helperPath</span><span style="color: #007700">.</span><span style="color: #0000BB">App_View_Helper&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"App/View/Helper"
</span>
</span></code></pre>
<p>The <tt>formDate</tt> view helper code looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #FF8000">//&nbsp;based&nbsp;on&nbsp;code&nbsp;from
//&nbsp;http://codecaine.co.za/posts/compound-elements-with-zend-form

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">App_View_Helper_FormDate&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_FormElement
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">formDate&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$attribs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;separate&nbsp;value&nbsp;into&nbsp;day,&nbsp;month&nbsp;and&nbsp;year
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$day&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$month&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$year&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$day&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'day'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$month&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'month'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$year&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'year'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list(</span><span style="color: #0000BB">$year</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">'-'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;build&nbsp;select&nbsp;options
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dayAttribs&nbsp;</span><span style="color: #007700">=&nbsp;isset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'dayAttribs'</span><span style="color: #007700">])&nbsp;?&nbsp;</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'dayAttribs'</span><span style="color: #007700">]&nbsp;:&nbsp;array();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$monthAttribs&nbsp;</span><span style="color: #007700">=&nbsp;isset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'monthAttribs'</span><span style="color: #007700">])&nbsp;?&nbsp;</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'monthAttribs'</span><span style="color: #007700">]&nbsp;:&nbsp;array();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearAttribs&nbsp;</span><span style="color: #007700">=&nbsp;isset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'yearAttribs'</span><span style="color: #007700">])&nbsp;?&nbsp;</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'yearAttribs'</span><span style="color: #007700">]&nbsp;:&nbsp;array();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dayMultiOptions&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">32</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$index&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STR_PAD_LEFT</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dayMultiOptions</span><span style="color: #007700">[</span><span style="color: #0000BB">$index</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STR_PAD_LEFT</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$monthMultiOptions&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$index&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STR_PAD_LEFT</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$monthMultiOptions</span><span style="color: #007700">[</span><span style="color: #0000BB">$index</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'F'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">01</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$startYear&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'startYear'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$startYear&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'startYear'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'startYear'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stopYear&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$startYear&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'stopYear'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stopYear&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'stopYear'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$attribs</span><span style="color: #007700">[</span><span style="color: #DD0000">'stopYear'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearMultiOptions&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$stopYear&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$startYear</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$startYear</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&gt;=&nbsp;</span><span style="color: #0000BB">$stopYear</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">--)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearMultiOptions</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$startYear</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">$stopYear</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearMultiOptions</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;return&nbsp;the&nbsp;3&nbsp;selects&nbsp;separated&nbsp;by&nbsp;&amp;nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">formSelect</span><span style="color: #007700">(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'[day]'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dayAttribs</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dayMultiOptions</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&amp;nbsp;'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">formSelect</span><span style="color: #007700">(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'[month]'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$monthAttribs</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$monthMultiOptions</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&amp;nbsp;'&nbsp;</span><span style="color: #007700">.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">formSelect</span><span style="color: #007700">(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'[year]'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearAttribs</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$yearMultiOptions
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>Again, there's a fair amount of code, but I expect that it's pretty self-explanatory. Essentially, we have a lot of set up in order to render three select boxes; one for the day, month and year. I decided to use month names but it's easy enough to change to numbers. In terms of configuration, you need to be able to specify the start and stop year numbers. These are then passed in as options when calling <tt>addElement</tt>. </p>
<p>That's about it. Two separate files is all you need to create a Zend_Form element object that is implemented via compound elements. It also follows that you can do the same for any other conceptual element that you want to be rendered as multiple elements in the page.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/a-zend-framwork-compount-form-element-for-dates/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zend Framework View Helpers</title>
		<link>http://akrabat.com/zend-framework/zend-framework-view-helpers/</link>
		<comments>http://akrabat.com/zend-framework/zend-framework-view-helpers/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 10:43:05 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1464</guid>
		<description><![CDATA[I can't seem to find an article here that consolidates my thoughts on Zend Framework's view helper system, so I thought I'd better correct that. Zend Framework's Zend_View component supports helper methods known as view helpers. They are used like this in a view script: &#60;?php&#160;echo&#160;$this-&#62;myHelper('myParam1');&#160;?&#62; Behind the scenes, this is implemented as a method [...]]]></description>
			<content:encoded><![CDATA[<p>I can't seem to find an article here that consolidates my thoughts on Zend Framework's view helper system, so I thought I'd better correct that. Zend Framework's <tt>Zend_View</tt> component supports helper methods known as <em>view helpers</em>. They are used like this in a view script:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">myHelper</span><span style="color: #007700">(</span><span style="color: #DD0000">'myParam1'</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span></code></pre>
<p>Behind the scenes, this is implemented as a method within a class something like this:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_MyHelper&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">myHelper</span><span style="color: #007700">(</span><span style="color: #0000BB">$myParam1</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;some&nbsp;logic&nbsp;that&nbsp;fills&nbsp;in&nbsp;$html.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$html</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>Note that by convention, view helpers return the string and then the view script <tt>echo</tt>s it and the <tt>Zend_View_Helper_</tt> section of the class name is known as the <em>prefix</em>. </p>
<p>A typical Zend Framework project using <tt>Zend_Application</tt>, such as that generated using the zf command line tool, will have a folder called <em>helpers</em> within the <em>views</em> folder for each module. There will also be a <em>helpers</em> folder within the <em>layouts</em> folder too. If you place your view helper in one of these <em>helpers</em> folders, then the prefix is <tt>Zend_View_Helper_</tt>.</p>
<p>We can also tell the view object about our own view helper folder. Typically this will live in the <em>library/App/View/Helper/</em> folder and so, you probably want a prefix of <tt>App_View_Helper_</tt>. To do this, we simply add this line to <tt>application.ini</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">resources</span><span style="color: #007700">.</span><span style="color: #0000BB">view</span><span style="color: #007700">.</span><span style="color: #0000BB">helperPath</span><span style="color: #007700">.</span><span style="color: #0000BB">App_View_Helper_&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"App/View/Helper/"</span>
</span></code></pre>
<p>The view helper now lives in <tt>App/View/Helper/MyHelper.php</tt> and looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">App_View_Helper_MyHelper&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">myHelper</span><span style="color: #007700">(</span><span style="color: #0000BB">$myParam1</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;some&nbsp;logic&nbsp;that&nbsp;fills&nbsp;in&nbsp;$html.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$html</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>Due to the way Zend Framework's plugin loader works, our view script doesn't change at all as the view object's plugin loader will look across all registered paths to find the view helper specified in the view script.</p>
<p>I tend to prefer using the <tt>App</tt> folder over the <tt>helpers</tt> folder within <tt>layouts</tt> as I like to have one place to go to for my "site-wide" classes and so my <tt>App</tt> folder contains front controller plugins, validators, filters, form elements as well.</p>
<p>As you have extended <tt>Zend_View_Helper_Abstract</tt> for creating your own helper, you have access to the view object itself too. This is helpful for accessing other view helpers, such as escape. My personal preference is for my view helpers to manage their own escaping as requires as that way I can include html tags within my helper.</p>
<p>For example, suppose we want to create a table from an array that looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array();
</span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'Name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Email'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'Alison'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'alison@example.com'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'Bert'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bert@example.com'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'Charlie'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'charlie@example.com'</span><span style="color: #007700">);</span>
</span></code></pre>
<p>Our view script may look like this:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tabulate</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'class'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'tabulated'</span><span style="color: #007700">));&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span></code></pre>
<p>(assuming our designer wants a class name attached to the table for styling.)</p>
<p>The view helper itself would then look something like:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">App_View_Helper_Tabulate&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">tabulate&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$attribs&nbsp;</span><span style="color: #007700">=&nbsp;array())
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$attribString&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$attribs&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$attribString&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'="'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'"'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$header&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_shift</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;table&nbsp;</span><span style="color: #0000BB">$attribString</span><span style="color: #DD0000">&gt;\n&lt;tr&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$header&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$cell</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$escapedCell&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">escape</span><span style="color: #007700">(</span><span style="color: #0000BB">$cell</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"&lt;th&gt;</span><span style="color: #0000BB">$escapedCell</span><span style="color: #DD0000">&lt;/th&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"&lt;tr&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$cell</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$escapedCell&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">escape</span><span style="color: #007700">(</span><span style="color: #0000BB">$cell</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"&lt;td&gt;</span><span style="color: #0000BB">$escapedCell</span><span style="color: #DD0000">&lt;/td&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$html&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&lt;/table&gt;'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$html</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>As you can see, we cannot escape the output of the view helper, so the view helper has to do its own escaping via the <tt>view</tt> property.</p>
<p>If you end up using the same set of view helpers on each project you do, then consider putting then into a vendor library. This is the same concept as using the <tt>App</tt> folder, but you use a different name (I use Akrabat) and store them in a separate vcs repository. This allows you easy reuse. You add a new line to <tt>application.ini</tt> to pick them up:</p>
<pre class="phpcode"><span style="color: #0000BB">resources</span><span style="color: #007700">.</span><span style="color: #0000BB">view</span><span style="color: #007700">.</span><span style="color: #0000BB">helperPath</span><span style="color: #007700">.</span><span style="color: #0000BB">Akrabat_View_Helper_&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Akrabat/View/Helper/"</span>
</span></code></pre>
<p>and you're done. Note that the <em>order of the helperPath in application.ini is important</em>. You want the vendor path before the App path so that you can override your vendor ones specifically within a project if you need to.</p>
<pre class="phpcode"><span style="color: #0000BB">resources</span><span style="color: #007700">.</span><span style="color: #0000BB">view</span><span style="color: #007700">.</span><span style="color: #0000BB">helperPath</span><span style="color: #007700">.</span><span style="color: #0000BB">Akrabat_View_Helper_&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Akrabat/View/Helper/"
</span><span style="color: #0000BB">resources</span><span style="color: #007700">.</span><span style="color: #0000BB">view</span><span style="color: #007700">.</span><span style="color: #0000BB">helperPath</span><span style="color: #007700">.</span><span style="color: #0000BB">App_View_Helper_&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"App/View/Helper/"</span>
</span></code></pre>
<p>Plugin helper path order is one of the more frustrating things about the plugin loaders used within Zend Framework and affects form elements, validators, filters and view helpers to name but a few. So if you discover that a view helper isn't being called when you think it ought to be, then check that it's not being overridden further down the chain. You can of course use this to your advantage and override a default Zend Framework view helper by simply creating an <tt>App</tt> version with the same name.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/zend-framework-view-helpers/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using your own View object with Zend_Application</title>
		<link>http://akrabat.com/zend-framework/using-your-own-view-object-with-zend_application/</link>
		<comments>http://akrabat.com/zend-framework/using-your-own-view-object-with-zend_application/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 09:41:20 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1458</guid>
		<description><![CDATA[Let's say that you want to use your own view object within your Zend Framework application. Creating the view object is easy enough in library/App/View.php: class&#160;App_View&#160;extends&#160;Zend_View { &#160;&#160;&#160;&#160;//&#160;custom&#160;methods&#160;here } along with adding the App_ namespace to the the autoloader in application.ini: autoloadernamespaces[]&#160;=&#160;"App_" All we need to now is get Zend_Application to bootstrap with our new [...]]]></description>
			<content:encoded><![CDATA[<p>Let's say that you want to use your own view object within your Zend Framework application.</p>
<p>Creating the view object is easy enough in <tt>library/App/View.php</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;App_View&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_View
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;custom&nbsp;methods&nbsp;here
</span><span style="color: #007700">}</span>
</span></code></pre>
<p>along with adding the <tt>App_</tt> namespace to the the autoloader in <tt>application.ini</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">autoloadernamespaces</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"App_"</span>
</span></code></pre>
<p>All we need to now is get Zend_Application to bootstrap with our new view class. There are two ways of doing this: within <tt>Bootstrap.php</tt> or using a custom resource.</p>
<h3>_initView() in Bootstrap.php</h3>
<p>At first blush, the code looks quite easy. In <tt>application/Bootstrap.php</tt>, we add our own method that creates the view object and assigns it to the viewRenderer:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Bootstrap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Application_Bootstrap_Bootstrap
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;</span><span style="color: #0000BB">_initView</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">App_View</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$viewRenderer&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_ViewRenderer</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$viewRenderer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setView</span><span style="color: #007700">(</span><span style="color: #0000BB">$view</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_HelperBroker</span><span style="color: #007700">::</span><span style="color: #0000BB">addHelper</span><span style="color: #007700">(</span><span style="color: #0000BB">$viewRenderer</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>As we have named the method <tt>_initView()</tt>, our method will take precedence over the built in View resource and be used instead. However, this implementation will ignore any view options that are configured in <tt>application.ini</tt> using the <tt>resources.view</tt> key, so a better method is this:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;Bootstrap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Application_Bootstrap_Bootstrap
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;</span><span style="color: #0000BB">_initView</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$resources&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getOption</span><span style="color: #007700">(</span><span style="color: #DD0000">'resources'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$resources</span><span style="color: #007700">[</span><span style="color: #DD0000">'view'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$resources</span><span style="color: #007700">[</span><span style="color: #DD0000">'view'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">App_View</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'doctype'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">doctype</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setDoctype</span><span style="color: #007700">(</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'doctype'</span><span style="color: #007700">]));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'charset'</span><span style="color: #007700">])&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">doctype</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">isHtml5</span><span style="color: #007700">())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headMeta</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setCharset</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'charset'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'contentType'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headMeta</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">appendHttpEquiv</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'contentType'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$viewRenderer&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_ViewRenderer</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$viewRenderer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setView</span><span style="color: #007700">(</span><span style="color: #0000BB">$view</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_HelperBroker</span><span style="color: #007700">::</span><span style="color: #0000BB">addHelper</span><span style="color: #007700">(</span><span style="color: #0000BB">$viewRenderer</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

}</span>
</span></code></pre>
<p>This version takes into account your configuration settings and behaves the same as the View resource provided by Zend Framework. The only difference is that we're now using <tt>App_View</tt>.</p>
<h3>Custom resource</h3>
<p>Another option is to override <tt>Zend_Application_Resource_View</tt> with our own view resource. In this case, we create a class called <tt>App_Resource_View</tt> stored in <tt>library/App/Resource/View.php</tt>. We only need to override one method, <tt>getView()</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">class&nbsp;App_Resource_View&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Application_Resource_View
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getView</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getOptions</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">App_View</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'doctype'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">doctype</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setDoctype</span><span style="color: #007700">(</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'doctype'</span><span style="color: #007700">]));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'charset'</span><span style="color: #007700">])&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">doctype</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">isHtml5</span><span style="color: #007700">())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headMeta</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setCharset</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'charset'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'contentType'</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headMeta</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">appendHttpEquiv</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'contentType'</span><span style="color: #007700">]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_view</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</span>
</span></code></pre>
<p>Essentially, all I have done is replace the class of the view object to be App_View and left everything else alone so that it behaves the same as the default View resource.</p>
<p>To get Zend_Application to load our custom resource, we just add one line to <tt>application.ini</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">pluginPaths</span><span style="color: #007700">.</span><span style="color: #0000BB">App_Resource&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"App/Resource"</span>
</span></code></pre>
<p>We now have a reusable resource that will load our own View class and can easily take it from project to project.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/using-your-own-view-object-with-zend_application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Remove index.php from your URL</title>
		<link>http://akrabat.com/zend-framework/remove-index-php-from-your-url/</link>
		<comments>http://akrabat.com/zend-framework/remove-index-php-from-your-url/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:51:08 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1414</guid>
		<description><![CDATA[One thing you may have noticed with Zend Framework projects that use the baseUrl() view helper to reference CSS and other static files is that it doesn't work if the URL contains contains index.php. Let's explain, by using code from the tutorial. In the layout.phtml file, we link to a CSS file like this: &#60;?php&#160;echo&#160;$this-&#62;headLink()-&#62;prependStylesheet($this-&#62;baseUrl().'/css/site.css');&#160;?&#62;&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>One thing you may have noticed with Zend Framework projects that use the baseUrl() view helper to reference CSS and other static files is that it doesn't work if the URL contains contains index.php.</p>
<p>Let's explain, by using code from the <a href="/zend-framework-tutorial">tutorial</a>.</p>
<p>In the <tt>layout.phtml</tt> file, we link to a CSS file like this:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headLink</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">prependStylesheet</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">baseUrl</span><span style="color: #007700">().</span><span style="color: #DD0000">'/css/site.css'</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">?&gt;</span>&nbsp;
</span></code></pre>
<p>This code uses a <tt>baseUrl()</tt> view helper that looks like this</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php&nbsp;
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Zend_View_Helper_BaseUrl&nbsp;
</span><span style="color: #007700">{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">baseUrl</span><span style="color: #007700">()&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Zend_Controller_Front</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$fc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getBaseUrl</span><span style="color: #007700">();&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
}
</span>
</span></code></pre>
<p>When you go to http://localhost/zf-tutorial/ you get an page that looks like this:</p>
<p><img src="http://akrabat.com/wp-content/uploads/zf-tutorial_16-300x207.png" alt="Screen short of Zend Framework tutorial" title="Listing albums in Zend Framework tutorial" width="300" height="207" /></p>
<p>However, if you go to http://localhost/zf-tutorial/index.php, you get this:</p>
<p><img src="http://akrabat.com/wp-content/uploads/2009-07-zf-tutorial_no_css.jpg" alt="zf-tutorial_no_css.jpg" border="0" width="300" height="203" /></p>
<p>This happens because the baseURL used in the <tt>href</tt> of the CSS link in the second case is <tt>/zf-tutorial/public/index.php/css/site.css</tt> rather than the correct <tt>/zf-tutorial/public/css/site.css</tt>.</p>
<h3>Solution using mod_rewrite</h3>
<p>One way to fix this is to use a mod_rewrite rule in your <tt>.htaccess</tt> file:</p>
<pre class="phpcode"><span style="color: #0000BB">
RewriteCond&nbsp;</span><span style="color: #007700">%{</span><span style="color: #0000BB">THE_REQUEST</span><span style="color: #007700">}&nbsp;^[</span><span style="color: #0000BB">A</span><span style="color: #007700">-</span><span style="color: #0000BB">Z</span><span style="color: #007700">]{</span><span style="color: #0000BB">3</span><span style="color: #007700">,</span><span style="color: #0000BB">9</span><span style="color: #007700">}&nbsp;/([^/]+/)*</span><span style="color: #0000BB">index</span><span style="color: #007700">.</span><span style="color: #0000BB">php&nbsp;
RewriteRule&nbsp;</span><span style="color: #007700">^</span><span style="color: #0000BB">index</span><span style="color: #007700">.</span><span style="color: #0000BB">php</span><span style="color: #007700">(.*)$&nbsp;/</span><span style="color: #0000BB">zf</span><span style="color: #007700">-</span><span style="color: #0000BB">tutorial</span><span style="color: #007700">/public$</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">[</span><span style="color: #0000BB">R</span><span style="color: #007700">=</span><span style="color: #0000BB">301</span><span style="color: #007700">,</span><span style="color: #0000BB">L</span><span style="color: #007700">]
</span>
</span></code></pre>
<p>I did have to put the full subdirectory into the rewrite rule to get it to work.</p>
<h3>The quick and dirty solution</h3>
<p>At the top of your index.php file take out the <tt>index.php</tt> from the <tt>REQUEST_URI</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">
$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_URI"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.php'</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_URI"</span><span style="color: #007700">]);
</span>
</span></code></pre>
<p>This obviously works, but feels a bit "hacky"!  There's probably other ways to solve it too, but once I found the rewrite rule, I stuck with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/remove-index-php-from-your-url/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Handling exceptions in a Front Controller plugin</title>
		<link>http://akrabat.com/zend-framework/handling-exceptions-in-a-front-controller-plugin/</link>
		<comments>http://akrabat.com/zend-framework/handling-exceptions-in-a-front-controller-plugin/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 08:02:57 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1372</guid>
		<description><![CDATA[If you have a Zend Framework Front Controller plugin which throws an exception, then the action is still executed and then the error action is then called, so that the displayed output shows two actions rendered, with two layouts also rendered. This is almost certainly not what you want or what you expected. This is [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a Zend Framework Front Controller plugin which throws an exception, then the action is still executed and then the error action is then called, so that the displayed output shows two actions rendered, with two layouts also rendered. This is almost certainly not what you want or what you expected.</p>
<p>This is how to handle errors in a Front Controller plugin:</p>
<ol>
<li>Prefer <tt>preDispatch()</tt> over <tt>dispatchLoopStartup()</tt> as it is called from within the dispatch loop</li>
<li>Catch the exception and the modify the request so that the error controller's error action is dispatched.</li>
<li>Create an error handler object so that the error action works as expected.</li>
</ol>
<p>This is the code:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Application_Plugin_Foo&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Plugin_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">preDispatch</span><span style="color: #007700">(</span><span style="color: #0000BB">Zend_Controller_Request_Abstract&nbsp;$request</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;do&nbsp;something&nbsp;that&nbsp;throws&nbsp;an&nbsp;exception

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Repoint&nbsp;the&nbsp;request&nbsp;to&nbsp;the&nbsp;default&nbsp;error&nbsp;handler
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setModuleName</span><span style="color: #007700">(</span><span style="color: #DD0000">'default'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setControllerName</span><span style="color: #007700">(</span><span style="color: #DD0000">'error'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setActionName</span><span style="color: #007700">(</span><span style="color: #DD0000">'error'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Set&nbsp;up&nbsp;the&nbsp;error&nbsp;handler
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Controller_Plugin_ErrorHandler</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">type&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Zend_Controller_Plugin_ErrorHandler</span><span style="color: #007700">::</span><span style="color: #0000BB">EXCEPTION_OTHER</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">request&nbsp;</span><span style="color: #007700">=&nbsp;clone(</span><span style="color: #0000BB">$request</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exception&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setParam</span><span style="color: #007700">(</span><span style="color: #DD0000">'error_handler'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

}</span>
</span></code></pre>
<p>That's it.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/handling-exceptions-in-a-front-controller-plugin/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

