<?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</title>
	<atom:link href="http://sourcecodebean.com/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>Two new features I really like in PHP 5.3</title>
		<link>http://sourcecodebean.com/archives/two-new-features-i-really-like-in-php-5-3/531</link>
		<comments>http://sourcecodebean.com/archives/two-new-features-i-really-like-in-php-5-3/531#comments</comments>
		<pubDate>Sun, 06 Jun 2010 16:40:09 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=531</guid>
		<description><![CDATA[Namespaces
Finally PHP has support for namespaces, not a day too late! Before the days of object oriented PHP name clashes for functions was usually prevented by adding a prefix to your function names. When object oriented PHP was introduced function name clashes was less likely, but class names still had to be unique, ie you [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Namespaces</strong><br />
Finally PHP has support for namespaces, not a day too late! Before the days of object oriented PHP name clashes for functions was usually prevented by adding a prefix to your function names. When object oriented PHP was introduced function name clashes was less likely, but class names still had to be unique, ie you could only have one class named for example &#8220;User&#8221;. This led to the need of prefixing class names. From PHP 5.3 we no longer have to do this! </p>
<p>Namespaces in PHP introduced two new keywords:</p>
<ul>
<li>namespace  &#8211; define a namespace</li>
<li>use &#8211; Use a namespace</li>
<li>use &#8230; as  &#8211; Use a namespace but give it an alias name</li>
</ul>
<p><strong>Late static binding</strong></p>
<p><strong>Lambda functions and closures</strong><br />
Lambda functions, or anonymous functions as they are called in PHP, is really something I have missed in PHP. Lambda functions are very convenient to use when you are dealing with callbacks, for example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/preg_replace_callback"><span class="kw3">preg_replace_callback</span></a><span class="br0">&#40;</span><span class="st0">&#8216;~-([a-z])~&#8217;</span>, <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$match</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.php.net/strtoupper"><span class="kw3">strtoupper</span></a><span class="br0">&#40;</span><span class="re0">$match</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>, <span class="st0">&#8216;hello-world&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// outputs helloWorld</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p> <br/></p>
<p>Closures are also a very welcome addition to the language. A closure is not the same thing as an anonymous method, which seem to be a widespread misunderstanding. This is the definition from wikipedia of what a closure is:</p>
<blockquote><p>&#8220;In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be &#8220;closed over&#8221; its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself.&#8221; <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">- Wikipedia</a></p></blockquote>
<p>In PHP closures will allow us to define closures like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$sayhito</span>= <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$name</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <a href="http://www.php.net/printf"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;Hi %s<span class="es0">\r</span><span class="es0">\n</span>&quot;</span>, <span class="re0">$name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$greet</span><span class="br0">&#40;</span><span class="st0">&#8216;Foo&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$greet</span><span class="br0">&#40;</span><span class="st0">&#8216;Bar&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// This will print </span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Hi Foo</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Hi Bar</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p> <br/></p>
<p>Pretty sweet!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/two-new-features-i-really-like-in-php-5-3/531/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yahoo Pipes is still very cool</title>
		<link>http://sourcecodebean.com/archives/yahoo-pipes-is-still-very-cool/626</link>
		<comments>http://sourcecodebean.com/archives/yahoo-pipes-is-still-very-cool/626#comments</comments>
		<pubDate>Thu, 06 May 2010 21:12:04 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=626</guid>
		<description><![CDATA[I know that Yahoo Pipes is not a new thing, it came out in 2007 so in web years it is almost a 100 years now, but it is still darn cool! If you somehow have managed to miss what it is, this is how wikipedia describes it:
&#8220;Yahoo! Pipes is a web application from Yahoo! [...]]]></description>
			<content:encoded><![CDATA[<p>I know that Yahoo Pipes is not a new thing, it came out in 2007 so in web years it is almost a 100 years now, but it is still darn cool! If you somehow have managed to miss what it is, this is how wikipedia describes it:</p>
<blockquote><p>&#8220;Yahoo! Pipes is a web application from Yahoo! that provides a graphical user interface for building data mashups that aggregate web feeds, web pages, and other services, creating Web-based apps from various sources, and publishing those apps&#8221;</p></blockquote>
<p>So why I am blogging about this in 2010? Well, it is still really useful. For many years I have been following the CodeSOD section on the blog &#8220;The daily WTF&#8221;, the rest of the stuff on there is funny as well, but I enjoy the CodeSOD the most. On the blog there was no option of subscribing to only one section, so i deiced to create a Pipe to feed me the CodeSOD only. </p>
<p>The user interface is super easy, just drag items from the list on the left and connect them together:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2010/05/Screen-shot-2010-05-06-at-10.56.54-PM.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/05/Screen-shot-2010-05-06-at-10.56.54-PM.png" alt="CodeSOD Pipe" title="Screen shot 2010-05-06 at 10.56.54 PM" width="484" height="395" class="aligncenter size-full wp-image-629" /></a></p>
<p>My CodeSOD pipe can be found <a href="http://pipes.yahoo.com/pipes/pipe.info?_id=12aff880e6f0a3884f7cfe7b42791e9d">here</a> if you want to subscribe to it!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/yahoo-pipes-is-still-very-cool/626/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First steps toward EPiServer and ASP.NET MVC</title>
		<link>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595</link>
		<comments>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595#comments</comments>
		<pubDate>Mon, 19 Apr 2010 20:33:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=595</guid>
		<description><![CDATA[Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC,  [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC,  to try to get a running prototype up. </p>
<p>I did some research online to see what others had done. I found two great posts by <a href="http://joelabrahamsson.com/entry/episerver-and-mvc-retrieving-current-page-using-a-custom-model-binder">Joel Abrahamsson</a> and one from <a href="http://fbrz.wordpress.com/2009/10/10/episerver-and-mvc/">Fabio Fabrizio</a>, who based his solution on Joels experiments. </p>
<p>Joels first attempt was to create a base class from which all Controllers inherited. However, this made the controller do more than it should and made it very hard to test. His second attempt was to create a custom model binder and make every action receive currentPage as a parameter. This approach makes it much easier to create unit tests for the code, since it is now possible to moc the currentPage parameter. </p>
<p>Fabio took a different approach and decided to implement a custom MVC handler and a controller factory. This approach fits very well with MVC and felt like the right way to go, so I decided to create a prototype based on Fabios code. Fabios code was based on EPiServer CMS 5 and ASP.NET MVC 1.0, but I wanted to use EPiServer 6 and ASP.NET MVC 2.0, so I started porting the solution.</p>
<p>Porting the solution to EPiServer 6 and ASP.NET MVC 2.0 required some changes to both code and configuration, which not all of them was trivial. At the moment I have a prototype up running, and it works remarkably well! I will continue my experiments and post my results.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>EPiServer friendly URLs for paginated pages (and why the asp:LinkButton must die)</title>
		<link>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510</link>
		<comments>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510#comments</comments>
		<pubDate>Mon, 15 Mar 2010 06:05:37 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=510</guid>
		<description><![CDATA[Recently I got a request from a customer to perform some search engine optimizing for an old EPiServer site we are maintaining. One of the optimizations was to fix the paging on their product page. The products are fetched from an external data source and are not stored as pages in EPiServer, thus normal EPiServer [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I got a request from a customer to perform some search engine optimizing for an old EPiServer site we are maintaining. One of the optimizations was to fix the paging on their product page. The products are fetched from an external data source and are not stored as pages in EPiServer, thus normal EPiServer paging controls can not be used. Image a normal paging control like this:</p>
<p>Prev 1, 2 , 3, &#8230;, 99 Next</p>
<p>A fairly normal approach to this would have been to use a query parameter for handling the paging. Like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;a href=&quot;/products/?page=4&quot;&gt;4&lt;/a&gt;</div>
</li>
</ol>
</div>
<p>By using this approach each page would have a unique entry point, using query strings is however not optimal for SEO, but even worse is using asp:LinkButtons. Whoever created the site had decided to use asp:LinkButtons. LinkButtons are very convenient to work with in ASP.NET, but the HTML code they generate is not very SEO friendly. This is the HTML that was generated:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;div id=&quot;ctl00_MainContent_pnlPaging&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl00_lbPage&quot;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl00$lbPage&#8217;,&#8221;)&quot;&gt;1&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl01_lbPage&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl01$lbPage&#8217;,&#8221;)&quot;&gt;2&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &lt;a id=&quot;ctl00_MainContent_rptPages_ctl02_lbPage&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$rptPages$ctl02$lbPage&#8217;,&#8221;)&quot;&gt;3&lt;/a&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;a id=&quot;ctl00_MainContent_lbNext&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;href=&quot;javascript:__doPostBack(&#8216;ctl00$MainContent$lbNext&#8217;,&#8221;)&quot;&gt;Next&lt;/a&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/div&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>We can see that:</p>
<ul>
<li>LinkButtons generates a javascript that is run when the link is clicked, instead of using a normal link</li>
<li>Paging is handled using a postback, the pages will no longer have unique entry points.</li>
</ul>
<p>This is <strong>disastrous </strong>from a SEO perspective.</p>
<p>So lets fix it. First step is to replace the LinkButtons with normal links (asp:Hyperlink) &#8211; and then we are going to do some URL rewrite magic to create SEO-friendly URLs. The code behind was changed to assign links like <code>/products/?page=4</code>.</p>
<p>Now, to get URLs like <code>/products/page/4</code>, we can create a custom url rewrite module in EPiServer. First we create a new class, Rewrite,  that inherits from <code>EPiServer.Web.FriendlyUrlRewriteProvider</code>. In this class we override three methods (don&#8217;t ask we why they have such confusing names, someone at EPiServer must have been under the influsene of something when nameing them):</p>
<ul>
<li><code>ConvertToInternalInternal </code>- Used to convert from <code>/product/page/4/</code> to an internal EPiServer page with the page id as a query parameter</li>
<li><code>ConvertToExternalInternal </code>- Used to convert from an internal EPiServer URL (<code>/PageType.aspx?lotsofqueryparameters=values</code>) to an external (<code>/products/page/4/</code>)</li>
<li><code>ConvertToInternal </code>- Needed to work around URL rewrite caching behaviour in EPiServer, will get into more detail on this later.</li>
</ul>
<p>This is the class:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">namespace Utils</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">public class Rewrite : FriendlyUrlRewriteProvider <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// The regexp to match a paged url</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw4">string</span> _regexpPaging = @<span class="st0">&quot;(.+)/page/([0-9]+)/$&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; protected override bool ConvertToInternalInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, ref object internalObject<span class="br0">&#41;</span><span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; protected override bool ConvertToExternalInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, object internalObject, Encoding toEncoding<span class="br0">&#41;</span><span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; public override bool ConvertToInternal<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; UrlBuilder url, out object internalObject<span class="br0">&#41;</span> <span class="br0">&#123;</span>&#8230;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>The first method we implement is the ConvertToInternalInternal which will convert the URL to an internal EPiServer URL.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected override bool ConvertToInternalInternal<span class="br0">&#40;</span>UrlBuilder url, ref object internalObject<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>url == <span class="kw2">null</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="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Regexp to match if the URL ends with /page/{Id}/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Match match = Regex.<span class="me1">Match</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, _regexpPaging<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// If we have a match, remove the /page/{Id}/ from the end of the URL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// and add a querystring to the internal URL that is dqcPagingId </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// (should have a unique name to not clash with some other querystring)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>match.<span class="me1">Length</span> &gt; <span class="nu0">0</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; url.<span class="me1">Path</span> = match.<span class="me1">Groups</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">Value</span> + <span class="st0">&quot;/&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; url.<span class="me1">QueryCollection</span><span class="br0">&#91;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#93;</span> = match.<span class="me1">Groups</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>.<span class="me1">Value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; base.<span class="me1">ConvertToInternalInternal</span><span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Now when the /page/{Id}/ is removed from the URL, and the querystring is added, we can let EPiServer do its normal URL-rewriting.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> base.<span class="me1">ConvertToInternalInternal</span><span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>While not nessesary, we should override the <code>ConvertToExternalInternal </code>as well. This will let EPiServer automatically convert internal urls containing the <code>dqcPagingId </code>querystring to a external url ending with /page/{Id}/.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected override bool ConvertToExternalInternal<span class="br0">&#40;</span>UrlBuilder url, object internalObject, Encoding toEncoding<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// First let EPiServer convert the internal URL to an external. This will give us a URL like:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// /Products/?dqcPagingId=5 (if it is a paged page)</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;bool isRewritten = base.<span class="me1">ConvertToExternalInternal</span><span class="br0">&#40;</span>url, internalObject, toEncoding<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Check if the URLs query string contains dqcPagingId</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// If it does we add /page/{Id} to the URL and removes the query string</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>url.<span class="me1">Query</span>.<span class="me1">Contains</span><span class="br0">&#40;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; url.<span class="me1">Path</span> = <span class="kw4">string</span>.<span class="me1">Concat</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, <span class="st0">&quot;page/&quot;</span>, url.<span class="me1">QueryCollection</span><span class="br0">&#91;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#93;</span>, <span class="st0">&quot;/&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; url.<span class="me1">QueryCollection</span>.<span class="me1">Remove</span><span class="br0">&#40;</span><span class="st0">&quot;dqcPagingId&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> isRewritten;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><br/></p>
<p>Now one can think we would be done, but there is one more method we need to implement. EPiServer uses a cache to cache URL rewrites from external URLs to internal URLs, but it will only cache the querystrings used by EPiServer, not the <code>dqcPagingId </code>we added. This gives a rather unexpected result. The first time the page is loaded everything loads fine, from code behind we can access the <code>dqcPagingId </code>and show the requested page. If you wait 10 senconds (default cache time) or more and reloads the page, it works fine. But if you reload the page sooner, the dqcPagingId will not be set when the page loads. This is because the querystring <code>dqcPagingId </code>is not cached by EPiServer. This took me quite some time to figure out.</p>
<p>The solution to the problem is to override ConvertToInternal to bypass the default caching:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">public override bool ConvertToInternal<span class="br0">&#40;</span>UrlBuilder url, out object internalObject<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// If the URL end on /page/{id}/, bypass cache by calling ConvertToInternalInternal</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// A more optimal solution would be to perform some kind of caching here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>Regex.<span class="me1">IsMatch</span><span class="br0">&#40;</span>url.<span class="me1">Path</span>, _regexpPaging<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; internalObject = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; ConvertToInternalInternal<span class="br0">&#40;</span>url, ref internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Else, it is ok to use the cached result</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> base.<span class="me1">ConvertToInternal</span><span class="br0">&#40;</span>url, out internalObject<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><br/></p>
<p>The last step is to add our Rewrite module to the web.config:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;providers&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &lt;add
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;name=&quot;MyUrlRewriter&quot;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp;enableSimpleAddress=&quot;true&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;friendlyUrlCacheAbsoluteExpiration=&quot;0:0:10&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;type=&quot;Utils.Rewrite, Utils&quot;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;description=&quot;URL rewriter for paging&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;/providers&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/urlRewrite&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><br/></p>
<p>Now we should be all done! Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/episerver-friendly-urls-for-paginated-pages-and-why-the-asplinkbutton-must-die/510/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trying out CouchDB for the first time</title>
		<link>http://sourcecodebean.com/archives/trying-out-couchdb-for-the-first-time/480</link>
		<comments>http://sourcecodebean.com/archives/trying-out-couchdb-for-the-first-time/480#comments</comments>
		<pubDate>Mon, 08 Mar 2010 06:05:59 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=480</guid>
		<description><![CDATA[There are several good libraries that will abstract accessing CouchDB. However, in order to understand what goes on in the libraries, I think it is important to first understand what is going on on a lower lever, so that is what I will show you. 
Step 1, install CouchDB. CouchDB for OSX can be downloaded [...]]]></description>
			<content:encoded><![CDATA[<p>There are several good libraries that will abstract accessing CouchDB. However, in order to understand what goes on in the libraries, I think it is important to first understand what is going on on a lower lever, so that is what I will show you. </p>
<p>Step 1, install CouchDB. CouchDB for OSX can be downloaded <a href="http://janl.github.com/couchdbx/">here</a>, the bundle contains bouth the Erlang runtime and CouchDB &#8211; no compiling or installing needed, just download the .dmg, mount it and run the application! If you are on Linux it is quite likely that CouchDB is in the repository of your distribution. In Ubuntu CouchDB is found in &#8220;Universe&#8221; and can easily be installed using apt. I haven&#8217;t tried CouchDB on Windows so I can&#8217;t give you any guidance here, if you try it out, please leave a comment. </p>
<p> Now when CouchDB is installed lets explore it using use one of my favorite tools, good old cURL. Port 5984 is the default port for CouchDB. </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$ curl -X GET http:<span class="co1">//localhost:5984/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><span class="st0">&quot;couchdb&quot;</span>:<span class="st0">&quot;Welcome&quot;</span>,<span class="st0">&quot;version&quot;</span>:<span class="st0">&quot;0.10.0&quot;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>CouchDB is up running! Lets create a new database:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$ curl -X PUT http:<span class="co1">//localhost:5984/testdb</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><span class="st0">&quot;ok&quot;</span>:<span class="kw2">true</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>CouchDB&#8217;s responses are also in JSON form. Lets inspect the database we just created:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$ curl -X GET http:<span class="co1">//localhost:5984/testdb</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;db_name&quot;</span>:<span class="st0">&quot;testdb&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&quot;doc_count&quot;</span>:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;doc_del_count&quot;</span>:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;update_seq&quot;</span>:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;purge_seq&quot;</span>:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;compact_running&quot;</span>:<span class="kw2">false</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&quot;disk_size&quot;</span>:<span class="nu0">79</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;instance_start_time&quot;</span>:<span class="st0">&quot;1266963717052501&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;disk_format_version&quot;</span>:<span class="nu0">4</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>CouchDB returns some statistics of the database, we can see that it contains 0 documents. Lets store an empty document in the database:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$ curl -X POST http:<span class="co1">//localhost:5984/testdb/ -H &quot;Content-Type: application/json&quot; -d {}</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;ok&quot;</span>:<span class="kw2">true</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&quot;id&quot;</span>:<span class="st0">&quot;ef40feff87010a6ef3a45a16df5af977&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;rev&quot;</span>:<span class="st0">&quot;1-967a00dff5e02add41819138abb3284d&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>CouchDB returns the unique id for the document and the version number (did i mention that all documents are version controlled?:)). Next step is to fetch all documents:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$ curl -X GET http:<span class="co1">//localhost:5984/testdb/_all_docs</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;total_rows&quot;</span>:<span class="nu0">1</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&quot;offset&quot;</span>:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;rows&quot;</span>:<span class="br0">&#91;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;id&quot;</span>:<span class="st0">&quot;ef40feff87010a6ef3a45a16df5af977&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;key&quot;</span>:<span class="st0">&quot;ef40feff87010a6ef3a45a16df5af977&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;value&quot;</span>:<span class="br0">&#123;</span><span class="st0">&quot;rev&quot;</span>:<span class="st0">&quot;1-967a00dff5e02add41819138abb3284d&quot;</span><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Yes! it is there! Now you should also be able to see your database &#8220;testdb&#8221; and the document you created in the CouchDB GUI.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/trying-out-couchdb-for-the-first-time/480/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CouchDB a NoSQL database</title>
		<link>http://sourcecodebean.com/archives/couchdb-a-nosql-database/460</link>
		<comments>http://sourcecodebean.com/archives/couchdb-a-nosql-database/460#comments</comments>
		<pubDate>Mon, 01 Mar 2010 20:44:29 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=460</guid>
		<description><![CDATA[Traditionally relational databases has been the primary way of storing, sorting and searching data, and for most purposes they are very good at it. However, in the last few years &#8211; with the growth of cloud computing and sites such as Facebook pushing relational databases to its limits, people have started to look for other [...]]]></description>
			<content:encoded><![CDATA[<p>Traditionally relational databases has been the primary way of storing, sorting and searching data, and for most purposes they are very good at it. However, in the last few years &#8211; with the growth of cloud computing and sites such as Facebook pushing relational databases to its limits, people have started to look for other alternatives. The problems with relational databases is that it has been hard to scale them in a vertical way (scale them over several servers with linear, or close to linear, performance increase). A few databases in a cluster is no problem, but when it comes to several terabytes of data, which should be searched in real time, they are simply not enough. CouchDB and the other NoSQL databases aims to provide a truly horizontally scalable database.</p>
<p>NoSQL is a umbrella term for a wide variety of data stores, which all have in common that they do not store data in a relational way. Some examples of NoSQL databases are CouchDB, MongoDB, Amazon SimpleDB and Google BigTable. This is some of the properties they have in common:</p>
<p><strong>1. No schema</strong><br />
The data is stored in one big hashtable like data structure. No schema is needed. </p>
<p><strong>2. No more joins</strong><br />
Joins are slow in general, when they are spread out over several servers it gets even worse. In CouchDB there is no join, instead data should be duplicated. This might sound odd if you like me was taucht back in collage that normalization is of highest importance, and that you should really really avoid to duplicate data in your database. This is still true for most relational databases, but keep in mind that when relational databases was invented, disk space was expensive and normalization was a great way to save a few bytes. Storage is everything but expensive today, this is why NoSQL empathizes that data should instead be duplicated. </p>
<p><strong>3. Eventual consistency</strong><br />
When you update the database there is no longer a quarantine that all subsequent queries will get the updated value immediately, it might take some time depending on the system load. For some systems such as banking systems, this kind of behavior would be a big no no. But for most large web sites, this is no problem, the data is going to be cached in one way or another anyway. </p>
<p><br/></p>
<h2>CouchDB</h2>
<p>CouchDB is a document database, accessible via a RESTful JSON API. Everything stored in the database is a &#8220;document&#8221; and is stored in a flat address space.There are no schemas, the documents are stored and retrieved as JSON objects.</p>
<p>To address this problem of adding structure back to semi-structured data, CouchDB has something called &#8220;views&#8221;. A view is as close as we gets to a SQL query. The views are expressed in Javascript and consist of map and filter functions. They are built dynamically and will not affect the underlying data.</p>
<p>CouchDB is written in Erlang and runs on all systems that the Erlang runtime supports (Linux, Windows, OSX and other unix systems). I have tested CouchDB on Linux and OSX. This is a screenshot of the CouchDBX GUI running on OSX:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2010/02/couchdb.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/02/couchdb.png" alt="" title="couchdb" width="500" height="337" class="aligncenter size-full wp-image-469" /></a></p>
<p>The JSON representation of the same document:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2010/02/couchdb2.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/02/couchdb2.png" alt="" title="couchdb2" width="500" height="388" class="aligncenter size-full wp-image-474" /></a></p>
<p>In my next post I will show you some hands on action with CouchDB.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/couchdb-a-nosql-database/460/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Articles coming up on CouchDB and ExtJS</title>
		<link>http://sourcecodebean.com/archives/articles-coming-up-couchdb-and-extjs/452</link>
		<comments>http://sourcecodebean.com/archives/articles-coming-up-couchdb-and-extjs/452#comments</comments>
		<pubDate>Sun, 14 Feb 2010 23:46:24 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=452</guid>
		<description><![CDATA[It has been a bit too long since my last post so I wanted to give you an short update on what I have been up to lately. Last year I built a web service using Amazon SimpleDB, which got me interested in document based databases. Since then I have been reading and following the [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a bit too long since my last post so I wanted to give you an short update on what I have been up to lately. Last year I built a web service using Amazon SimpleDB, which got me interested in document based databases. Since then I have been reading and following the progress of different open source document based databases, such as CouchDB and MongoDB. </p>
<p>During the time, I have been thinking about what other kind of projects a document based database would be suitable for. Because my lack of imagination (or just because I was too eager to get started:)), i decided to build a simple Web CMS system using PHP and CoachDB. Pages in a CMS system are perfect for storing as documents in a document database, so it seemed like a good choice. </p>
<p>Building the CMS backend actually turned out to be what took the shortest time. I wanted to create a nice looking Javascript based GUI for the editors and administrators, and decided to build it using the javascript library ExtJS. I had never worked with ExtJS before (and I am more of a backend guy than a javascript frontend guy), so it took me some time to get my mind around it, but the result is sweet &#8211; ExtJS is really powerful. </p>
<p>The CMS is far from complete, but so far it has given me quite a few ideas for new blog posts, so stay tuned for the upcoming posts on CoachDB and ExtJS!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/articles-coming-up-couchdb-and-extjs/452/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a JSON-RPC service using Zend Json Server</title>
		<link>http://sourcecodebean.com/archives/creating-a-json-rpc-service-using-zend-json-server/422</link>
		<comments>http://sourcecodebean.com/archives/creating-a-json-rpc-service-using-zend-json-server/422#comments</comments>
		<pubDate>Thu, 07 Jan 2010 08:11:15 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=422</guid>
		<description><![CDATA[In this post I am going to show how easy it is to create a JSON-RPC web service using the built in support in Zend Framework.
First we need to create the php-file that will handle the incoming RPC calls. It is not advised to put this inside the MVC structure of a Zend web application, [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am going to show how easy it is to create a JSON-RPC web service using the built in support in Zend Framework.</p>
<p>First we need to create the php-file that will handle the incoming RPC calls. It is not advised to put this inside the MVC structure of a Zend web application, since that will lead to unessesary complexity and overhead. The Zend people recommend that we create the JSON-RPC under <code>/public/api/vX/</code>, so lets create the file <code>/public/api/v1/jsonrpc.php</code> (if you haven&#8217;t setup your Zend MVC structure, read my blog post  <a href="http://sourcecodebean.com/archives/getting-started-with-the-zend-framework/374">Getting started with the zend framework</a> to get started).</p>
<p>We will have to do the regular bootstrapping to get our application up and running:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Define path to application directory</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_PATH&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; || <a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_PATH&#8217;</span>, <a href="http://www.php.net/realpath"><span class="kw3">realpath</span></a><span class="br0">&#40;</span><a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&#8216;/../../../application&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Define application environment</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_ENV&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; || <a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_ENV&#8217;</span>, <span class="br0">&#40;</span><a href="http://www.php.net/getenv"><span class="kw3">getenv</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_ENV&#8217;</span><span class="br0">&#41;</span> ? <a href="http://www.php.net/getenv"><span class="kw3">getenv</span></a><span class="br0">&#40;</span><span class="st0">&#8216;APPLICATION_ENV&#8217;</span><span class="br0">&#41;</span> : <span class="st0">&#8216;production&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// Ensure library/ is on include_path</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/set_include_path"><span class="kw3">set_include_path</span></a><span class="br0">&#40;</span><a href="http://www.php.net/implode"><span class="kw3">implode</span></a><span class="br0">&#40;</span>PATH_SEPARATOR, <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; &nbsp; <a href="http://www.php.net/realpath"><span class="kw3">realpath</span></a><span class="br0">&#40;</span><span class="st0">&#8216;../../../library&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.php.net/get_include_path"><span class="kw3">get_include_path</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/** Zend_Application */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span> <span class="st0">&#8216;Zend/Application.php&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Create application, bootstrap, and run</span></div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$application</span> = <span class="kw2">new</span> Zend_Application<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; APPLICATION_ENV,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; APPLICATION_PATH . <span class="st0">&#8216;/configs/application.ini&#8217;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$application</span>-&gt;<span class="me1">bootstrap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The next step is to create the class that will be exposed through the service. I will create a very simple class that will simply perform an addition of two ints. It is very important to describe the input parameters using the @param directive in the comment. This information is used by the Json Server when creating the SMD (Service Mapping Description).</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Simple &#8211; sample class to expose via JSON-RPC</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">class</span> Simple</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* Return sum of two variables</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;*</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* @param &nbsp;int $x</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* @param &nbsp;int $y</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* @return array</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> add<span class="br0">&#40;</span><span class="re0">$x</span>, <span class="re0">$y</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;result&#8217;</span> =&gt; <span class="re0">$x</span> + <span class="re0">$y</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The last step to get the JSON-RPC server running:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Instantiate server, etc.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$server</span> = <span class="kw2">new</span> Zend_Json_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$server</span>-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Simple&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="st0">&#8216;GET&#8217;</span> == <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#8216;REQUEST_METHOD&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Indicate the URL endpoint, and the JSON-RPC version used:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$server</span>-&gt;<span class="me1">setTarget</span><span class="br0">&#40;</span><span class="st0">&#8216;/api/v1/jsonrpc.php&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt;<span class="me1">setEnvelope</span><span class="br0">&#40;</span>Zend_Json_Server_Smd::<span class="me2">ENV_JSONRPC_2</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Grab the SMD</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$smd</span> = <span class="re0">$server</span>-&gt;<span class="me1">getServiceMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// Return the SMD to the client</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Content-Type: application/json&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$smd</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$server</span>-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Now your JSON-RPC Server should be up running. Browsing <code>http://{your web server}/api/v1/jsonrpc.php</code> should result in the following SMD:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;transport&quot;</span>:<span class="st0">&quot;POST&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;envelope&quot;</span>:<span class="st0">&quot;JSON-RPC-2.0&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&quot;contentType&quot;</span>:<span class="st0">&quot;application<span class="es0">\/</span>json&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;SMDVersion&quot;</span>:<span class="st0">&quot;2.0&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;target&quot;</span>:<span class="st0">&quot;<span class="es0">\/</span>api<span class="es0">\/</span>v1<span class="es0">\/</span>jsonrpc.php&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;services&quot;</span>: <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="st0">&quot;add&quot;</span>:<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;envelope&quot;</span>:<span class="st0">&quot;JSON-RPC-2.0&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;transport&quot;</span>:<span class="st0">&quot;POST&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;parameters&quot;</span>:<span class="br0">&#91;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;type&quot;</span>:<span class="st0">&quot;integer&quot;</span>,<span class="st0">&quot;name&quot;</span>:<span class="st0">&quot;x&quot;</span>,<span class="st0">&quot;optional&quot;</span>:<span class="kw2">false</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;type&quot;</span>:<span class="st0">&quot;integer&quot;</span>,<span class="st0">&quot;name&quot;</span>:<span class="st0">&quot;y&quot;</span>,<span class="st0">&quot;optional&quot;</span>:<span class="kw2">false</span><span class="br0">&#125;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;returns&quot;</span>:<span class="st0">&quot;array&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="st0">&quot;methods&quot;</span>:<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;add&quot;</span>:<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;envelope&quot;</span>:<span class="st0">&quot;JSON-RPC-2.0&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;transport&quot;</span>:<span class="st0">&quot;POST&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;parameters&quot;</span>:<span class="br0">&#91;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><span class="st0">&quot;type&quot;</span>:<span class="st0">&quot;integer&quot;</span>,<span class="st0">&quot;name&quot;</span>:<span class="st0">&quot;x&quot;</span>,<span class="st0">&quot;optional&quot;</span>:<span class="kw2">false</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><span class="st0">&quot;type&quot;</span>:<span class="st0">&quot;integer&quot;</span>,<span class="st0">&quot;name&quot;</span>:<span class="st0">&quot;y&quot;</span>,<span class="st0">&quot;optional&quot;</span>:<span class="kw2">false</span><span class="br0">&#125;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;returns&quot;</span>:<span class="st0">&quot;array&quot;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>jQuery does not support calling JSON-RPC services out of the box, but fourtenly there is plenty of plugins for jquery that fixes this. One is the JSON-RPC client found <a href="http://www.tanabi.com/projects/jsonrpc ">here</a>. Download the client and put the javascript files into your <code>/js</code> folder. Then create a new file <code>test.html</code> and add the following html:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script LANGUAGE=&quot;javascript&quot; SRC=&quot;js/jquery-1.3.min.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script LANGUAGE=&quot;javascript&quot; SRC=&quot;js/json2.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script LANGUAGE=&quot;javascript&quot; SRC=&quot;js/jquery.zend.jsonrpc.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test = jQuery.Zend.jsonrpc({url: &#8216;/api/v1/jsonrpc.php&#8217;});
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(test.add(1,1)['result']);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;body&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>We are all done! Browsing test.html should result in an alert box containg the result.<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2010/01/result.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/01/result.png" alt="result" title="result" width="420" height="153" class="aligncenter size-full wp-image-442" /></a></p>
<p>Congratulations! You have created a JSON-RPC service!</p>
<p> <br/><br />
Read more about the Zend Framework:</p>
<ul>
<li><a href="http://sourcecodebean.com/archives/creating-a-custom-view-helper-in-the-zend-framework/6">Creating a custom View Helper in the Zend Framework</a></li>
<li><a href="http://sourcecodebean.com/archives/friendly-urls-and-the-zend-router/31">Friendly URLs and the Zend</a></li>
<li><a href="http://sourcecodebean.com/archives/upgrading-from-zend-framework-1-7-to-1-9-the-autoloader/414">Upgrading from Zend Framework 1.7 to 1.9 – The autoloader</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/creating-a-json-rpc-service-using-zend-json-server/422/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Upgrading from Zend Framework 1.7 to 1.9 &#8211; The autoloader</title>
		<link>http://sourcecodebean.com/archives/upgrading-from-zend-framework-1-7-to-1-9-the-autoloader/414</link>
		<comments>http://sourcecodebean.com/archives/upgrading-from-zend-framework-1-7-to-1-9-the-autoloader/414#comments</comments>
		<pubDate>Wed, 30 Dec 2009 16:54:56 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=414</guid>
		<description><![CDATA[Today I upgraded a project we started working on last year from Zend Framework 1.7 to Zend Framework 1.9. I excepted to run into several API incompatibilities, but the only problem I got was the autoloader.
In Zend 1.7, and earlier versions, the autoloader was registered like this:



&#160;


require_once &#34;Zend/Loader.php&#34;;


Zend_Loader::registerAutoload&#40;&#41;;


&#160;



In Zend Framwork 1.9 this has changed slightly, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I upgraded a project we started working on last year from Zend Framework 1.7 to Zend Framework 1.9. I excepted to run into several API incompatibilities, but the only problem I got was the autoloader.</p>
<p>In Zend 1.7, and earlier versions, the autoloader was registered like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span> <span class="st0">&quot;Zend/Loader.php&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">Zend_Loader::<span class="me2">registerAutoload</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>In Zend Framwork 1.9 this has changed slightly, you now have to register the namespaces you want to autoload:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span> <span class="st0">&#8216;Zend/Loader/Autoloader.php&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$loader</span> = Zend_Loader_Autoloader::<span class="me2">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$loader</span>-&gt;<span class="me1">registerNamespace</span><span class="br0">&#40;</span><span class="st0">&#8216;Dqc_&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>This was the only change we needed to do to upgrade from 1.7 to 1.9, quite impressive! </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/upgrading-from-zend-framework-1-7-to-1-9-the-autoloader/414/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with the Zend Framework</title>
		<link>http://sourcecodebean.com/archives/getting-started-with-the-zend-framework/374</link>
		<comments>http://sourcecodebean.com/archives/getting-started-with-the-zend-framework/374#comments</comments>
		<pubDate>Sun, 13 Dec 2009 21:53:00 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=374</guid>
		<description><![CDATA[It used to be a bit tricky to get started with the Zend Framework. The Zend Framework is very flexible and allows you to set it up in almost any way that fits your needs. This means for example that the directory structure and location of files is up to you, however &#8211; there is [...]]]></description>
			<content:encoded><![CDATA[<p>It used to be a bit tricky to get started with the Zend Framework. The Zend Framework is very flexible and allows you to set it up in almost any way that fits your needs. This means for example that the directory structure and location of files is up to you, however &#8211; there is a recommended layout. When I first started using Zend I had to figure out this by looking at examples and reading the (at that time) rather poor documentation available. In Zend Framework 1.9 a tool, <code>zf.sh</code>, was introduced. It simplifies creating a new site a lot. In this blogpost I will guide you though the process of setting up a Zend development environment in OS X. The only part that is OS X specific is MAMP. Zend Framework runs just fine under Windows and Linux as well.  </p>
<p><strong>Step 1 &#8211; Getting a *AMP setup (LAMP, MAMP, WAMP)</strong><br />
The AMP (Apache, Mysql, PHP) stack is available for almost all modern operating systems. I am writing this on my Macbook, so in this tutorial I will use <a href="http://www.mamp.info">MAMP</a>. When I develop PHP in Windows I usually use WAMPServer and in Linux you can install the LAMP-stack using the packaging system in most distributions.  </p>
<p>Setting up MAMP is pretty straightforward, download the <a href="http://www.mamp.info/en/downloads/index.html">MAMP .dmg-file</a> and drag the MAMP folder to your Applications folder.<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2009/12/mamp.png"><img src="http://sourcecodebean.com/wp-content/uploads/2009/12/mamp.png" alt="mamp" title="mamp" width="423" height="344" class="aligncenter size-full wp-image-384" /></a></p>
<p>Start the application and press the &#8220;Open start page&#8221; button to make sure everything works. On the start page you will find information about your site, phpInfo, phpMyAdmin and SqLiteManager. We will get back to configuring the http root directory later.</p>
<p><strong>Step 2 &#8211; Download and install Zend Framework</strong><br />
Go to the <a href="http://framework.zend.com/download/current/">framwork download page</a> and download the minimal distribution. In this tutorial I am using 1.9.6, but the instructions will probably apply to all 1.9 versions. </p>
<p>Extract the downloaded file and move the folder to a shared location, for example /usr/local/. Next step is to create an alias for the zf.sh tool. Edit your ~/.bash_profile and add the following line (change the path to where you moved the extracted files):</p>
<blockquote><p><code>zf=/usr/local/ZendFramework1.9/bin/zf.sh</code></p></blockquote>
<p>This will allow you execute the zf tool without using the full path. Try it out by executing <code>zf show version</code>, it should return the version number of the file you downloaded.</p>
<blockquote><p><code><br />
$ zf show version<br />
Zend Framework Version: 1.9.6<br />
</code></p></blockquote>
<p><strong>Step 3 &#8211; Create your project</strong><br />
Go to the folder where you want to create your new project, in my case ~/Development/. Run <code>zf create project zf-tutorial</code>. This will setup the default directory structure and create the necessary files. </p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2009/12/zendstructure.png"><img src="http://sourcecodebean.com/wp-content/uploads/2009/12/zendstructure.png" alt="Zend Framework directory structure" title="Zend Framework directory structure" width="252" height="397" class="aligncenter size-full wp-image-386" /></a></p>
<p>The application/ folder is where the source code for your website lives. It contains separate folder for models, views and controllers. The public/ folder is the folder that is going to be your document root. </p>
<p>Now you need to copy the Zend library (in my case /usr/local/ZendFramework1.9/library/Zend) or create a symlink for it so your site can find the Zend files. I prefer using a symlink: </p>
<blockquote><p><code><br />
$ cd ~/Development/zf-tutorial/library<br />
$ ln -s /usr/local/ZendFramework1.9/library/Zend/ Zend<br />
</code></p></blockquote>
<p><strong>Step 4 &#8211; Run the project</strong><br />
The last step before we will have running site up is to configure the step that we skipped in Step 1, configuring the Apache document root.<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2009/12/sitesettings.png"><img src="http://sourcecodebean.com/wp-content/uploads/2009/12/sitesettings.png" alt="sitesettings" title="sitesettings" width="359" height="238" class="aligncenter size-full wp-image-388" /></a></p>
<p>Open up MAMP and click the preferences button, under the apache tab you will find the document root. Click select and navigate to the &#8220;public&#8221; folder in your zend project. Apply the settings and restart the server. </p>
<p>Now open your browser and direct it to <a href="http://localhost">http://localhost</a> and you should see the default welcomescreen:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2009/12/zenddefault.png"><img src="http://sourcecodebean.com/wp-content/uploads/2009/12/zenddefault.png" alt="zenddefault" title="zenddefault" width="610" height="408" class="aligncenter size-full wp-image-390" /></a></p>
<p>Now open your <code>application/controllers/IndexController.php</code> and start hacking your code!</p>
<p> <br/><br />
If you want to learn more about the Zend Framework i have some posts on some more advanced topics:</p>
<ul>
<li><a href="http://sourcecodebean.com/archives/creating-a-custom-view-helper-in-the-zend-framework/6">Creating a custom View Helper in the Zend Framework</a></li>
<li><a href="http://sourcecodebean.com/archives/friendly-urls-and-the-zend-router/31">Friendly URLs and the Zend</a></li>
<li><a href="http://sourcecodebean.com/archives/upgrading-from-zend-framework-1-7-to-1-9-the-autoloader/414">Upgrading from Zend Framework 1.7 to 1.9 – The autoloader</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/getting-started-with-the-zend-framework/374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
