<?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>source code bean &#187; Zend Framework</title>
	<atom:link href="http://sourcecodebean.com/archives/tag/zend-framework/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>giving you tricks and tips of good coding</description>
	<lastBuildDate>Sun, 06 Jun 2010 16:40:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a custom View Helper in the Zend Framework</title>
		<link>http://sourcecodebean.com/archives/creating-a-custom-view-helper-in-the-zend-framework/6</link>
		<comments>http://sourcecodebean.com/archives/creating-a-custom-view-helper-in-the-zend-framework/6#comments</comments>
		<pubDate>Sat, 14 Feb 2009 12:16:29 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=6</guid>
		<description><![CDATA[Zend Framework ships with several View Helpers, such as the url View Helper. Anywhere in a view script one can call $this-&#62;url() to generate a url to a certain page. For example:



$this-&#62;url&#40;array&#40;


&#160; &#8216;module&#8217; =&#62; &#8216;moduleName&#8217;,


&#160; &#8216;action&#8217; =&#62; &#8216;actionName&#8217;,


&#160; &#8216;additionalParam&#8217; =&#62;&#8216;value&#8217;&#41;&#41;



I wanted to create my own helper, for generating paths to thumbnails, and it turned out [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Framework ships with several View Helpers, such as the url View Helper. Anywhere in a view script one can call $this-&gt;url() to generate a url to a certain page. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$this</span>-&gt;<span class="me1">url</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;module&#8217;</span> =&gt; <span class="st0">&#8216;moduleName&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;action&#8217;</span> =&gt; <span class="st0">&#8216;actionName&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&#8216;additionalParam&#8217;</span> =&gt;<span class="st0">&#8216;value&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>I wanted to create my own helper, for generating paths to thumbnails, and it turned out to be really simple. Since I wanted to be able to access the helper from any module, I created the view helper class in <code>/library/My/View/Helper/Thumbnail.php</code>. The definition of the class looks like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> My_View_Helper_Thumbnail <span class="kw2">extends</span> Zend_View_Helper_Abstract</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">function</span> thumbnail<span class="br0">&#40;</span><span class="re0">$id</span>, <span class="re0">$type</span>, <span class="re0">$width</span>, <span class="re0">$height</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="re0">$thumbModel</span> = <span class="kw2">new</span> My_Model_Thumbnails<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$thumbModel</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; -&gt;<span class="me1">getThumbnail</span><span class="br0">&#40;</span><span class="re0">$id</span>, <span class="re0">$width</span>, <span class="re0">$height</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The <code>getThumbnail()</code> function will check if there exists a thumbnail (of size <code>$width</code> x <code>$height</code>) for the image with id <code>$id</code>, if one exists the path to it is returned. Otherwise the thumbnail will be generated on the fly.<br />
The last piece of magic before it is possible to use our new view helper is to add one line to our <code>bootstrap.php</code>. After Zend MVC has been started, the path to our new view helper must be added to the layout in orders for view to be able to find it.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$layout</span> = Zend_Layout::<span class="me2">startMvc</span><span class="br0">&#40;</span>APPLICATION_PATH . <span class="st0">&#8216;/layouts/scripts&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$layout</span>-&gt;<span class="me1">getView</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">addHelperPath</span><span class="br0">&#40;</span><span class="st0">&#8216;My/View/Helper&#8217;</span>, <span class="st0">&#8216;My_View_Helper&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>When this is done it is possible to call <code>$this-&gt;thumbnail()</code> in all view scripts in the project.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/creating-a-custom-view-helper-in-the-zend-framework/6/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
