<?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>blog.log2e.com &#187; Uncategorized</title>
	<atom:link href="http://blog.log2e.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.log2e.com</link>
	<description>Tutorials and Code Snippets</description>
	<lastBuildDate>Tue, 25 May 2010 12:47:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Utilizing the Zend AMF Server Inside a Zend Controller</title>
		<link>http://blog.log2e.com/2008/12/14/utilizing-the-zend-amf-server-inside-a-zend-controller/</link>
		<comments>http://blog.log2e.com/2008/12/14/utilizing-the-zend-amf-server-inside-a-zend-controller/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 00:39:02 +0000</pubDate>
		<dc:creator>Stefan Schmalhaus</dc:creator>
				<category><![CDATA[AMFPHP]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Zend Amf]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://blog.log2e.com/?p=199</guid>
		<description><![CDATA[Many tutorials suggest that you set up the Zend AMF server directly in the bootstrap or index.php file. But what if you want to build a hybrid application with both Flex and HTML interfaces? In this case it&#8217;s better to let the bootstrap file do its usual job of application dispatching, and to make the [...]]]></description>
			<content:encoded><![CDATA[<p>Many tutorials suggest that you set up the Zend AMF server directly in the bootstrap or <em>index.php</em> file. But what if you want to build a hybrid application with both Flex and HTML interfaces? In this case it&#8217;s better to let the bootstrap file do its usual job of application dispatching, and to make the AMF gateway a controller.</p>
<p><span id="more-199"></span></p>
<h3>Introducing the Gateway Controller</h3>
<p>Setting up a Zend AMF server inside a Zend controller is pretty easy:</p>
<div class="geshi no php">
<div class="head">&lt;?php</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> GatewayController <span class="kw2">extends</span> Zend_Controller_Action </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="kw2">public</span> <span class="kw2">function</span> indexAction<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">getHelper</span><span class="br0">&#40;</span><span class="st0">&#39;ViewRenderer&#39;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$server</span> <span class="sy0">=</span> <span class="kw2">new</span> Zend_Amf_Server<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$server</span><span class="sy0">-&gt;</span><span class="me1">addDirectory</span><span class="br0">&#40;</span> <span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&#39;/../services/&#39;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw3">echo</span><span class="br0">&#40;</span><span class="re1">$server</span><span class="sy0">-&gt;</span><span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;</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>
</ol>
</div>
<p>Since we do not want to output any visible content we disable the view renderer (otherwise the action controller will look for a view script in a subdirectory named after the controller). The endpoint URI of our AMF server gateway is now something like <em>http://www.mydomain.com/gateway/</em>, and doesn&#8217;t interfere with our other controllers. By calling the <code>addDirectory()</code> method we tell the server where to look for the services. We only need to provide a base directory here. If we set up our remote objects in Flex with full namespaces the AMF server is able to resolve the dot syntax into the appropriate path. </p>
<p>For example, the dot syntax in the source attribute of this remote object</p>
<p><code>&lt;mx:RemoteObject id="articlesRO" showBusyCursor="true" source="com.xeladon.cms.ArticlesService" destination="zend"/&gt;</code></p>
<p>will be transformed into the path <code>com/xeladon/cms/ArticlesService</code>. Please note that in this setup there is no need for explicitly propagating each service class to the server with the <code>setClass()</code> method. For more information on the different approaches (<code>addDirectory()</code> vs. <code>setClass()</code>) please refer to Wade Arnold&#8217;s blog post on <a href="http://wadearnold.com/blog/?p=103" target="_blank" onclick="pageTracker._trackPageview('/outgoing/wadearnold.com/blog/?p=103&amp;referer=');">Lazy Loading Classes</a>. </p>
<h3>Organizing Value Objects with Namespaces</h3>
<p>Now let&#8217;s take this one step further. As a long-time AMFPHP user, I have gotten in the habit of organizing my value objects with namespaces. I usually put my VOs in a folder below the service classes:</p>
<p><a href="http://blog.log2e.com/wp-content/uploads/2008/12/zend_amf_controller_setup.gif"><img class="alignnone size-medium wp-image-120" title="Zend Framework Folder Structure" src="http://blog.log2e.com/wp-content/uploads/2008/12/zend_amf_controller_setup.gif" alt="" width="280" height="434" /></a></p>
<p>We can still do this with Zend but we have to make sure that Zend Loader is able to find the VO classes. I suggest adding the path to the include path in the <em>index.php</em> file:</p>
<div class="geshi no php">
<div class="head">&lt;?php</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;APPLICATION_PATH&#39;</span><span class="sy0">,</span> <span class="kw3">realpath</span><span class="br0">&#40;</span><span class="kw3">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&#39;/../application/&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">set_include_path</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">get_include_path</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sy0">.</span> PATH_SEPARATOR <span class="sy0">.</span> APPLICATION_PATH <span class="sy0">.</span> <span class="st0">&#39;/../library/&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sy0">.</span> PATH_SEPARATOR <span class="sy0">.</span> APPLICATION_PATH <span class="sy0">.</span> <span class="st0">&#39;/models/&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="sy0">.</span> PATH_SEPARATOR <span class="sy0">.</span> APPLICATION_PATH <span class="sy0">.</span> <span class="st0">&#39;/services/com/xeladon/cms/vo/&#39;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<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><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">Zend_Loader<span class="sy0">::</span><span class="me2">registerAutoload</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<h3>Adding More Functionality</h3>
<p>Based on the above setup, we can easily add more functionaliy to the bootstrap file without worrying about the AMF server gateway. We would probably want to make use of <code>Zend_Config</code> for configuring the application, and <code>Zend_Db</code> for managing database interactions. By adding a few more lines of code to the bootstrap file, these functions become automatically available to the AMF service classes. </p>
<p>Let&#8217;s assume we set up a database adapter in the bootstrap file. This allows us to write very clean code inside the service classes. The following code is just an example (where <code>ArticleVO</code> is a typical value object, and <code>Articles</code> is supposed to be a Zend model class that extends <code>Zend_Db_Table</code>):</p>
<div class="geshi no php">
<div class="head">&lt;?php</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ArticlesService</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span> &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="kw2">function</span> getArticles<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$articles</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$table</span> <span class="sy0">=</span> <span class="kw2">new</span> Articles<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$result</span> <span class="sy0">=</span> <span class="re1">$table</span><span class="sy0">-&gt;</span><span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re1">$result</span> <span class="kw1">as</span> <span class="re1">$row</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span> <span class="sy0">=</span> <span class="kw2">new</span> ArticleVO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span><span class="sy0">-&gt;</span><span class="me1">id</span> <span class="sy0">=</span> <span class="re1">$row</span><span class="sy0">-&gt;</span><span class="me1">id</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span><span class="sy0">-&gt;</span><span class="me1">author</span> <span class="sy0">=</span> <span class="re1">$row</span><span class="sy0">-&gt;</span><span class="me1">author</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span><span class="sy0">-&gt;</span><span class="me1">title</span> <span class="sy0">=</span> <span class="re1">$row</span><span class="sy0">-&gt;</span><span class="me1">title</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span><span class="sy0">-&gt;</span><span class="me1">teaser</span> <span class="sy0">=</span> <span class="re1">$row</span><span class="sy0">-&gt;</span><span class="me1">teaser</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$article</span><span class="sy0">-&gt;</span><span class="me1">body</span> <span class="sy0">=</span> <span class="re1">$row</span><span class="sy0">-&gt;</span><span class="me1">body</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">array_push</span><span class="br0">&#40;</span><span class="re1">$articles</span><span class="sy0">,</span> <span class="re1">$article</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$articles</span><span class="sy0">;</span> &nbsp; &nbsp; &nbsp; &nbsp;</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>
</ol>
</div>
<h3>Summary</h3>
<p>What do we gain with all this? </p>
<ol>
<li>We keep things organized and can rely on the same structures (packages, folders, namespaces) in both ActionScript and PHP.</li>
<li>We can use other powerful features of the Zend framework for the application&#8217;s server-side logic.</li>
<li>We have the flexibility of adding Flex/Flash and HTML interfaces to the same core application whenever and whereever needed.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.log2e.com/2008/12/14/utilizing-the-zend-amf-server-inside-a-zend-controller/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
	</channel>
</rss>

