<?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>Julian Castaneda &#187; Frameworks and API&#8217;s</title>
	<atom:link href="http://www.smooka.com/blog/category/frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smooka.com/blog</link>
	<description>From programming to music!</description>
	<lastBuildDate>Mon, 29 Aug 2011 15:50:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Unit Testing your AJAX requests with Zend Framework and PHPunit</title>
		<link>http://www.smooka.com/blog/2011/01/15/unit-testing-ajax-requests-zend-framework-phpunit/</link>
		<comments>http://www.smooka.com/blog/2011/01/15/unit-testing-ajax-requests-zend-framework-phpunit/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 20:28:26 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Solutions]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[PHPunit]]></category>
		<category><![CDATA[unit test]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=421</guid>
		<description><![CDATA[I ran into a problem yesterday, when trying to create a couple of unit tests using PHPunit along side the Zend Framework. If you want to unit [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.smooka.com/blog/wp-content/uploads/phpunit.gif" alt="" title="phpunit" width="94" height="80" class="alignleft size-full wp-image-432" />I ran into a problem yesterday, when trying to create a couple of unit tests using <a href="https://github.com/sebastianbergmann/phpunit/" target="_blank">PHPunit</a> along side the <a href="http://zendframework.com/" target="_blank">Zend Framework</a>. </p>
<p>If you want to unit test an action in a controller that checks the request to see if is an XML HTTP Request using the <a href="http://www.smooka.com/blog/2009/04/17/5-things-working-with-zend-framework-and-ajax/">isXmlHttpRequest()</a> method provided by the Zend Framework, then you will definitely run into a problem.</p>
<p>isXmlHttpRequest() pretty much checks to see if the $_SERVER['HTTP_X_REQUESTED_WITH'] is set with &#8216;XMLHttpRequest&#8217;, if not, then returns FALSE. I thought that by setting the server variable with XMLHttpRequest directly will allow me to bypass the check.<br />
<span id="more-421"></span></p>
<pre class="brush: php; title: ; notranslate">
function myAjaxTestAction()
{
      $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';

      //then do your ajax tests here
}
</pre>
<p>This solution seemed like it was going to work, but it turns out that isXmlHttpRequest() makes a call to getHeader() like this:</p>
<pre class="brush: php; title: ; notranslate">
public function isXmlHttpRequest()
{
        return ($this-&gt;getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
}
</pre>
<p>What I didn&#8217;t know is that when you are doing unit tests the framework extends the HTTP class and changes the getHeader() to only use headers set using the setHeader or setHeaders methods.<br />
After a couple of minutes tracing over the calls being made. I discovered that the fix was pretty simple, set our headers doing the following in your test case:</p>
<pre class="brush: php; title: ; notranslate">
function myAjaxTestAction()
{
      $this-&gt;request-&gt;setHeader('X_REQUESTED_WITH');

      //then do your ajax tests here
}
</pre>
<p>This will fake an AJAX request and return the expected result.</p>
<p>I hope this article helps many of you since I spent many hours trying to decipher why setting &#8216;XMLHttpRequest&#8217; to the $_SERVER did not work as I expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2011/01/15/unit-testing-ajax-requests-zend-framework-phpunit/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Internalization and Translation</title>
		<link>http://www.smooka.com/blog/2010/08/06/zend-framework-translation-internalization/</link>
		<comments>http://www.smooka.com/blog/2010/08/06/zend-framework-translation-internalization/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 20:08:12 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Translate]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=362</guid>
		<description><![CDATA[This week I set myself to lay the ground work of translation on the application we are doing at work. In our application we are using Zend [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.smooka.com/blog/wp-content/uploads/translate1.png" alt="" title="translate" width="200" height="259" class="alignright size-full wp-image-377" />This week I set myself to lay the ground work of translation on the application we are doing at work. In our application we are using Zend Framework, so I decided to take a look into the Zend_Translate component to see if it was easy and something that could work in our current environment.</p>
<p>After reading the reference guide on <a href="http://framework.zend.com/manual/en/zend.translate.html" target="_blank">Zend_Translate</a> I decided that we were going to use &#8220;<a href="http://www.gnu.org/software/gettext/manual/gettext.html" target="_blank">gettext</a>&#8221; as our translation adapter.  PHP has support for gettext right our of the box, and with the Zend_Translate it&#8217;s easy to change adapters, if you decide that you want to use a different adapter.</p>
<p>Assuming that you already have the latest version of Zend Framework (1.10.*) installed in your server, I&#8217;m going to explain and guide you guys on how I got it all working.<br />
<span id="more-362"></span></p>
<p>First you need to get a tool that we will use to parse your project files to create the translation dictionaries. I&#8217;m using <a href="http://www.poedit.net/" target="_blank">PoEdit</a> which is free and supports multiple platforms.</p>
<p>Now let&#8217;s put some code down in your index.phtml file of you application. This one should be located in /yourapp/application/views/scripts/index/<br />
If it&#8217;s not there, then just create one, but make sure you have to corresponding controller and action.<br />
Inside of this file we are going to put the following:</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php
$t = new Zend_Translate(
	'gettext',  //the adapter
	APPLICATION_PATH.'/translation', //where the lang files will be stored
	'auto',  //set to auto include .mo files
	array('scan' =&gt; Zend_Translate::LOCALE_FILENAME) //set to scan lang files
);

echo $t-&gt;_(&quot;This is my first translation&quot;).&quot;&lt;br/&gt;&quot;;
?&gt;
</pre>
<p>Before this piece of code works, we need to generate our translation files, else it will throw an error stating that the application can&#8217;t find the corresponding language files.</p>
<p>We need to create a new folder in your application folder. I&#8217;m calling it <strong>lang</strong>, but you can call it anything you want. This folder will be used to store your language files. So you should have something like &#8220;/yourapp/application/lang&#8221;.</p>
<p>After this, you need to open up PoEdit:</p>
<ol class="olblue">
<li>
<p>Go to File-&gt;Catalog Manager</p>
</li>
<li>
<p>Click on the &#8220;Create new translations project&#8221; icon</p>
</li>
<li>
<p>Give your project a name</p>
</li>
<li>
<p>Select the directory to store your translation files (&#8220;/yourapp/application/lang&#8221;) and hit OK</p>
</li>
</ol>
<p>With a newly crated project, we can now generate our language file.<br />
To do this,  close the Catalog Manager window</p>
<ol class="olblue">
<li>
<p>Go to File-&gt;New Catalog</p>
</li>
<li>
<p>Give it a project Name and/or version and be sure to feel in the appropriate information. Be sure to select Charset  &#8221;utf-8&#8243;</p>
</li>
<li>
<p>Now, click on the paths tab. Here you will have to add all the dir paths that point to the files that you want to parse for translation. (/yourapp/application/views/scripts/index/index.phtml)</p>
</li>
<li>
<p>Finally, click on the Keywords tab, and make sure you include the keyword the parser will use to identify the text that needs translation. In our example we will put an underscore &#8220;_&#8221;  since in our code we have echo $t-&gt;_(&#8220;This is my first translation&#8221;);</p>
</li>
<li>
<p>Hit OK and make sure that you save the file with a name such as mylang_en.po. The <strong>_en</strong> is crucial, since Zend_Translate will scan the appropriate language file and use this naming convention to load the appropriate file</p>
</li>
</ol>
<p>Since by default the PoEdit PHP parser handles files with .php extension. We need to modify the preferences so that it allows other types, such as .phtml files.<br />
To do this, go to Edit-&gt;Preferences-&gt;Parsers  When you get to parsers select PHP and then click on the EDIT button. In the parser setup window we need to add *.phtml extension to the &#8220;List of extensions&#8221; field in the Language section, separated by semicolons. The field should be something like this: <code>*.php;*.phtml</code><br />
Then, in the Invocation section, you need to add  -L php to the parser command. This should look like this  <code>xgettext --force-po -o %o %C %K %F -L php</code></p>
<p>Once you have completed this, a pop up screen will show the progress of the catalog update and then list you all the parsed strings. Hit OK and then you will be in the translation view. Just select the string you would like to translate save and you are good to go.</p>
<p>Once you&#8217;ve completed the following steps just copy the .po file and create additional one&#8217;s for the languages you want to do translation. Remember the naming convention discussed on the steps above.</p>
<p>In your code to switch languages, you need to put the following line <code>$t->setLocale('es');</code> This for example will switch the locale to Spanish and will try to load a file with &#8220;_es&#8221;</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$t = new Zend_Translate('gettext', APPLICATION_PATH.'/translation', 'auto', array('scan' =&gt; Zend_Translate::LOCALE_FILENAME));

echo $t-&gt;_(&quot;This is my first translation&quot;).&quot;&lt;br/&gt;&quot;;

//change locale to spanish
$t-&gt;setLocale('es');

echo $t-&gt;_(&quot;This is my Second translation&quot;).&quot;&lt;br/&gt;&quot;;
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2010/08/06/zend-framework-translation-internalization/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Working with Google Charts and Visualization API</title>
		<link>http://www.smooka.com/blog/2009/11/09/working-with-google-charts-and-visualization-api/</link>
		<comments>http://www.smooka.com/blog/2009/11/09/working-with-google-charts-and-visualization-api/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 11:00:58 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=232</guid>
		<description><![CDATA[This week I had the chance to play around with Google&#8217;s charts API, and let me tell you that I&#8217;m loving it. In the past couple of [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.smooka.com/blog/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/blog/wp-content/thumbnails/232.jpg&amp;w=70&amp;h=70&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p><img alt="Red line chart with pale gray background" src="http://chart.apis.google.com/chart?cht=lc&amp;chd=s:pqokeYONOMEBAKPOQVTXZdecaZcglprqxuux393ztpoonkeggjp&amp;chco=FF0000&amp;chls=4.0,3.0,0.0&amp;chs=200x125&amp;chxt=x,y&amp;chxl=0:|Jun|July|Aug|1:||20|30|40|50&amp;chf=bg,s,EFEFEF" align="left"/>This week I had the chance to play around with Google&#8217;s charts API, and let me tell you that I&#8217;m loving it. In the past couple of years I&#8217;ve had to integrate charts into the different projects. I have used everything from Fusion Charts, to PHP/SWF Charts, and DOJO&#8217;s charting engine. But, since I learned that Google provided developers with an API to create charts on the fly, I wanted to give it a try. <span id="more-232"></span></p>
<p><b>To create static bart chart is as simple as:</b></p>
<pre class="brush: xml; light: true; title: ; notranslate">
&lt;img src=&quot;http://chart.apis.google.com/chart?
cht=bvs
&amp;chs=200x125
&amp;chd=t:10,50,60,80,40|50,60,100,40,20
&amp;chco=4d89f9,c6d9fd
&amp;chbh=20&amp;chds=0,160&quot;/&gt;
</pre>
<p><b>Let me break out the parameters for you into:</b><img src="http://chart.apis.google.com/chart?cht=bvs&#038;chs=200x125&#038;chd=t:10,50,60,80,40|50,60,100,40,20&#038;chco=4d89f9,c6d9fd&#038;chbh=20&#038;chds=0,160" align="right"/><br />
// this is the chart type<br />
<code>cht=bvs </code><br />
//this is the bar colors<br />
<code>chco=4D89F9,C6D9FD</code><br />
//this is the chart values<br />
<code>chd=t:10,50,60,80,40|50,60,100,40,20</code><br />
//chart dimensions<br />
<code>chds=0,160</code></p>
<p>In  the <a href="http://code.google.com/apis/chart/types.html" rel="nofollow" target="_blank">Google Charts API</a> website you can find more info regarding this simple but useful chart api. But, that&#8217;s not all. They also have the <a href="http://code.google.com/apis/visualization/" rel="nofollow" target="_blank">Google Visualization API</a> where they actually give you more advanced and dynamic set of visualization tools. They pretty much opened their entire library of data visualization helpers to allow developers like yourself create user interfaces that contain elements that some of your users will be used to seeing, since graphs, charts and other elements you will find in this API is already being implemented in google products.</p>
<p>I encourage you to check both, the more simple and straight forward <a href="http://code.google.com/apis/chart/types.html" rel="nofollow" target="_blank">Charts API</a>, or the more advanced and dynamic <a href="http://code.google.com/apis/visualization/" rel="nofollow" target="_blank">Visualtization API</a>. </p>
<p>Feel free to let me and the readers know on the exiting ways you are utilizing those API&#8217;s</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2009/11/09/working-with-google-charts-and-visualization-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Zend Framework and Firebug &#8211; Log and Debug your Projects</title>
		<link>http://www.smooka.com/blog/2009/08/21/zend-framework-firebug-log-debug/</link>
		<comments>http://www.smooka.com/blog/2009/08/21/zend-framework-firebug-log-debug/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 12:53:57 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[wildfire]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=162</guid>
		<description><![CDATA[When developing an application there are some important factors that you have to pay close attention to avoid problems in the future. I think one of the [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.smooka.com/blog/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/blog/wp-content/thumbnails/162.jpg&amp;w=70&amp;h=70&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p><img src="http://www.smooka.com/blog/wp-content/uploads/firebug.jpg" alt="firebug" title="firebug" width="162" height="162" class="alignleft size-full wp-image-167" />When developing an application there are some important factors that you have to pay close attention to avoid problems in the future. I think one of the most important is logging information on how your application is working and when it fails.</p>
<p>Must of us like to log only big exceptions and fatal errors, but the truth is, that when you are in the development process it&#8217;s very important to keep track of not just errors, but important information of when something gets executed. As a web developer one of the most important tools to have is <a href="http://getfirebug.com/">FireBug</a>. If you didn&#8217;t know, FireBug has an API that you can use to send console messages for logging purposes, when debugging JavaScript. But, did you know you can use FireBug to debug your php applications?<span id="more-162"></span></p>
<p>Through <a href="http://www.wildfirehq.org/">Wildfire</a>, a project that was created to help developers have a standard way for sending messages between different programming languages and scripts. The <a href="http://framework.zend.com/">Zend Framework</a> is now able to communicate to the Firebug console in order to display logging messages injected by PHP.</p>
<p>Using this component is pretty straight forward if you are using Zend_Controller_Front (MVC) with zend Framework.</p>
<p>Place this in your bootstrap file before dispatching your front controller</p>
<pre class="brush: php; title: ; notranslate">
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);
</pre>
<p>Then whenever you want to log something to Firebug just use it in your model, view, or controller like this.</p>
<pre class="brush: php; title: ; notranslate">
$logger-&gt;log('This is an INFORMATIONAL log message!', Zend_Log::INFO);
$logger-&gt;log('This is a WARNING log message!', Zend_Log::WARN);
$logger-&gt;log('This is an ERROR log message!', Zend_Log::ERROR);
</pre>
<p>If you notice in the example above, you can specify the type of message you want to log (INFO, WARN, etc..). This is helpful when you want to clearly identify what type of message you are viewing since it will apply a special formatting and in some instances provide more information.</p>
<p>If you want to use the <a href="http://framework.zend.com/manual/en/zend.log.writers.html#zend.log.writers.firebug">Zend_Log_Writer_Firebug </a>as a stand alone here is the sample code you can use:</p>
<pre class="brush: php; title: ; notranslate">
//Instantiate the Firebug Writer
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);

//start the wildfire component
$request = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();
$channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
$channel-&gt;setRequest($request);
$channel-&gt;setResponse($response);

// Start output buffering
ob_start();

// Now you can make calls to the logger

$logger-&gt;log('This is a log message!', Zend_Log::INFO);

// Flush log data to browser
$channel-&gt;flush();
$response-&gt;sendHeaders();
</pre>
<p>If you don&#8217;t use ZendFramework there is another alternative to php developers. FirePhp, will also enable you to send log messages to Firebug by using php method calls. Be sure to check <a href="http://www.firephp.org/">FirePhp website</a> to get more information.</p>
<p>So remember that this is just one easy and great way to debug your projects, there are many other tools and methods you can incorporate into your projects. If you want to share other ways or simply just want to ask a question regarding this post, feel free to leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2009/08/21/zend-framework-firebug-log-debug/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Zend Cache and Zend Feed Problem</title>
		<link>http://www.smooka.com/blog/2009/04/22/zend-cache-and-zend-feed-problem/</link>
		<comments>http://www.smooka.com/blog/2009/04/22/zend-cache-and-zend-feed-problem/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 17:23:00 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Cache]]></category>
		<category><![CDATA[Zend_Feed]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=88</guid>
		<description><![CDATA[Today I ran into a problem while caching feeds using Zend_Cache. I decided that I wanted to cache the returned object by the Zend_Feed. 

While iterating through the feed, the script would output the following message "Warning: Invalid argument supplied for foreach()", under, "Zend/Feed/Element.php on line 322" on an empty cache.]]></description>
			<content:encoded><![CDATA[<p>Today I ran into a problem while caching feeds using Zend_Cache. I decided that I wanted to cache the returned object by the Zend_Feed. I had something like this:</p>
<pre class="brush: php; title: ; notranslate">
$rssFeedLink = 'http://www.smooka.com/blog/';
$cacheId = 'rss_feed';

$frontendOptions = array(
		   	'lifetime' =&gt; 1800, // cache lifetime of 30 mins
		   	'automatic_serialization' =&gt; true);

$backendOptions = array(
		    	// Directory where to put the cache files
		    	'cache_dir' =&gt; '/cache/'
			);

// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory('Core','File', $frontendOptions, $backendOptions);

if (!$rss = $cache-&gt;load($cacheId)) {
	try {
		$rss = Zend_Feed::import($rssFeedLink);
	} catch (Exception $e) {
		$feedError = true;
	}

	//check to see if it did not fail
	if (!$feedError &amp;&amp; is_object($rss))
	{
		$cache-&gt;save($rss, $cacheId);
	}
}

//assuming that everything went right while fetching the feed
foreach ($rss as $item) {
	$channel['items'][] = array(
		'title'       =&gt; $item-&gt;title(),
		'link'        =&gt; $item-&gt;link(),
		'description' =&gt; $item-&gt;description()
	);
}
</pre>
<p><span id="more-88"></span><br />
While iterating through the feed, the script would output the following message &#8220;Warning: Invalid argument supplied for foreach()&#8221;, under, &#8220;Zend/Feed/Element.php on line 322&#8243; on an empty cache.</p>
<p>It appears that Zend_Feed and Zend_Cache do not get along, I&#8217;m pretty sure it&#8217;s a bug since it looks like Zend_Feed_Rss is sent to sleep by the $cache->save() call and it&#8217;s not waking up automatically. The solution that I found it&#8217;s pretty straight forward. Just modify the following piece of code.</p>
<pre class="brush: php; title: ; notranslate">
if (!$feedError &amp;&amp; is_object($rss))
{
	$cache-&gt;save($rss, $cacheId);
	//ADD a wakeup call to the RSS object
	$rss-&gt;__wakeup();
}
</pre>
<p>That should do the trick!</p>
<p>After I fixed the issue, I went back to the drawing board and decided that it was more efficient to cache the actual HTML output, instead of the returned object by Zend_Feed. That seems to work just fine, since the call to $cache->save() is done after the iteration of the feed items. That fixes the issue and at the same time you are eliminating the need to iterate and re-build the HTML every time the page is loaded.</p>
<p>Hope this information helps you, and if you have any questions or comments be sure to post them here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2009/04/22/zend-cache-and-zend-feed-problem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>5 Things: Working with Zend Framework and Ajax</title>
		<link>http://www.smooka.com/blog/2009/04/17/5-things-working-with-zend-framework-and-ajax/</link>
		<comments>http://www.smooka.com/blog/2009/04/17/5-things-working-with-zend-framework-and-ajax/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 13:02:09 +0000</pubDate>
		<dc:creator>Julian</dc:creator>
				<category><![CDATA[Frameworks and API's]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.smooka.com/blog/?p=60</guid>
		<description><![CDATA[Today I wanted to write about 5 awesome Zend Framework components/helpers that can help you when working with ajax.
<ul>
	<li>isXmlHttpRequest()</li>
	<li>JSON action helper</li>
	<li>AutoComplete</li>
	<li>ContextSwitch &#038; AjaxContext</li>
</ul>]]></description>
			<content:encoded><![CDATA[<p><div style="text-align:center"><script type="text/javascript"><!--
google_ad_client = "pub-7272171247401837";
/* 468x15, created 8/17/10 */
google_ad_slot = "2361033267";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><br />
Today I wanted to write about 5 awesome Zend Framework components/helpers that can help you when working with ajax.</p>
<ul>
<li><a href="http://framework.zend.com/manual/en/zend.controller.request.html#zend.controller.request.http.ajax" target="blank">isXmlHttpRequest()</a></li>
<li><a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.json" target="blank">JSON action helper</a></li>
<li><a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.autocomplete">AutoComplete</a></li>
<li><a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">ContextSwitch</a></li>
<li><a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">AjaxContext</a></li>
</ul>
<h2>isXmlHttpRequest()</h2>
<p>This handy little method can eliminate the need to send extra parameters such as /ajax/true/ to let the controller know it&#8217;s an Ajax request. <span id="more-60"></span>Most standard JavaScript frameworks (jQuery, YUI, Prototype) pass the X-Requested-With HTTP header with the value &#8216;XMLHttpRequest&#8217; when doing an Ajax request. isXmlHttpRequest() basically just checks to see if that header is set and if it is, then execute the code that needs to be executed if it&#8217;s an Ajax call.</p>
<pre class="brush: php; title: ; notranslate">
/**
* myAction from myController
*/
function myAction()
{
if  ($this-&gt;getRequest()-&gt;isXmlHttpRequest()) {
// do the handling of your ajax request
}
else {
// if it's not an ajax request then do regular handling here
}
}
</pre>
<h2>JSON action helper</h2>
<p>I was surprised when I found this action helper in the framework. The JSON helper is pretty slick. It does 5 things that usually I had to do myself.</p>
<ul>
<li>It Disables layouts if currently enabled.</li>
<li>Disables the ViewRenderer if currently enabled.</li>
<li>Sets the &#8216;Content-Type&#8217; response header to &#8216;application/json&#8217;.</li>
<li>Immediately returns the response, without waiting for the action to finish execution.</li>
<li>Array passed as argument will be returned as a json formatted response.</li>
</ul>
<p>So if you use this helper along with the isXmlHttpRequest() method you can easily handle any Ajax request and return a response with minimum amount of programming.</p>
<pre class="brush: php; title: ; notranslate">
/**
* myAction from myController
*/
function myAction()
{
if ($this-&gt;getRequest()-&gt;isXmlHttpRequest()) {
// do the handling of your ajax request

$myArrayofData = array('a','b','c');

//encode your data into JSON and send the response
$this-&gt;_helper-&gt;json($myArrayofData);
//nothing else will get executed after the line above

}
else {
// if it's not an ajax request then do regular handling here
}
}
</pre>
<h2>AutoComplete</h2>
<p>This helper is currently supported only by Dojo and Scriptaculous AJAX libraries, so if you are working with any of those libraries, Zend&#8217;s AutoComplete will make your life a little bit easier. AutoComplete will do the same actions as the JSON helper (disable layout, sent appropriate content type, etc&#8230;) but will do the extra step to make sure your data is formatted correctly to use with the auto complete feature of the previously mentioned libraries. Definitely check this helper out if you are working with auto complete functionality.<br />
To read more and see examples <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.autocomplete">click here</a>.</p>
<h2>ContextSwitch and AjaxContext</h2>
<p>These last two are very similar, If I&#8217;m not mistaken AjaxContext extends ContextSwitch to contain all the functionality of ContextSwitch, but, there are a couple of key differences that you must know! ContextSwitch is a great way to direct a request to it&#8217;s appropiate response. Let&#8217;s say you have a page that is meant to list information. With ContextSwitch you can make the action output a regular page for it&#8217;s regular call, but, if the user switched the request to ?request=xml. The user will now get the xml version! You only need to create a view script like this: myaction.xml.phtml, and handle the display there.<br />
AjaxContext it&#8217;s essentially a wrapper to  ContextSwitch that it&#8217;s only triggered if an XmlHttpRequest is sent in the headers. I suggest you head over to the ZendFramework&#8217;s Official documentation to read in depth on how <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">ContextSwitch</a> &amp; <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.ajaxcontext">AjaxContext</a> work since there is a a lot to cover, and there is really no point in repeating was is already in the FrameWork docs.</p>
<p>I hope this post helps you, and if you have any more interesting or helpful Zend Framework tips, you are invited to post them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smooka.com/blog/2009/04/17/5-things-working-with-zend-framework-and-ajax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

