<?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, 11 May 2012 12:52:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Vagrant in Zend Framework 1</title>
		<link>http://akrabat.com/zend-framework/vagrant-in-zf1-trunk/</link>
		<comments>http://akrabat.com/zend-framework/vagrant-in-zf1-trunk/#comments</comments>
		<pubDate>Wed, 09 May 2012 07:12:34 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2324</guid>
		<description><![CDATA[I recently added support for vagrant to the Zend Framework codebase to enable easier testing. I was motivated by some work the joind.in folks have done to get a working development environment for joind.in development using Vagrant. Vagrant is a fantastic tool that enables you to manage and run virtual machines from the command line, [...]]]></description>
			<content:encoded><![CDATA[<p>I recently added support for <a href="http://vagrantup.com/">vagrant</a> to the Zend Framework codebase to enable easier testing. I was motivated by some work the <a href="https://joind.in">joind.in</a> folks have done to get a working development environment for joind.in development using Vagrant.</p>
<p>Vagrant is a fantastic tool that enables you to manage and run virtual machines from the command line, including automatic provisioning of them using <a href="http://puppetlabs.com/">puppet</a> or <a href="http://www.opscode.com/chef/">chef</a>.  The really cool thing about it however from my point of view is that vagrant automatically sets up the VM with a folder called /vagrant that holds the code on your local hard drive from where you started the VM. This means that you can continue to edit your code in your local editor/IDE and test it within the VM easily.</p>
<p>I highly recommend checking it out.</p>
<h3>ZF1's Vagrant set up</h3>
<p>The Vagrant set up for ZF1 is designed for testing ZF1 against multiple PHP versions. As such it sets up a simple Ubuntu VM with the required toolchain for compiling PHP and provides a script called <tt>php-build.sh</tt> which will download and build any PHP 5.2, 5.3 or 5.4 version that you are interested in. I based this script on information in Derick Rethans' excellent <a href="http://derickrethans.nl/multiple-php-version-setup.html">Multiple PHP versions set-up</a> article. </p>
<p>One thing that I discovered was that by default, the VM cannot create symlinks in <tt>/vagrant</tt>. The way to solve this is to add the following to the <tt>Vagrantfile</tt>:</p>
<pre>
  config.vm.customize [
    "setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"
  ]
</pre>
<p>I chose to use puppet to install the required packages and created a simple <a href="http://framework.zend.com/svn/framework/standard/trunk/puppet/manifests/default.pp">default.pp</a> configuration. I'm sure this isn't optimal, but it works :) </p>
<h3>Running ZF1 unit tests</h3>
<p>The process to set up ZF1 to run unit tests in a VM using PHPUnit 3.4, follow these steps:</p>
<p><em>1. Install requirements for running the VM:</em></p>
<ul>
<li>VirtualBox (<a href="https://www.virtualbox.org/">https://www.virtualbox.org/</a>)</li>
<li>Ruby (<a href="http://www.ruby-lang.org/">http://www.ruby-lang.org/</a>)</li>
<li>Vagrant (<a href="http://vagrantup.com/">http://vagrantup.com/</a>)</li>
</ul>
<p><em>2. Checkout the ZF1 repository:</em></p>
<pre>
    $ svn checkout http://framework.zend.com/svn/framework/standard/trunk zf1-dev
    $ cd zf1-dev
</pre>
<p><em>3. Start the process by running Vagrant.</em></p>
<pre>
    $ vagrant up
</pre>
<p>This will take a long while as it has to download a VM image and then provision it. Once it has finished, it will exit and leave you back at the command prompt. </p>
<p><em>4. SSH into the VM</em></p>
<pre>
    $ vagrant ssh
</pre>
<p>Vagrant sets up key-less ssh connections to the VM that "just works" :)</p>
<p><em>5. Build a version of PHP.</em></p>
<pre>
    $ php-build.sh 5.3.11
</pre>
<p>This also takes a while as it compiles PHP for you! It also installs PHPUnit 3.4 as that's the version we need to unit test ZF1. </p>
<p>Each version of PHP that you compile is stored in /usr/local/php/{version number}. You can compile any version; I have 5.2.4, 5.2.12, 5.3.3 and 5.3.11 installed at the moment…</p>
<p><em>6. Select PHP to use:</em></p>
<pre>
   $ pe 5.3.11
</pre>
<p><tt>pe</tt> is a handy shell function that Derick wrote that changes your PHP environment to whichever version your specify.</p>
<p><em>7. Run tests</em></p>
<pre>
   $ cd /vagrant/tests
   $ php runtests.php
</pre>
<p>Alternatively, you can run each component's tests individually:</p>
<pre>
   $ phpunit --stderr -d memory_limit=-1 Zend/Acl/AclTest.php
   $ phpunit --stderr -d memory_limit=-1 Zend/Amf/AllTests.php
   (etc...)
</pre>
<p>Obviously, you repeat steps 5 through 7 for each version of PHP you want to test on.</p>
<p>To stop your Vagrant VM, exit the SSH shell and type `vagrant halt` or `vagrant suspend`. For more details on controlling your Vagrant VM, I recommend <a href="http://www.lornajane.net/posts/2012/using-an-existing-vagrant-setup-for-php-development">Lorna's article</a></p>
<p>Running unit tests on Zend Framework 1 is now considerably easier!</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=2324&amp;md5=0987707112a96caf9810d1494847d728" 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/vagrant-in-zf1-trunk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fvagrant-in-zf1-trunk%2F&amp;language=en_GB&amp;category=text&amp;title=Vagrant+in+Zend+Framework+1&amp;description=I+recently+added+support+for+vagrant+to+the+Zend+Framework+codebase+to+enable+easier+testing.+I+was+motivated+by+some+work+the+joind.in+folks+have+done+to+get+a+working...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Unit testing Zend Framework 1</title>
		<link>http://akrabat.com/zend-framework/unit-testing-zend-framework-1/</link>
		<comments>http://akrabat.com/zend-framework/unit-testing-zend-framework-1/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 10:33:50 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2320</guid>
		<description><![CDATA[As part of our release process for Zend Framework 1.12, I've been working through the unit tests and running them on PHP 5.2.4 as it seems that recent changes weren't being tested with that version. This isn't totally surprising as Open Source contributors are, almost by definition, interested in new things and so are much [...]]]></description>
			<content:encoded><![CDATA[<p>As part of our release process for Zend Framework 1.12, I've been working through the unit tests and running them on PHP 5.2.4 as it seems that recent changes weren't being tested with that version. This isn't totally surprising as Open Source contributors are, almost by definition, interested in new things and so are much more likely to be running PHP 5.4 rather than 5.2! This is, of course, a compelling reason for using continuous integration and I'm quite excited with <a href="http://travis-ci.org/">Travis-CI</a> and we are using it with ZF2.</p>
<h3>Installing PHPUnit 3.4</h3>
<p>The first challenge that I encountered was that ZF1's unit test are not compatible with PHPUnit 3.6. As there are over 14,000 ZF1 unit tests which have been written since 2006, there hasn't been much enthusiasm for rewriting them to be PHPUnit 3.6 compatible. (However, if someone wants to volunteer, please contact me!)</p>
<p>As I have PHPUnit 3.6 installed for testing other projects, I needed to install PHPUnit 3.4 side-by-side with version 3.6 This turns out to be relatively easy and has been documented by Christer Edvartsen in his article <a href="http://tech.vg.no/2011/11/29/running-multiple-versions-of-phpunit/">Running Multiple Versions of PHPUnit</a>.</p>
<p>I ran into one problem with his instructions though and needed this code to be added to the top of <tt>phpunit</tt>:</p>
<pre class="phpcode"><span style="color: #0000BB">set_include_path</span><span style="color: #007700">(</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #0000BB">PATH_SEPARATOR</span><span style="color: #007700">,&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">__DIR__&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'/../share/php'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'/usr/share/php'</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">get_include_path</span><span style="color: #007700">()
)));</span>
</span></code></pre>
<p>Having done this though, PHPUnit 3.4 works correctly and we can run ZF1 unit tests.</p>
<h3>Running the ZF1 unit tests</h3>
<p>To run ZF1's unit tests, you first need to check out the code from the <a href="http://framework.zend.com/svn/framework/standard/trunk/">Subversion repository</a>.  </p>
<pre>svn co http://framework.zend.com/svn/framework/standard/trunk/</pre>
<p>Due to the number of tests and the memory that they take up, you should run tests for each component individually.</p>
<p>The command to use is:</p>
<pre>phpunit34 --stderr -d memory_limit=-1 Zend/{Component}/AllTests.php</pre>
<p>Note:</p>
<ul>
<li><tt>--stderr</tt> pipes PHPUnit's output to stderr which means that any tests that rely on <tt>header()</tt> will work. (i,e. don't test <tt>Zend_Session</tt> without it!)</li>
<li><tt>-d memory_limit=-1</tt> will turn off PHP's memory_limit setting. The ZF1 unit tests use a lot of memory, so this is easiest.</li>
<li>The tests <em>rely</em> on being set up correctly. This is done using <tt>AllTests.php</tt> so don't forget this. Tests will fail if you forget!</li>
</ul>
<p>All that needs to happen now is that any failing tests are fixed!</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=2320&amp;md5=612fa1861d4dbd0aa3f4c2bfcb2a9ba0" 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/unit-testing-zend-framework-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Funit-testing-zend-framework-1%2F&amp;language=en_GB&amp;category=text&amp;title=Unit+testing+Zend+Framework+1&amp;description=As+part+of+our+release+process+for+Zend+Framework+1.12%2C+I%27ve+been+working+through+the+unit+tests+and+running+them+on+PHP+5.2.4+as+it+seems+that+recent+changes+weren%27t...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>One-to-many joins with Zend_Db_Table_Select</title>
		<link>http://akrabat.com/zend-framework/one-to-many-joins-with-zend_db_table_select/</link>
		<comments>http://akrabat.com/zend-framework/one-to-many-joins-with-zend_db_table_select/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 08:36:46 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1715</guid>
		<description><![CDATA[Let's say that you want to set up a one-to-many relationship between two tables: Artists and Albums because you've refactored my ZF1 tutorial. Let's assume that an artist has many albums. These are the basic table definitions: artists table: id, artist albums table: id, artist_id, title When you list the albums, you obviously want to [...]]]></description>
			<content:encoded><![CDATA[<p>Let's say that you want to set up a one-to-many relationship between two tables: Artists and Albums because you've refactored my <a href="http://akrabat.com/zend-framework-tutorial">ZF1 tutorial</a>. </p>
<p>Let's assume that an <em>artist</em> has many <em>albums</em>. These are the basic table definitions:</p>
<p><tt>artists</tt> table: id, artist<br />
<tt>albums</tt> table: id, artist_id, title</p>
<p>When you list the albums, you obviously want to see the artist name rather than the id, so clearly you use a join!</p>
<p>Assuming you're using <tt>Zend_Db_Table</tt>, the easiest way is to turn off the integrity check and do a join in a mapper or table method.</p>
<p>Something like this:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">AlbumTable&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Db_Table_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'album'</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">fetchAllWithArtistName</span><span style="color: #007700">(</span><span style="color: #0000BB">$order&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'title&nbsp;ASC'</span><span style="color: #007700">))
&nbsp;&nbsp;&nbsp;&nbsp;{
&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">$this</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">setIntegrityCheck</span><span style="color: #007700">(</span><span style="color: #0000BB">false</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">_name</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">$select</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">joinLeft</span><span style="color: #007700">(</span><span style="color: #DD0000">'artist'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'album.artist_id&nbsp;=&nbsp;artist.id'</span><span style="color: #007700">,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'artist_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'name'</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">(</span><span style="color: #0000BB">$order</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">$rows&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</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;return&nbsp;</span><span style="color: #0000BB">$rows</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
}
</span>
</span></code></pre>
<p>The row set returned will have all the columns from the <tt>albums</tt> table and one additional column called <tt>artist_name</tt> which is an alias of the <tt>name</tt> column from the <tt>artists</tt> table.</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1715&amp;md5=4181415e8d23141cb558bfff3f64bb16" 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/one-to-many-joins-with-zend_db_table_select/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fone-to-many-joins-with-zend_db_table_select%2F&amp;language=en_GB&amp;category=text&amp;title=One-to-many+joins+with+Zend_Db_Table_Select&amp;description=Let%27s+say+that+you+want+to+set+up+a+one-to-many+relationship+between+two+tables%3A+Artists+and+Albums+because+you%27ve+refactored+my+ZF1+tutorial.+Let%27s+assume+that+an+artist+has+many...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1565&amp;md5=29729db7a154abd1fcffa9242d7094d6" 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_config_ini-and-a-string/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fzend_config_ini-and-a-string%2F&amp;language=en_GB&amp;category=text&amp;title=Zend_Config_Ini+and+a+string&amp;description=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...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1508&amp;md5=de017c79a461419235abf7696d29e043" 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/exploring-zend-paginator/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fexploring-zend-paginator%2F&amp;language=en_GB&amp;category=text&amp;title=Exploring+Zend_Paginator&amp;description=One+area+of+displaying+lists+on+web+pages+that+I%27ve+generally+disliked+doing+is+pagination+as+it%27s+a+bit+of+a+faff.+Recently%2C+I+needed+to+do+just+this+though...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1467&amp;md5=63ecbdecd98846359d5a0a0fa66b5746" 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/a-zend-framwork-compount-form-element-for-dates/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fa-zend-framwork-compount-form-element-for-dates%2F&amp;language=en_GB&amp;category=text&amp;title=A+Zend+Framwork+compound+form+element+for+dates&amp;description=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%3A+A+little+bit...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1464&amp;md5=96af2d15aa191d623346e23f16e7c89e" 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-view-helpers/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fzend-framework-view-helpers%2F&amp;language=en_GB&amp;category=text&amp;title=Zend+Framework+View+Helpers&amp;description=I+can%27t+seem+to+find+an+article+here+that+consolidates+my+thoughts+on+Zend+Framework%27s+view+helper+system%2C+so+I+thought+I%27d+better+correct+that.+Zend+Framework%27s+Zend_View+component+supports...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1458&amp;md5=f4ca8829e433f8e06c6457649873c6f8" 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/using-your-own-view-object-with-zend_application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fusing-your-own-view-object-with-zend_application%2F&amp;language=en_GB&amp;category=text&amp;title=Using+your+own+View+object+with+Zend_Application&amp;description=Let%27s+say+that+you+want+to+use+your+own+view+object+within+your+Zend+Framework+application.+Creating+the+view+object+is+easy+enough+in+library%2FApp%2FView.php%3A+class%26nbsp%3BApp_View%26nbsp%3Bextends%26nbsp%3BZend_View+%7B+%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2F%2F%26nbsp%3Bcustom%26nbsp%3Bmethods%26nbsp%3Bhere+%7D+along...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Translations of my Zend Framework Tutorial for ZF 1.10 &amp; 1.11</title>
		<link>http://akrabat.com/zend-framework/translations-of-my-zend-framework-tutorial-for-zf-1-10-1-11/</link>
		<comments>http://akrabat.com/zend-framework/translations-of-my-zend-framework-tutorial-for-zf-1-10-1-11/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 07:56:21 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=1421</guid>
		<description><![CDATA[Recently, a couple of people have very generously donated their time to translate my Zend Framework Tutorial into their native language to help their fellow countrymen. Firstly, Mario Santagiuliana has provided an Italian translation: Introduzione allo Zend Framework. Secondly, Radosław Benkel translated into Polish and I'm hosting the PDF here: Pierwsze kroki z Zend Framework.pdf. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, a couple of people have very generously donated their time to translate my <a href="/zend-framework-tutorial">Zend Framework Tutorial</a> into their native language to help their fellow countrymen.</p>
<p>Firstly, Mario Santagiuliana has provided an Italian translation: <a href="http://www.phpnews.it/corsi/corso-zend-framework/">Introduzione allo Zend Framework</a>. </p>
<p>Secondly, Radosław Benkel translated into Polish and I'm hosting the PDF here: <a href="/wp-content/uploads/Pierwsze-kroki-z-Zend-Framework.pdf">Pierwsze kroki z Zend Framework.pdf</a>.</p>
<p>Thank you both!</p>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1421&amp;md5=270999af42d4c2dfa89dd490d9f15e88" 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/translations-of-my-zend-framework-tutorial-for-zf-1-10-1-11/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Ftranslations-of-my-zend-framework-tutorial-for-zf-1-10-1-11%2F&amp;language=en_GB&amp;category=text&amp;title=Translations+of+my+Zend+Framework+Tutorial+for+ZF+1.10+%26%23038%3B+1.11&amp;description=Recently%2C+a+couple+of+people+have+very+generously+donated+their+time+to+translate+my+Zend+Framework+Tutorial+into+their+native+language+to+help+their+fellow+countrymen.+Firstly%2C+Mario+Santagiuliana+has...&amp;tags=blog" type="text/html" />
	</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>
 <p><a href="http://akrabat.com/?flattrss_redirect&amp;id=1414&amp;md5=29528a2bcdea337b342365b2c3863883" 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/remove-index-php-from-your-url/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework%2Fremove-index-php-from-your-url%2F&amp;language=en_GB&amp;category=text&amp;title=Remove+index.php+from+your+URL&amp;description=One+thing+you+may+have+noticed+with+Zend+Framework+projects+that+use+the+baseUrl%28%29+view+helper+to+reference+CSS+and+other+static+files+is+that+it+doesn%27t+work+if+the...&amp;tags=blog" type="text/html" />
	</item>
	</channel>
</rss>

