<?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/category/zend-framework/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>thoughts and ideas from a .net developer</description>
	<lastBuildDate>Sat, 26 May 2012 09:45:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>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 [...]]]></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><span id="more-422"></span><br />
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>6</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 [...]]]></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><span id="more-374"></span><br />
<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>1</slash:comments>
		</item>
		<item>
		<title>Friendly URLs and the Zend Router</title>
		<link>http://sourcecodebean.com/archives/friendly-urls-and-the-zend-router/31</link>
		<comments>http://sourcecodebean.com/archives/friendly-urls-and-the-zend-router/31#comments</comments>
		<pubDate>Mon, 23 Feb 2009 20:46:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=31</guid>
		<description><![CDATA[Creating custom friendly URLs using the Zend framework is really simple. The default routing setup for Zend is : &#8216;:module/:controller/:action/*&#8217;, (* will match any var/value) which is fine for most setups. However on some pages having the var/value might not look very good, for example this url is not very readable: /popular/index/type/images/page/1/sortOrder/alltime (controller/action/var/value/var/value/var/value) We would [...]]]></description>
			<content:encoded><![CDATA[<p>Creating custom friendly URLs using the Zend framework is really simple. The default routing setup for Zend is : &#8216;:module/:controller/:action/*&#8217;,  (* will match any  var/value) which is fine for most setups. However on some pages having the var/value might not look very good, for example this url is not very readable:</p>
<p><code>/popular/index/type/images/page/1/sortOrder/alltime</code> <em>(controller/action/var/value/var/value/var/value) </em></p>
<p>We would prefer something like this:<br />
<code>/popular/images/1/alltime </code></p>
<p>Luckily Zend provides a very flexible router that we can configure as we want. <span id="more-31"></span>To start with, we create a new config file called routes.ini, and adds these lines:</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">routes.popular.<span class="re1">route </span>=<span class="re2"> popular/:type/:page/:sortOrder</span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.defaults.<span class="re1">controller </span>=<span class="re2"> popular</span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.defaults.<span class="re1">action </span>=<span class="re2"> index</span></div>
</li>
<li class="li2">
<div class="de2">routes.popular.defaults.<span class="re1">type </span>=<span class="re2"> images</span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.defaults.<span class="re1">sortOrder </span>=<span class="re2"> alltime</span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.defaults.<span class="re1">page </span>=<span class="re2"> <span class="nu0">1</span></span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.reqs.<span class="re1">type </span>=<span class="re2"> \w+</span></div>
</li>
<li class="li1">
<div class="de1">routes.popular.reqs.<span class="re1">page </span>=<span class="re2"> \d+</span></div>
</li>
<li class="li2">
<div class="de2">routes.popular.reqs.<span class="re1">sortOrder </span>=<span class="re2"> \w+</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><code>routes.popular.route</code> tells us what to match. <code>routes.popular.defaults.*</code> sets default values for the variables (if none are given in the url). <code>routes.popular.reqs.*</code> sets requirements on the variables, for example that page must be a number.</p>
<p>The last thing we need to do is add a few lines to our bootstrap.php: </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="re0">$config</span> = <span class="kw2">new</span> Zend_Config_Ini<span class="br0">&#40;</span>APPLICATION_PATH . <span class="st0">&#8216;/config/routes.ini&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$router</span> = <span class="re0">$frontController</span>-&gt;<span class="me1">getRouter</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$router</span>-&gt;<span class="me1">addConfig</span><span class="br0">&#40;</span><span class="re0">$config</span>,<span class="st0">&#8216;routes&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p> <br/><br />
If you want to learn more about the Zend Framework:</p>
<ul>
<li><a href="http://sourcecodebean.com/archives/getting-started-with-the-zend-framework/374">Getting started with the Zend Framework</a></li>
<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/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>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/friendly-urls-and-the-zend-router/31/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<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 [...]]]></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. <span id="more-6"></span>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>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: sourcecodebean.com @ 2012-05-30 06:16:04 -->
