<?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>Derivante &#187; PHP on Rails</title>
	<atom:link href="http://www.derivante.com/tag/php-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.derivante.com</link>
	<description>to obtain or receive from a source</description>
	<lastBuildDate>Mon, 26 Apr 2010 18:44:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PHP on Rails &#8211; The Flash</title>
		<link>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/</link>
		<comments>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 14:15:44 +0000</pubDate>
		<dc:creator>Jacques Fuentes</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP on Rails]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=162</guid>
		<description><![CDATA[Last week I wrote an article about extending my framework (Pho framework courtsey of Kien La) to DRY your code and to be more similar to Rails. Today, I would like to present to you another class that will help extend your framework in similar fashion. The Flash If you aren't familiar with the way [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-425" style="margin: 15px;" title="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" alt="php-med-trans-light" width="95" height="51" />Last week I wrote an article about extending my framework (Pho framework courtsey of Kien La) to DRY your code and to be more similar to Rails.</p>
<p>Today, I would like to present to you another class that will help extend your framework in similar fashion.</p>
<h3>The Flash</h3>
<p>If you aren't familiar with the way RoR uses "flash" messaging, you can take a look at the <a title="RoR API Flash" href="http://api.rubyonrails.org/classes/ActionController/Flash.html" target="_blank">RoR API</a>, or you can read this explanation:</p>
<blockquote><p>The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out.</p></blockquote>
<p>The flash allows you to place messages in a user's session that can be viewed when navigating (or redirecting) to different pages or even the current page. If you read further into the API, you will see that the RoR flash offers you the following behavior:</p>
<ul>
<li>Set a regular message (viewable upon next http request)</li>
<li>Create a "now" message (viewable now and <strong>not</strong> upon next http request)</li>
<li>"Keep" a message or all messages (is a message is viewable now, it will now also be viewable upon the next http request)</li>
<li>Discard/unset a message or all messages</li>
</ul>
<p>If you're a good little coder, you would first attempt to steal this code from somewhere else. I have found a few sites that offer 'some' of the functionality of the RoR flash; however, I would like to have the flash in its full capacity. Therefore, I have created my own class.</p>
<p>The behavior of the flash is rather simple and so is my class. If you think about the lifetime of the messages, it should hit you that we have essentially two types of flash messages: now and later (next http request). Even though we have two types of messages, we actually only have one corresponding message. This means that a certain flash message "error" could have a now <em>and</em> a later message. The reason behind this is beceause we want to use discard() and keep() which we should be able to pass a 'key' to when invoked so that discard would remove all traces of our "error" message (or all messages if no key is passed). My class requires php &gt;= version 5. Keep in mind that you will only want one instance of this class, so you could make it a singleton if you'd like. The constructor does all of the work by removing old messages and such, so you only have to worry about setting messages. Okay, let's see some code.</p>
<div><a href="http://www.derivante.com/wp-content/uploads/2008/12/flash.zip">Download UserSessionFlash </a></div>
<pre class="php">&lt;!--p
<span style="color: #808080; font-style: italic;"># Instantiate flash which will remove old messages;</span>
<span style="color: #0000ff;">$flash</span> = <span style="color: #000000; font-weight: bold;">new</span> UserSessionFlash;
<span style="color: #0000ff;">$flash</span>-&gt;<span style="color: #006600;">error</span> = <span style="color: #ff0000;">'This is an error message for the next page request.'</span>;
<span style="color: #0000ff;">$flash</span>-&amp;gt;now<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span>, <span style="color: #ff0000;">'Message will be available for the current page request.'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Allows you to use the 'notice' for the next page request.</span>
<span style="color: #808080; font-style: italic;"># If you do not pass a key, then it will keep ALL messages.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;keep<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;"># The $flash-&amp;gt;error above has now been erased.</span>
<span style="color: #808080; font-style: italic;"># Just like keep, if you do not pass a key, then ALL messages will be erased.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;discard<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'error'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;"># Delete all messages.</span>
<span style="color: #0000ff;">$flash</span>-&amp;gt;discard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
?&amp;gt;</pre>
<p>As you can see this is all fairly basic. Simply add the flash instance to your action controller, and you access it via your page controller. What I have done is included a function in my action controller called flash() that returns my instance of the flash class. So my page controller would call</a></p>
<pre class="php"><span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;error = <span style="color: #ff0000;">'This is an error msg.'</span>;
<span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;now<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'notice'</span>, <span style="color: #ff0000;">'Show me the money.'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>If you'd like, you can check out some other implementations below. Again, none of the other classes I've found offer the full rails flash behavior.</p>
<p><a href="http://poorbuthappy.com/ease/archives/2007/04/22/3589/rails-flash-in-php">http://poorbuthappy.com/ease/archives/2007/04/22/3589/rails-flash-in-php</a><br />
<a href="http://www.phpclasses.org/browse/package/3668.html">http://www.phpclasses.org/browse/package/3668.html</a><br />
<a href="http://shabadeehoob.com/2007/03/17/rails-like-flash-messages-in-cakephp/">http://shabadeehoob.com/2007/03/17/rails-like-flash-messages-in-cakephp/</a><br />
<a href="http://api.phpontrax.com/__filesource/fsource_PHPonTrax__vendortraxsession.php.html">http://api.phpontrax.com/__filesource/fsource_PHPonTrax__vendortraxsession.php.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2008/12/15/php-on-rails-the-flash/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP on Rails</title>
		<link>http://www.derivante.com/2008/12/08/php-on-rails/</link>
		<comments>http://www.derivante.com/2008/12/08/php-on-rails/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 18:23:07 +0000</pubDate>
		<dc:creator>Jacques Fuentes</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP on Rails]]></category>

		<guid isPermaLink="false">http://www.derivante.com/?p=128</guid>
		<description><![CDATA[Adding conventions to DRY our code This article will provide a few snippets of code that I have recently plugged into the custom PHP framework that I use. However, those of you whom use and are more familiar with popular frameworks may be able to use this as well. I decided a few days ago [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-425" style="margin: 15px;" title="php-med-trans-light" src="http://www.derivante.com/wp-content/uploads/2009/05/php-med-trans-light.gif" alt="php-med-trans-light" width="95" height="51" /></p>
<h3>Adding conventions to DRY our code</h3>
<p>This article will provide a few snippets of code that I have recently plugged into the custom PHP framework that I use. However, those of you whom use and are more familiar with popular frameworks may be able to use this as well. I decided a few days ago to alter this custom framework in the following ways:</p>
<ul>
<li>an "application controller" which has access to the base and current page controller</li>
<li>filter capabilities in page controllers</li>
</ul>
<p>First, I will outline some basics about my framework so that you can understand my implementations.</p>
<p>The custom framework that I use is <strong>very</strong> similar to rails' conventions. It is an MVC framework and has almost a 1:1 folder/file structure to rails. One glaring difference is that it uses camelCase throughout the framework in opposition to the rails convetion. Anyway, let me start with the app folder (since this is the center of our attention).</p>
<p><img class="alignnone size-medium wp-image-56" title="app_layout" src="http://www.derivante.com/wp-content/uploads/2008/12/app_layout-211x300.png" alt="" width="211" height="300" /></p>
<p>You can see above that my app folder is almost exactly like rails except for the camelCase and the fact that my views have .tpls (go smarty!). You'll also notice that I have 3 page controllers plus the application controller. We are going to start with how I have implemented the application controller.</p>
<h3>The Application Controller</h3>
<p>You should already know that my page controllers will extend the "base" controller (which is simply Controller for me). Thus, my page controllers have access to all of the essentials such as redirections, session access, IoC, flash messaging, etc. So, we want to make another controller which will allow those 3 page controllers (and any future page controller) to access shared functions so that we can maintain DRY code. Keep in mind that this application controller should also have access to the parent controller.</p>
<p><strong>The problem:</strong> How do we easily give our application controller shared communication between the parent controllers and child controller, but also disallowing it from being a page controller itself?</p>
<p>First let's take a look at my page controller:</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> HomeController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">requireUser</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p>You may have guessed that this "action" will respond to http://somewhere/home/index/ and render app/view/home/index.tpl (and you are correct!). Inside the function we are trying to access the application controller's "requireUser" method. Here's what the application controller looks like:</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> ApplicationController <span style="color: #000000; font-weight: bold;">extends</span> AbstractApplicationController
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> requireUser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">session</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;hasRole<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'User'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&amp;gt;flash<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&amp;gt;error = <span style="color: #ff0000;">'You must be logged in to do that.'</span>;
	<span style="color: #0000ff;">$this</span>-&amp;gt;redirect<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'controller'</span> =&amp;gt; <span style="color: #ff0000;">'home'</span>, <span style="color: #ff0000;">'action'</span> =&amp;gt; <span style="color: #ff0000;">'index'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p>As you can see my application controller doesn't extend "Controller", but I'm still calling upon its methods. By having it extend from another class I get the easy benefit of not allowing the controller to be a "page" controller which means I cannot place action functions inside of it and render pages. Instead, it is simply there to DRY up some code and allow my other page controllers to access its methods. So how do we accomplish this and what does AbstractApplicationController look like? We'll get to that in a second. First, let me show you the two parts of the parent controller which we will need. Basically, directly before our method actionIndex in HomeController is invoked, we will create a new ApplicationController object in our parent controller so we can start communication.</p>
<p></code> </p>
<p><code></p>
<pre class="php"><span style="color: #0000ff;">$this</span>-&amp;gt;applicationController = <span style="color: #000000; font-weight: bold;">new</span> ApplicationController<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p> </p>
<p>We pass $this to its constructor because the AbstractionApplicationController is basically a placeholder (or proxy) for the controller object.</p>
<p></code> </p>
<p><code></p>
<pre class="php">&lt;!--p
abstract <span style="color: #000000; font-weight: bold;">class</span> AbstractApplicationController
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$controller</span>;
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _construct<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #808080; font-style: italic;">#038;$controller)</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/is_subclass_of"><span style="color: #000066;">is_subclass_of</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span>, <span style="color: #ff0000;">'Controller'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">controller</span> =&amp;amp; <span style="color: #0000ff;">$controller</span>;
      <span style="color: #b1b100;">else</span>
         throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/get_class"><span style="color: #000066;">get_class</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span><span style="color: #66cc66;">&#41;</span> .<span style="color: #ff0000;">' must be a subclass of Controller'</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getController<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&amp;gt;controller;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$meth</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">return</span> <a style="text-decoration: none;" href="http://www.php.net/call_user_func_array"><span style="color: #000066;">call_user_func_array</span></a><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&amp;gt;controller, <span style="color: #0000ff;">$meth</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p><span style="font-size: 15px; font-weight: bold; color: #570E00; font-family:Georgia,">The beauty is in the magic method __call()</span><br />
As you can see any method not found in your ApplicationController will find the magic __call() method and attempt to execute the method on the cached controller. This means when we redirect or add messages to the flash, we can use the same syntax as we would in our page controllers. This gives us one-way communication. What about accessing the "requireUser" method from the page controller? Again, there is beauty in __call(). We place the same thing in the base controller so that any method invoked from our page controller that isn't part of the base/page controller should be redirected to the ApplicationController object. Let's take a look.</p>
<p></code> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$meth</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">applicationController</span>-&amp;gt;<span style="color: #0000ff;">$meth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
?&amp;gt;</pre>
<p> </p>
<p>This allows us to call $this-&gt;requireUser() in our HomeController' actionIndex() method and it will invoke through base controller's __call() on the ApplicationController object. Now we have two-way communication between our page controllers and our application controller. Keep in mind that since the ApplicationController is not a child of Controller, we can only call upon public methods inside Controller and our page controllers.</p>
<p></code></p>
<h3>The Filter</h3>
<p>Rails has a great option for allowing the developer to invoke certain methods before or after the current page's action is invoked. I wanted this bonus also.</p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #000000; font-weight: bold;">class</span> GamesController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #66cc66;">&#123;</span>
   protected <span style="color: #0000ff;">$BEFORE_FILTER</span> = <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
     <span style="color: #ff0000;">'requireUser'</span>--&gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'except'</span> =&amp;gt; <a style="text-decoration: none;" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'myExceptionAction'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#41;</span>;	
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//before filter method will be invoked before this method is invoked</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> actionMyExceptionAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//before filter method will not be invoked for this</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
?&amp;gt;</pre>
<p> </p>
<p>If you aren't familiar with rails, don't fret, the logic is simple. I'm using the protected $BEFORE_FILTER to tell my parent controller to execute the requireUser() method before it executes any of the action methods <strong>except</strong> actionMyExceptionAction(). You can change the "except" key to "only" to change from a black-list to a white-list. Where is the requireUser() method? Well, If you haven't fallen asleep yet, you should know that requireUser() resides in our ApplicationController. How does this code work?</p>
<p></code><span style="font-size: 15px; font-weight: bold; color: #570E00; font-family:Georgia,">The beauty is in PHP's reflection class.</span></p>
<p> </p>
<p><code></p>
<pre class="php">&lt;!--p
<span style="color: #808080; font-style: italic;">//instaniate our ApplicationController before</span>
<span style="color: #808080; font-style: italic;">//the current page's action is invoked</span>
<span style="color: #0000ff;">$thi</span>--&gt;<span style="color: #006600;">applicationController</span> = <span style="color: #000000; font-weight: bold;">new</span> ApplicationController<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//run before filter</span>
<span style="color: #0000ff;">$this</span>-&amp;gt;filter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'before'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temporality</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$filter</span> = <a style="text-decoration: none;" href="http://www.php.net/strtoupper"><span style="color: #000066;">strtoupper</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temporality</span>.<span style="color: #ff0000;">'_FILTER'</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$controllerName</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;getControllerName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'Controller'</span>;
  <span style="color: #0000ff;">$controller</span> = <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controllerName</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$controller</span>-&amp;gt;getProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$property</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$property</span>-&amp;gt;name === <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0000ff;">$filter</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #66cc66;">&#123;</span><span style="color: #0000ff;">$property</span>-&amp;gt;name<span style="color: #66cc66;">&#125;</span>;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
 	<a style="text-decoration: none;" href="http://www.php.net/settype"><span style="color: #000066;">settype</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span>, <span style="color: #ff0000;">'array'</span><span style="color: #66cc66;">&#41;</span>;
 	<span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
 	<a style="text-decoration: none;" href="http://www.php.net/unset"><span style="color: #000066;">unset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filter</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$method</span> =&amp;gt; <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
 	<span style="color: #0000ff;">$action</span> = <span style="color: #0000ff;">$this</span>-&amp;gt;currentAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
 	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#41;</span> || <span style="color: #66cc66;">&#40;</span>!<a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; !<a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; <a style="text-decoration: none;" href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$action</span>, <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'only'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
          &amp;amp;&amp;amp; !<a style="text-decoration: none;" href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$action</span>, <span style="color: #0000ff;">$options</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'except'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #0000ff;">$this</span>-&amp;gt;<span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
?&amp;gt;</pre>
<p> </p>
<p>Basically, we use reflection on our GamesController and then find any properties that match our filter name (BEFORE_FILTER). We make sure to turn it into an array if it isn't (which means we can do BEFORE_FILTER = 'requireUser';), and then invoke the method based on whether or not it fits the description. If "only" and "except" do not exist then it should be invoked. Otherwise, it should not be invoked if the current page's action is not in the "only" array or if it is in the "except" array. As you can see, you simply need to add protected $AFTER_FILTER = 'someMethodHere'; to have an after filter invoke some method. There you have it!</p>
<p></code>This concludes our crazy talk of making your framework more like rails (or at least adding bits and pieces to make your life easier).</p>
<p> </p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.derivante.com/2008/12/08/php-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->