<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: A lightweight and flexible front controller for PHP 5</title>
	<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5</link>
	<description>Chris Corbyn, WebGeek</description>
	<pubDate>Thu, 28 Aug 2008 23:58:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2</generator>

	<item>
		<title>By: WishCow</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-9954</link>
		<author>WishCow</author>
		<pubDate>Mon, 31 Mar 2008 15:00:01 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-9954</guid>
		<description>Great article!
Small note, instead of
&lt;code&gt;
foreach ($this-&#62;viewData as $key =&#62; $value) {
 $$key = $value;
}
&lt;/code&gt;

You can do:
&lt;code&gt;
extract($this-&#62;viewData);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Great article!<br />
Small note, instead of</p>
<div class="codesnip-container" >foreach ($this-&gt;viewData as $key =&gt; $value) {<br />
 $$key = $value;<br />
}</div>
<p>You can do:</p>
<div class="codesnip-container" >extract($this-&gt;viewData);</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anjanesh</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-9754</link>
		<author>Anjanesh</author>
		<pubDate>Mon, 24 Mar 2008 15:51:56 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-9754</guid>
		<description>Im assuming the reason you're having
&lt;b&gt;protected $name&lt;/b&gt;; under &lt;b&gt;abstract class ActionController&lt;/b&gt; is because __CLASS__ will return &lt;b&gt;ActionController&lt;/b&gt; instead of &lt;b&gt;GuestbookActions&lt;/b&gt; ?
Well, since &lt;a href="http://www.php.net/manual/en/language.oop5.late-static-bindings.php" rel="nofollow"&gt;PHP 5.3&lt;/a&gt; we should be able to get &lt;b&gt;GuestbookActions&lt;/b&gt;.</description>
		<content:encoded><![CDATA[<p>Im assuming the reason you&#8217;re having<br />
<b>protected $name</b>; under <b>abstract class ActionController</b> is because __CLASS__ will return <b>ActionController</b> instead of <b>GuestbookActions</b> ?<br />
Well, since <a href="http://www.php.net/manual/en/language.oop5.late-static-bindings.php" rel="nofollow">PHP 5.3</a> we should be able to get <b>GuestbookActions</b>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan Palencar</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-5319</link>
		<author>Jan Palencar</author>
		<pubDate>Sat, 02 Feb 2008 15:06:58 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-5319</guid>
		<description>Hi Chris,
i'm really happy that i've run across this site. 
I read an article in some PHP documentation, that's were i got you web from. 
Thanks for your work, 
nice that you are sharing with us.

i can say it's a high quality code, since i coded a year or two (pro) in Java.
can see nice OO techniques.

see ya.</description>
		<content:encoded><![CDATA[<p>Hi Chris,<br />
i&#8217;m really happy that i&#8217;ve run across this site.<br />
I read an article in some PHP documentation, that&#8217;s were i got you web from.<br />
Thanks for your work,<br />
nice that you are sharing with us.</p>
<p>i can say it&#8217;s a high quality code, since i coded a year or two (pro) in Java.<br />
can see nice OO techniques.</p>
<p>see ya.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-4999</link>
		<author>Daniel</author>
		<pubDate>Tue, 22 Jan 2008 19:57:22 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-4999</guid>
		<description>Can you please add a tutoral on your composite view pattern?</description>
		<content:encoded><![CDATA[<p>Can you please add a tutoral on your composite view pattern?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Risto</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-4103</link>
		<author>Risto</author>
		<pubDate>Mon, 24 Dec 2007 10:37:21 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-4103</guid>
		<description>Best article ever made of controllers! Is this code free to use and modify?</description>
		<content:encoded><![CDATA[<p>Best article ever made of controllers! Is this code free to use and modify?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chris</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3425</link>
		<author>chris</author>
		<pubDate>Sat, 01 Dec 2007 22:40:34 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3425</guid>
		<description>I'll write a blog post on composite views.  It's a wildly varying topic which ranges from composing each view manually (with include() for example), to creating a "layout" template and associating a page template with each controller, to going all out and providing a sub-controller/slot style view (generally a bad idea).

I'm pretty busy right now with loads of other stuff but it's on my todo list ;)</description>
		<content:encoded><![CDATA[<p>I&#8217;ll write a blog post on composite views.  It&#8217;s a wildly varying topic which ranges from composing each view manually (with include() for example), to creating a &#8220;layout&#8221; template and associating a page template with each controller, to going all out and providing a sub-controller/slot style view (generally a bad idea).</p>
<p>I&#8217;m pretty busy right now with loads of other stuff but it&#8217;s on my todo list ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3320</link>
		<author>Daniel</author>
		<pubDate>Wed, 28 Nov 2007 20:16:24 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3320</guid>
		<description>Hi,

Can you please email me some eample code of how you setup you composite views?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Can you please email me some eample code of how you setup you composite views?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3172</link>
		<author>Daniel</author>
		<pubDate>Fri, 23 Nov 2007 21:45:30 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-3172</guid>
		<description>This is my front controller:

Every action gets dispatch here and not in page controllers.

I will post my composite page controller aswell if you like. Although I would really like to see how you do it.

&lt;code&gt;
directory = $directory;
	}
	
	/**
     * 
     * Sets the controller default action.
     * 
     * @param string $class The class to call.
	 * @param string $method The method to dispatch.
	 *
	 * @return void
     * 
     */	
	function setDefault($class, $method) {
		$this-&#62;default = $this-&#62;forward($class, $method);
	}

	/**
     * 
     * Sets the controller error action.
     * 
     * @param string $class The class to call.
	 * @param string $method The method to dispatch.
	 *
	 * @return void
     * 
     */	
	function setError($class, $method) {
		$this-&#62;error = $this-&#62;forward($class, $method);
	}

	/**
     * 
     * Sets the controller error controller action.
     * 
     * @param string $class The class to call.
	 * @param string $method The method to dispatch.
	 *
	 * @return void
     * 
     */		
	function addPreAction($class, $method) {
		$this-&#62;pre_action[] = $this-&#62;forward($class, $method);
	}

	/**
     * 
     * Dispatches the pre actions and page controller class.
     * 
     * @param object $request The request object.
	 *
     * @return void
     * 
     */	
  	function dispatch(&#38;$request) {
		$action = $this-&#62;requestHandler($request);
		
		while ($action) {
			foreach ($this-&#62;pre_action as $pre_action) {
				$result = $this-&#62;execute($pre_action);
						
				if ($result) {
					$action = $result;
						 
					break;
				}
			}
			
			$action = $this-&#62;execute($action);
		}
  	}
    
	/**
     * 
     * Executes a controller class.
     * 
     * @param array $action The action to be executed.
	 *
     * @return array $action Returns another action so the controller can be forwarded. If there is no foward action NULL is returned.
     * 
     */	
	function execute($action) {
		$file = $this-&#62;directory . basename($action['class']) . '.php';

		if (file_exists($file)) {
			include_once($file);
			
			 $controller = 'Controller' . preg_replace('/[^a-zZ-Z0-9]/', NULL, $action['class']);
			
			 $class = new $controller($this-&#62;registry);

			if (method_exists($class, $action['method'])) {
				$action = $class-&#62;{$action['method']}($this-&#62;registry);
			} else {
				$action = $this-&#62;error;
				
				$this-&#62;error = NULL;
			}
		} else {
			$action = $this-&#62;error;
			
			$this-&#62;error = NULL;
		}
		
		return $action;
	}

	/**
     * 
     * Handles the request and creates an action ready for dispatch.
     * 
     * @param object $request The request object to is used to build the action to be executed.
	 *
     * @return array $action Returns an action so the controller can be forwarded.
     * 
     */		
	function requestHandler(&#38;$request) {
	    if ($request-&#62;has('controller')) {
			$class = $request-&#62;get('controller');
			
			if ($request-&#62;has('action')) {
				$method = $request-&#62;get('action') . 'Action';
			} else {
				$method = 'defaultAction';
			}

			return $this-&#62;forward($class, $method);
	    } else {
	        return $this-&#62;default;
	    }
	}		
}
?&#62;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>This is my front controller:</p>
<p>Every action gets dispatch here and not in page controllers.</p>
<p>I will post my composite page controller aswell if you like. Although I would really like to see how you do it.</p>
<div class="codesnip-container" >directory = $directory;<br />
	}</p>
<p>	/**<br />
     *<br />
     * Sets the controller default action.<br />
     *<br />
     * @param string $class The class to call.<br />
	 * @param string $method The method to dispatch.<br />
	 *<br />
	 * @return void<br />
     *<br />
     */<br />
	function setDefault($class, $method) {<br />
		$this-&gt;default = $this-&gt;forward($class, $method);<br />
	}</p>
<p>	/**<br />
     *<br />
     * Sets the controller error action.<br />
     *<br />
     * @param string $class The class to call.<br />
	 * @param string $method The method to dispatch.<br />
	 *<br />
	 * @return void<br />
     *<br />
     */<br />
	function setError($class, $method) {<br />
		$this-&gt;error = $this-&gt;forward($class, $method);<br />
	}</p>
<p>	/**<br />
     *<br />
     * Sets the controller error controller action.<br />
     *<br />
     * @param string $class The class to call.<br />
	 * @param string $method The method to dispatch.<br />
	 *<br />
	 * @return void<br />
     *<br />
     */<br />
	function addPreAction($class, $method) {<br />
		$this-&gt;pre_action[] = $this-&gt;forward($class, $method);<br />
	}</p>
<p>	/**<br />
     *<br />
     * Dispatches the pre actions and page controller class.<br />
     *<br />
     * @param object $request The request object.<br />
	 *<br />
     * @return void<br />
     *<br />
     */<br />
  	function dispatch(&amp;$request) {<br />
		$action = $this-&gt;requestHandler($request);</p>
<p>		while ($action) {<br />
			foreach ($this-&gt;pre_action as $pre_action) {<br />
				$result = $this-&gt;execute($pre_action);</p>
<p>				if ($result) {<br />
					$action = $result;</p>
<p>					break;<br />
				}<br />
			}</p>
<p>			$action = $this-&gt;execute($action);<br />
		}<br />
  	}</p>
<p>	/**<br />
     *<br />
     * Executes a controller class.<br />
     *<br />
     * @param array $action The action to be executed.<br />
	 *<br />
     * @return array $action Returns another action so the controller can be forwarded. If there is no foward action NULL is returned.<br />
     *<br />
     */<br />
	function execute($action) {<br />
		$file = $this-&gt;directory . basename($action[&#8217;class&#8217;]) . &#8216;.php&#8217;;</p>
<p>		if (file_exists($file)) {<br />
			include_once($file);</p>
<p>			 $controller = &#8216;Controller&#8217; . preg_replace(&#8217;/[^a-zZ-Z0-9]/&#8217;, NULL, $action[&#8217;class&#8217;]);</p>
<p>			 $class = new $controller($this-&gt;registry);</p>
<p>			if (method_exists($class, $action[&#8217;method&#8217;])) {<br />
				$action = $class-&gt;{$action[&#8217;method&#8217;]}($this-&gt;registry);<br />
			} else {<br />
				$action = $this-&gt;error;</p>
<p>				$this-&gt;error = NULL;<br />
			}<br />
		} else {<br />
			$action = $this-&gt;error;</p>
<p>			$this-&gt;error = NULL;<br />
		}</p>
<p>		return $action;<br />
	}</p>
<p>	/**<br />
     *<br />
     * Handles the request and creates an action ready for dispatch.<br />
     *<br />
     * @param object $request The request object to is used to build the action to be executed.<br />
	 *<br />
     * @return array $action Returns an action so the controller can be forwarded.<br />
     *<br />
     */<br />
	function requestHandler(&amp;$request) {<br />
	    if ($request-&gt;has(&#8217;controller&#8217;)) {<br />
			$class = $request-&gt;get(&#8217;controller&#8217;);</p>
<p>			if ($request-&gt;has(&#8217;action&#8217;)) {<br />
				$method = $request-&gt;get(&#8217;action&#8217;) . &#8216;Action&#8217;;<br />
			} else {<br />
				$method = &#8216;defaultAction&#8217;;<br />
			}</p>
<p>			return $this-&gt;forward($class, $method);<br />
	    } else {<br />
	        return $this-&gt;default;<br />
	    }<br />
	}<br />
}<br />
?&gt;</p></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: d11wtq</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-1908</link>
		<author>d11wtq</author>
		<pubDate>Wed, 10 Oct 2007 09:30:50 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-1908</guid>
		<description>Hi guys, sorry for the delay in responding.  I moved out to Australia a week and a half ago and am now a SitePoint employee.

With respect to handling POST (and GET for that matter if we're being pedantic ;)) you really want to create a Request class.  Pretty simple really:

$x = $req-&gt;getParameter("something");
$y = $req-&gt;getParameter("thing", Request::POST);
$req-&gt;hasParameter("foo");
$req-&gt;setError("some_field", "Field not filled in");
$req-&gt;getRequestURI();

etc etc etc...

Take a look at Java's HttpServletRequest class for inspiration, or symfony's sfRequest class.

With respect to the composite design pattern I'm assuming you're referring more specifically to a composite view?  That's a View layer topic and something which warrants a completely separate article.  I guess I could write an article on the basics of the composite view when I get some time :)

Thanks for the feedback guys.</description>
		<content:encoded><![CDATA[<p>Hi guys, sorry for the delay in responding.  I moved out to Australia a week and a half ago and am now a SitePoint employee.</p>
<p>With respect to handling POST (and GET for that matter if we&#8217;re being pedantic ;)) you really want to create a Request class.  Pretty simple really:</p>
<p>$x = $req->getParameter(&#8221;something&#8221;);<br />
$y = $req->getParameter(&#8221;thing&#8221;, Request::POST);<br />
$req->hasParameter(&#8221;foo&#8221;);<br />
$req->setError(&#8221;some_field&#8221;, &#8220;Field not filled in&#8221;);<br />
$req->getRequestURI();</p>
<p>etc etc etc&#8230;</p>
<p>Take a look at Java&#8217;s HttpServletRequest class for inspiration, or symfony&#8217;s sfRequest class.</p>
<p>With respect to the composite design pattern I&#8217;m assuming you&#8217;re referring more specifically to a composite view?  That&#8217;s a View layer topic and something which warrants a completely separate article.  I guess I could write an article on the basics of the composite view when I get some time :)</p>
<p>Thanks for the feedback guys.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-1660</link>
		<author>Daniel</author>
		<pubDate>Tue, 02 Oct 2007 18:40:44 +0000</pubDate>
		<guid>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comment-1660</guid>
		<description>Great tutorial!

is it possible you could extened it to include the composite pattern?</description>
		<content:encoded><![CDATA[<p>Great tutorial!</p>
<p>is it possible you could extened it to include the composite pattern?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
