<?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>Jonah's Thoughts &#187; events</title>
	<atom:link href="http://php-thoughts.cubedwater.com/tag/events/feed/" rel="self" type="application/rss+xml" />
	<link>http://php-thoughts.cubedwater.com</link>
	<description>On PHP and things related</description>
	<lastBuildDate>Mon, 21 Jun 2010 01:38:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Alternative to the beforeAction() event</title>
		<link>http://php-thoughts.cubedwater.com/2010/alternative-to-the-beforeaction-event/</link>
		<comments>http://php-thoughts.cubedwater.com/2010/alternative-to-the-beforeaction-event/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 01:35:27 +0000</pubDate>
		<dc:creator>Jonah</dc:creator>
				<category><![CDATA[Yii]]></category>
		<category><![CDATA[beforeAction()]]></category>
		<category><![CDATA[events]]></category>

		<guid isPermaLink="false">http://php-thoughts.cubedwater.com/?p=245</guid>
		<description><![CDATA[<link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" /><link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" />I often see that when someone needs something executed on every page request before anything else is executed, they usually put it in the controller beforeAction() method.  Things you might want executed on every page request might be, for example (so you know what I&#8217;m talking about):

Handling the layout
Cleaning all GET and POST parameters [...]]]></description>
			<content:encoded><![CDATA[<link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" /><link rel="stylesheet" type="text/css" href="/wp-content/plugins/syntax.css" /><p>I often see that when someone needs something executed on every page request before anything else is executed, they usually put it in the controller <a href="http://www.yiiframework.com/doc/api/CController#beforeAction-detail">beforeAction()</a> method.  Things you might want executed on every page request might be, for example (so you know what I&#8217;m talking about):</p>
<ul>
<li>Handling the layout</li>
<li>Cleaning all GET and POST parameters (I don&#8217;t like to do this but some people do)</li>
<li>Language handling</li>
</ul>
<p>I&#8217;ve seen people do this with CakePHP as well as with Yii.  I find that <em>beforeAction()</em> is actually not an optimal place to put this sort of code.  Imagine you put code for all of the examples I gave above plus three more in the <em>beforeAction()</em> method.  You might image that the <em>beforeAction()</em> method would become cluttered and hard to read.  I prefer lower coupling than this.  We want these scripts split up into logical places.  And guess what?  With Yii this is easy.  Application Components!</p>
<p>Let me write an example.  Lets say we need to figure out what layout to display to the user on each request.  We would like to execute some logic for this before the controller action is called.  We do <em>not</em> use <em>beforeFilter()</em>.  We instead create an application component.  An application component is simply a class that extends CApplicationComponent.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> LayoutHandler <span class="kw2">extends</span> CApplicationComponent <span class="br0">&#123;</span>
&nbsp;
	<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		parent<span class="sy0">::</span><span class="me2">init</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
		<span class="co1">//Put code here.</span>
&nbsp;
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Now everything in the <em>init()</em> method will be executed when this application component is instantiated.  So now, we just need to get it to instantiate on every request, before the action is executed.  This is done in the application configuration using the <em><a href="http://www.yiiframework.com/doc/api/CModule#preload-detail">preload</a></em> property.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">&lt;?php</span>
<span class="co1">// This is the main Web application configuration. Any writable</span>
<span class="co1">// CWebApplication properties can be configured here.</span>
<span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
	<span class="st_h">'basePath'</span><span class="sy0">=&gt;</span>dirname<span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="sy0">.</span>DIRECTORY_SEPARATOR<span class="sy0">.</span><span class="st_h">'..'</span><span class="sy0">,</span>
&nbsp;
	<span class="co1">// The 'layoutHandler' component will pre-load</span>
	<span class="st_h">'preload'</span><span class="sy0">=&gt;</span>array<span class="br0">&#40;</span><span class="st_h">'layoutHandler'</span><span class="br0">&#41;</span><span class="sy0">,</span>
&nbsp;
	<span class="co1">// application components</span>
	<span class="st_h">'components'</span><span class="sy0">=&gt;</span>array<span class="br0">&#40;</span>
		<span class="st_h">'layoutHandler'</span><span class="sy0">=&gt;</span>array<span class="br0">&#40;</span>
			<span class="st_h">'class'</span><span class="sy0">=&gt;</span><span class="st_h">'path.to.LayoutHandler'</span><span class="sy0">,</span>
		<span class="br0">&#41;</span><span class="sy0">,</span>
	<span class="br0">&#41;</span><span class="sy0">,</span>
<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Now there are two main things I have done here:</p>
<ol>
<li>Defined the <em>LayoutHandler </em>class as an application component in the <em>components</em> property</li>
<li>Set the <em>layoutHandler</em> to <em>preload</em> with the preload property</li>
</ol>
<p>You may find that this method is much more organized than simply stuffing code in <em>CController::beforeAction()</em>.  It also leaves your code more modular.</p>
<p>Note that now you can even access the <em>LayoutHandler </em>class anywhere within your app via <em>Yii::app()->layoutHandler</em>.  Not useful in this example, but this is often very handy.</p>
]]></content:encoded>
			<wfw:commentRss>http://php-thoughts.cubedwater.com/2010/alternative-to-the-beforeaction-event/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
