<?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/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>W3 Style</title>
	<link>http://www.w3style.co.uk</link>
	<description>Chris Corbyn, WebGeek</description>
	<pubDate>Fri, 27 Jun 2008 13:34:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2</generator>
	<language>en</language>
			<item>
		<title>Compiling Objective-C without XCode in OS X</title>
		<link>http://www.w3style.co.uk/compiling-objective-c-without-xcode-in-os-x</link>
		<comments>http://www.w3style.co.uk/compiling-objective-c-without-xcode-in-os-x#comments</comments>
		<pubDate>Fri, 27 Jun 2008 08:33:09 +0000</pubDate>
		<dc:creator>d11wtq</dc:creator>
		
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/compiling-objective-c-without-xcode-in-os-x</guid>
		<description><![CDATA[Ok, so I&#8217;m about to blog about something which may seem blindingly obvious to OS X developers, but to be fair I didn&#8217;t find much on Google myself.
I&#8217;m new to Objective-C and programming for OS X in general, but I get frustrated when all articles, books and tutorials narrow themselves down to just one IDE [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so I&#8217;m about to blog about something which may seem blindingly obvious to OS X developers, but to be fair I didn&#8217;t find much on Google myself.</p>
<p>I&#8217;m new to Objective-C and programming for OS X in general, but I get frustrated when all articles, books and tutorials narrow themselves down to just one IDE and push you away from the glory of the command line.  So how do you compile a simple &#8220;Hello World!&#8221; application in objective-C on a Mac?</p>
<p>This is a brief &#8220;Hello World!&#8221; tutorial for ObjC, on top of demonstrating the build process beyond XCode.</p>
<p>We&#8217;ll create a class called &#8220;Talker&#8221; which simply says things we tell it to, then we&#8217;ll run it from our main() function.</p>
<h3>Create your directory structure</h3>
<p>Simple enough.  Make yourself a directory called &#8220;HelloWorld&#8221; or something like that, then create a sub-directory called &#8220;build&#8221;.</p>
<div class="codesnip-container" >mkdir HelloWorld<br />
mkdir HelloWorld/build</div>
<h3>Create the interface and implementation files</h3>
<p><strong>HelloWorld/Talker.h</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="co2">#import &lt;Foundation/Foundation.h&gt;</span></p>
<p><span class="kw4">@interface</span> Talker : <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSObject.html"><span class="kw5">NSObject</span></a></p>
<p>- <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> say: <span class="br0">&#40;</span>STR<span class="br0">&#41;</span> phrase;</p>
<p><span class="kw4">@end</span></div>
</div>
<p><strong>HelloWorld/Talker.m</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="co2">#import &quot;Talker.h&quot;</span></p>
<p><span class="kw4">@implementation</span> Talker</p>
<p>- <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> say: <span class="br0">&#40;</span>STR<span class="br0">&#41;</span> phrase <span class="br0">&#123;</span><br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;%s<span class="es0">\n</span>&quot;</span>, phrase<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">@end</span></div>
</div>
<h3>Create your main() file</h3>
<p><strong>HelloWorld/hello.m</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="co2">#import &quot;Talker.h&quot;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; Talker *talker = <span class="br0">&#91;</span><span class="br0">&#91;</span>Talker alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
&nbsp; <span class="br0">&#91;</span>talker say: <span class="st0">&quot;Hello World!&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; <span class="br0">&#91;</span>talker release<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<h3>Compile it with gcc specifying -framework Foundation</h3>
<div class="codesnip-container" >gcc -o build/hello Talker.m hello.m -framework Foundation</div>
<h3>Run it!</h3>
<div class="codesnip-container" >./build/hello</p>
<p>&#8230;</p>
<p>Hello World!</p></div>
<p>Enjoy :)</p>
<p><em>WordPress appears to have removed some formatting whitespace from my code, but it will still compile.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/compiling-objective-c-without-xcode-in-os-x/feed</wfw:commentRss>
		</item>
		<item>
		<title>Enumerations and jMock&#8230; ouch! But here&#8217;s how.</title>
		<link>http://www.w3style.co.uk/enumerations-and-jmock</link>
		<comments>http://www.w3style.co.uk/enumerations-and-jmock#comments</comments>
		<pubDate>Sat, 15 Sep 2007 11:47:35 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/enumerations-and-jmock</guid>
		<description><![CDATA[I may write an article on jMock eventually but right now I&#8217;m just dropping by to throw this home-brewed code onto the web to help out a few Java developers who have hit hurdles using jMock and returning Enumeration instances from a stub/mock method.
jMock provides only 3 different actions for returning values by default.  [...]]]></description>
			<content:encoded><![CDATA[<p>I may write an article on jMock eventually but right now I&#8217;m just dropping by to throw this home-brewed code onto the web to help out a few Java developers who have hit hurdles using jMock and returning Enumeration instances from a stub/mock method.</p>
<p>jMock provides only 3 different actions for returning values by default.  There&#8217;s ReturnvalueAction which returns a reference to the object you give it, ReturnDefaultValueAction which returns a basic default matching the required type and ReturnIteratorAction which returns re-usable Iterator instances.  ReturnIteratorAction was created to tackle a very similar problem.</p>
<p>When you use ReturnValueAction (or the returnValue() method of org.jmock.Expectations) you simply get a reference to your object.  This means that for Iterators and Enumerations, once they&#8217;ve been iterated over they&#8217;re completely exhausted, or if you partially iterate over one your remaining tests will provide unpredictable results.</p>
<p>The objective here is to provide a cloned Enumeration every time you invoke the mocked method.  Here&#8217;s how I did it:</p>
<p><em>(Apologies for the syntax highlighting. Not sure what GeSHi is doing with it).</em></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="coMULTI">/*<br />
&nbsp;Provides a way to return Enumeration objects from a Mock object.<br />
&nbsp;This gets around the problem with Enumerations being exhausted using<br />
&nbsp;org.jmock.lib.actions.ReturnValueAction.<br />
&nbsp;<br />
&nbsp;This program is free software: you can redistribute it and/or modify<br />
&nbsp;it under the terms of the GNU General Public License as published by<br />
&nbsp;the Free Software Foundation, either version 3 of the License, or<br />
&nbsp;(at your option) any later version.<br />
&nbsp;<br />
&nbsp;This program is distributed in the hope that it will be useful,<br />
&nbsp;but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
&nbsp;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.&nbsp; See the<br />
&nbsp;GNU General Public License for more details.</p>
<p>&nbsp;You should have received a copy of the GNU General Public License<br />
&nbsp;along with this program.&nbsp; If not, see &lt;http://www.gnu.org/licenses/&gt;.<br />
&nbsp;<br />
&nbsp;*/</span></p>
<p><span class="kw2">package</span> org.<span class="me1">mayo</span>.<span class="me1">jmock</span>;</p>
<p><span class="co2">import org.hamcrest.Description;</span><br />
<span class="co2">import org.jmock.api.Action;</span><br />
<span class="co2">import org.jmock.api.Invocation;</span><br />
<span class="co2">import java.util.Set;</span><br />
<span class="co2">import java.util.HashSet;</span><br />
<span class="co2">import java.util.Enumeration;</span><br />
<span class="co2">import java.util.Iterator;</span></p>
<p><span class="coMULTI">/**<br />
&nbsp;* Allows Enumeration to be returned from a Mock object with the jMock framework.<br />
&nbsp;* A new Enumeration will be returned for each invokation of the mock method.<br />
&nbsp;* @author Chris Corbyn &lt;chris@w3style.co.uk&gt;<br />
&nbsp;*/</span><br />
<span class="kw2">public</span> <span class="kw2">class</span> ReturnEnumerationAction&lt;E&gt; <span class="kw2">implements</span> <a href="http://www.google.com/search?q=allinurl%3AAction+java.sun.com&amp;bntl=1"><span class="kw3">Action</span></a> <span class="br0">&#123;</span><br />
&nbsp; <br />
&nbsp; <span class="coMULTI">/** The values from the Enumeration as a Set */</span><br />
&nbsp; <span class="kw2">private</span> Set&lt;E&gt; set;<br />
&nbsp; <br />
&nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp;* Constructs a new Action using the given Enumeration.<br />
&nbsp; &nbsp;* The Enumeration will be exhasuted initially, but invoking<br />
&nbsp; &nbsp;* ReturnEnumerationAction.invoke() will return a clone of it.<br />
&nbsp; &nbsp;* @param Enumeration&lt;E&gt; target<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span class="kw2">public</span> ReturnEnumerationAction<span class="br0">&#40;</span>Enumeration&lt;E&gt; target<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; set = <span class="kw2">new</span> HashSet&lt;E&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>target.<span class="me1">hasMoreElements</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; set.<span class="me1">add</span><span class="br0">&#40;</span>target.<span class="me1">nextElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <br />
&nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp;* Invoke this Action to get a new Enumeration like the original.<br />
&nbsp; &nbsp;* @param Invocation invocation<br />
&nbsp; &nbsp;* @return Enumeration&lt;E&gt;<br />
&nbsp; &nbsp;* @throws Throwable<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span class="kw2">public</span> Enumeration&lt;E&gt; invoke<span class="br0">&#40;</span>Invocation invocation<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?q=allinurl%3AThrowable+java.sun.com&amp;bntl=1"><span class="kw3">Throwable</span></a> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">final</span> Set&lt;E&gt; set = <span class="kw2">this</span>.<span class="me1">set</span>;<br />
&nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> Enumeration&lt;E&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Iterator&lt;E&gt; it = set.<span class="me1">iterator</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">boolean</span> hasMoreElements<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> it.<span class="me1">hasNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw2">public</span> E nextElement<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> it.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <br />
&nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp;* Describe this action.<br />
&nbsp; &nbsp;* This is not implemented, mostly because I&#8217;m too lazy to find out how.<br />
&nbsp; &nbsp;* @param Description description<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span class="kw2">public</span> <span class="kw4">void</span> describeTo<span class="br0">&#40;</span>Description description<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <br />
<span class="br0">&#125;</span></div>
</div>
<p>You use it like this:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">mockery.<span class="me1">checking</span><span class="br0">&#40;</span><span class="kw2">new</span> Expectations<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#123;</span><br />
&nbsp; one<span class="br0">&#40;</span>mockObject<span class="br0">&#41;</span>.<span class="me1">mockMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; will<span class="br0">&#40;</span><span class="kw2">new</span> ReturnEnumerationAction<span class="br0">&#40;</span>yourEnum<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</div>
<p>Hopefully this helps a few people :)  I had googled for a solution before writing this and didn&#8217;t see any code despite several queries about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/enumerations-and-jmock/feed</wfw:commentRss>
		</item>
		<item>
		<title>A simple Filter Chain example in PHP 5</title>
		<link>http://www.w3style.co.uk/a-simple-filter-chain-example-in-php-5</link>
		<comments>http://www.w3style.co.uk/a-simple-filter-chain-example-in-php-5#comments</comments>
		<pubDate>Sun, 19 Aug 2007 14:42:44 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/a-simple-filter-chain-example-in-php-5</guid>
		<description><![CDATA[Filter chains can be really useful when you want to pass an object (or several objects) through a series of tests in a scalable fashion.  An example may be performing some request validation/filtering before your application starts to do anything too specific with it.
The idea behind a filter chain is that you create a [...]]]></description>
			<content:encoded><![CDATA[<p>Filter chains can be really useful when you want to pass an object (or several objects) through a series of tests in a scalable fashion.  An example may be performing some request validation/filtering before your application starts to do anything too specific with it.</p>
<p>The idea behind a filter chain is that you create a container (the chain) which holds a series of strategies (filters). Each filter is called in turn and is passed a reference to the chain so that it can tell the chain to continue to the next filter.  Anything one filter does to the object(s) it is passed should be available in subsequent filters.</p>
<p>I&#8217;m going to demonstrate an example by passing a pseudo request and response object along a chain during or before the routing phase in a controller.</p>
<p><strong>test.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&quot;BASE_DIR&quot;</span>, <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/FilterChain.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Foo.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Bar.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Zip.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Request.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Response.php&quot;</span>;</p>
<p><span class="re0">$filterChain</span> = <span class="kw2">new</span> FilterChain<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Foo<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Bar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Zip<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$request</span> = <span class="kw2">new</span> Request<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$response</span> = <span class="kw2">new</span> Response<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;</div>
</div>
<p><strong>FilterChain.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> FilterChain <span class="br0">&#123;</span><br />
&nbsp; protected <span class="re0">$filters</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; public <span class="kw2">function</span> addFilter<span class="br0">&#40;</span>Filter <span class="re0">$filter</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">filters</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$filter</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$filter</span> = <a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">filters</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$filter</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span>; <span class="co1">//End of chain</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$filter</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span>, <span class="re0">$this</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Filter/Foo.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Filter_Foo implements Filter <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span>, FilterChain <span class="re0">$filterChain</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="kw2">__CLASS__</span> . <span class="st0">&quot; running&#8230;&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Filter/Bar.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Filter_Bar implements Filter <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span>, FilterChain <span class="re0">$filterChain</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="kw2">__CLASS__</span> . <span class="st0">&quot; running&#8230;&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Filter/Zip.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Filter_Zip implements Filter <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span>, FilterChain <span class="re0">$filterChain</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="kw2">__CLASS__</span> . <span class="st0">&quot; running&#8230;&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>The Request/Response classes I&#8217;ve used are only pseudo, but the code is available at the end of this article if you need to see them.</p>
<p>When we run that code we see this:</p>
<div class="codesnip-container" >chris-corbyns-computer:~/filter-chain d11wtq$ php-cli test.php<br />
Filter_Foo running&#8230;<br />
Filter_Bar running&#8230;<br />
Filter_Zip running&#8230;</div>
<p>Each filter simply asked the chain to keep going, which is merely a case of array_shift()&#8217;ing the next filter and running it.</p>
<p>Now lets take a scenario where one of our filters is looking to break the chain on a certain condition.</p>
<p><strong>Filter/Bar.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Filter_Bar implements Filter <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span>, FilterChain <span class="re0">$filterChain</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="kw2">__CLASS__</span> . <span class="st0">&quot; running&#8230;&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$request</span>-&gt;<span class="me1">getAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;module&quot;</span><span class="br0">&#41;</span> != <span class="st0">&quot;good&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;The requested module is no good. Breaking chain!&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Let&#8217;s test the theory this filter chain will go no further than the &#8220;Filter_Bar&#8221; stage if we set &#8220;module&#8221; to anything other than &#8220;good&#8221;.</p>
<p>Here&#8217;s the modified test.php.<br />
<strong>test.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&quot;BASE_DIR&quot;</span>, <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/FilterChain.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Foo.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Bar.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Filter/Zip.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Request.php&quot;</span>;<br />
<span class="kw1">require_once</span> BASE_DIR . <span class="st0">&quot;/Response.php&quot;</span>;</p>
<p><span class="re0">$filterChain</span> = <span class="kw2">new</span> FilterChain<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Foo<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Bar<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$filterChain</span>-&gt;<span class="me1">addFilter</span><span class="br0">&#40;</span><span class="kw2">new</span> Filter_Zip<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$request</span> = <span class="kw2">new</span> Request<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$request</span>-&gt;<span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;module&quot;</span>, <span class="st0">&quot;bad&quot;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$response</span> = <span class="kw2">new</span> Response<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;</div>
</div>
<p>Low and behold, the chain stops as we expect!</p>
<div class="codesnip-container" >chris-corbyns-computer:~/filter-chain d11wtq$ php-cli test.php<br />
Filter_Foo running&#8230;<br />
Filter_Bar running&#8230;<br />
The requested module is no good. Breaking chain!</div>
<p>Now let&#8217;s see the beauty of the fact this is a chain.  Maybe one of our other filters is there to try and correct problems with badly requested modules?  It&#8217;s not a realistic example, but here goes anyway.</p>
<p><strong>Filter/Foo.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Filter_Foo implements Filter <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doFilter<span class="br0">&#40;</span>Request <span class="re0">$request</span>, Response <span class="re0">$response</span>, FilterChain <span class="re0">$filterChain</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="kw2">__CLASS__</span> . <span class="st0">&quot; running&#8230;&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$request</span>-&gt;<span class="me1">getAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;module&quot;</span><span class="br0">&#41;</span> == <span class="st0">&quot;bad&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Module looks bad, but I can fix this&#8230;. &quot;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="re0">$request</span>-&gt;<span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;module&quot;</span>, <span class="st0">&quot;good&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Fixed!&quot;</span> . PHP_EOL;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$filterChain</span>-&gt;<span class="me1">doFilter</span><span class="br0">&#40;</span><span class="re0">$request</span>, <span class="re0">$response</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>And the result now:</p>
<div class="codesnip-container" >chris-corbyns-computer:~/filter-chain d11wtq$ php-cli test.php<br />
Filter_Foo running&#8230;<br />
Module looks bad, but I can fix this&#8230;. Fixed!<br />
Filter_Bar running&#8230;<br />
Filter_Zip running&#8230;</div>
<p>It&#8217;s a pretty simple pattern really, but one that comes in handy at times.  You&#8217;ll see this exact same pattern used in Java servlet containers before the requested servlet is actually dispatched :)</p>
<p>* Here&#8217;s the Request/Response classes for those who wanted to see them (pseudo):<br />
<strong>Request.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Request <span class="br0">&#123;</span><br />
&nbsp; protected <span class="re0">$attributes</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; public <span class="kw2">function</span> setAttribute<span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">attributes</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span> = <span class="re0">$value</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getAttribute<span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/array_key_exists"><span class="kw3">array_key_exists</span></a><span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$this</span>-&gt;<span class="me1">attributes</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">attributes</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Response.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> Response <span class="br0">&#123;</span><br />
<span class="br0">&#125;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/a-simple-filter-chain-example-in-php-5/feed</wfw:commentRss>
		</item>
		<item>
		<title>First signs of namespaces in PHP 6</title>
		<link>http://www.w3style.co.uk/first-signs-of-namespaces-in-php-6</link>
		<comments>http://www.w3style.co.uk/first-signs-of-namespaces-in-php-6#comments</comments>
		<pubDate>Fri, 17 Aug 2007 14:16:49 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/first-signs-of-namespaces-in-php-6</guid>
		<description><![CDATA[Every so often I grab the latest PHP 6 snapshot from snaps.php.net just to see what&#8217;s been going on in the development of it.  Today I downloaded a snapshot as usual and was pleased to see they&#8217;ve already started to include support for namespacing.
Unicode improvements aside, namespacing is something I&#8217;ve longed for in PHP [...]]]></description>
			<content:encoded><![CDATA[<p>Every so often I grab the latest PHP 6 snapshot from <a href="http://snaps.php.net/">snaps.php.net</a> just to see what&#8217;s been going on in the development of it.  Today I downloaded a snapshot as usual and was pleased to see they&#8217;ve already started to include support for namespacing.</p>
<p>Unicode improvements aside, namespacing is something I&#8217;ve longed for in PHP for a long time.  It&#8217;s not a rocket-science approach they&#8217;ve taken; effectively the implementation just prefixes the current namespace onto class and function names which are now permitted to contains the double-colon (::) syntax in their names.</p>
<p>You can achieve an almost Java-like package handling system by using __autoload() (or the SPL alternative) though.</p>
<p>My experimentation thus far is this:</p>
<p><strong>autoload.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&quot;BASE_DIR&quot;</span>, <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw2">function</span> __autoload<span class="br0">&#40;</span><span class="re0">$class</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="re0">$file</span> = BASE_DIR . <span class="st0">&quot;/&quot;</span> .<a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;::&quot;</span>, <span class="st0">&quot;/&quot;</span>, <span class="re0">$class</span><span class="br0">&#41;</span> . <span class="st0">&quot;.php&quot;</span>;<br />
&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">require_once</span> <span class="re0">$file</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Foo/Thing/Example.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>namespace Foo::<span class="me2">Thing</span>;</p>
<p><span class="kw2">class</span> Example <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Foo::Thing::Example instantiated&#8230;<span class="es0">\n</span>&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Foo/Thing/AnotherExample.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>namespace Foo::<span class="me2">Thing</span>;</p>
<p><span class="kw2">class</span> AnotherExample extends Example <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Foo::Thing::AnotherExample instantiated&#8230;<span class="es0">\n</span>&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Bar/Example.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>namespace Bar;</p>
<p><span class="kw2">class</span> Example <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Bar::Example instantiated&#8230;<span class="es0">\n</span>&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>test.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw1">require_once</span> <span class="st0">&quot;autoload.php&quot;</span>;</p>
<p>import Foo::<span class="me2">Thing</span>::<span class="me2">Example</span>;<br />
import Foo::<span class="me2">Thing</span>::<span class="me2">AnotherExample</span>;</p>
<p><span class="re0">$a</span> = <span class="kw2">new</span> Example<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$b</span> = <span class="kw2">new</span> Bar::<span class="me2">Example</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$c</span> = <span class="kw2">new</span> AnotherExample<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<p>And the output&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">d11wtq@pc-cac:~/php6$ php <span class="kw3">test</span>.php <br />
Foo::Thing::Example instantiated&#8230;<br />
Bar::Example instantiated&#8230;<br />
Foo::Thing::Example instantiated&#8230;<br />
Foo::Thing::AnotherExample instantiated&#8230;<br />
d11wtq@pc-cac:~/php6$</div>
</div>
<p>It&#8217;s certainly a step in the right direction, but (and let&#8217;s not be too cruel &#8212; it only appeared in the past few weeks!) some quirks I have noticed.</p>
<ol>
<li>You cannot import both Foo::Thing::Example and Bar::Example inside the same file.  You need to use the full name for at least one of them.</li>
<li>No warnings are generated if you try to use something which doesn&#8217;t exist (No::Such::Namespace)</li>
<li>It&#8217;s not possible to import everything by wildcard (import Foo::Thing::*;) but I can live with that since it usually leads to poor programming</li>
</ol>
<p>I&#8217;m looking forward to seeing how this pans out :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/first-signs-of-namespaces-in-php-6/feed</wfw:commentRss>
		</item>
		<item>
		<title>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>
		<comments>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5#comments</comments>
		<pubDate>Sun, 12 Aug 2007 13:13:28 +0000</pubDate>
		<dc:creator>d11wtq</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5</guid>
		<description><![CDATA[Apparently the front controller is a commonly misunderstood design pattern so I figured I&#8217;d throw out some thoughts to the masses.
To put it simply, a Front Controller is a gateway to an application.  All requests hit the front controller leaving it to decide what to do next.  Typically a front controller will set [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently the front controller is a commonly misunderstood design pattern so I figured I&#8217;d throw out some thoughts to the masses.</p>
<p>To put it simply, a Front Controller is a gateway to an application.  All requests hit the front controller leaving it to decide what to do next.  Typically a front controller will set up the session, connect to the database and delegate requests to numerous page controllers (or action controllers).  Everything that happens here is program flow logic.</p>
<p>You could create a really basic front controller by putting a simply switch() statement inside an index.php file:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><a href="http://www.php.net/session_start"><span class="kw3">session_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$page</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;home&quot;</span>;<br />
<span class="kw1">switch</span> <span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">case</span> <span class="st0">&quot;home&quot;</span>:<br />
&nbsp; &nbsp; <span class="kw1">include</span> <span class="st0">&quot;pages/home.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; <span class="kw1">case</span> <span class="st0">&quot;guestbook&quot;</span>:<br />
&nbsp; &nbsp; <span class="kw1">include</span> <span class="st0">&quot;pages/guestbook.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">break</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Ths sort of procedural programming works, but I&#8217;ll try to convince you of the benefit of creating even a simple website in an object oriented fashion.  The code we&#8217;ll produce to power an extremely scalable and flexible controller architecture should take no more than a couple of hundred lines.  Of course, once you have the groundwork down it&#8217;s up to you how much fluff you begin to add to make it even more magical ;)</p>
<h2>The object oriented approach</h2>
<p>Typically a front controller will be a class with a known method name invoked to &#8220;launch&#8221; the application.  We&#8217;ll call this class &#8220;FrontController&#8221; (can&#8217;t think why!) and we&#8217;ll name it&#8217;s dispatcher method &#8220;dispatch()&#8221;.  Let&#8217;s start basic and add complexity as we progress.  Usually an application powered by the Front Controller pattern will appear to have virtually no logic in its index file.</p>
<h3>Some groundwork</h3>
<p><strong>index.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&quot;PAGE_DIR&quot;</span>, <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&quot;/pages&quot;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">require_once</span> <span class="st0">&quot;FrontController.php&quot;</span>;<br />
FrontController::<span class="me2">createInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">dispatch</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<p><strong>FrontController.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> FrontController <span class="br0">&#123;</span><br />
&nbsp; public <a href="http://www.php.net/static"><span class="kw3">static</span></a> <span class="kw2">function</span> createInstance<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st0">&quot;PAGE_DIR&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Critical error: Cannot proceed without PAGE_DIR.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$instance</span> = <span class="kw2">new</span> self<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$instance</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$page</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;home&quot;</span>;<br />
&nbsp; &nbsp; <span class="re0">$action</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;index&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. HomeActions</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span> . <span class="st0">&quot;Actions&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. pages/home/HomeActions.php</span><br />
&nbsp; &nbsp; <span class="re0">$file</span> = PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$page</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$class</span> . <span class="st0">&quot;.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">require_once</span> <span class="re0">$file</span>;<br />
&nbsp; &nbsp; <span class="re0">$actionMethod</span> = <span class="st0">&quot;do&quot;</span> . <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span> = <span class="kw2">new</span> <span class="re0">$class</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$controller</span>, <span class="re0">$actionMethod</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="co1">//e.g. $controller-&gt;doIndex();</span><br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="re0">$actionMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>pages/guestbook/GuestbookActions.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> GuestbookActions <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Index action called&#8230;&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doCreatePost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;CreatePost action called&#8230;&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now open up your browser and point it to http://localhost/index.php?page=guestbook&#038;action=index</p>
<p>You should see &#8220;Index action called&#8230;&#8221;.</p>
<p>Change the URL to http://localhost/index.php?page=guestboook&#038;action=createPost</p>
<p>Nice eh?</p>
<p>Ok, well we can do better than this. I mean, we don&#8217;t really want to be outputting stuff in our controller classes.  That sort of logic should be inside a template.  For our guestbook above we&#8217;ve got &#8220;index&#8221; and &#8220;createPost&#8221; actions.  Lets&#8217; make some templates associated with each of those actions.</p>
<h3>Introducing the view</h3>
<p>We&#8217;ll make some templates at pages/guestbook/indexView.php and pages/guestbook/createPostView.php.  This is painfully straightfoward really.</p>
<p><strong>pages/guestbook/indexView.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">Index action called&#8230;</div>
</div>
<p><strong>pages/guestbook/createPostView.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">CreatePost action called&#8230;</div>
</div>
<p>Now lets get these files included from our front controller.  For now I&#8217;m just going to modify the dispatch() method a little bit.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> FrontController <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// &#8230; snip &#8230; //</span><br />
&nbsp; public <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$page</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;home&quot;</span>;<br />
&nbsp; &nbsp; <span class="re0">$action</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;index&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. HomeActions</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span> . <span class="st0">&quot;Actions&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. pages/home/HomeActions.php</span><br />
&nbsp; &nbsp; <span class="re0">$file</span> = PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$page</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$class</span> . <span class="st0">&quot;.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">require_once</span> <span class="re0">$file</span>;<br />
&nbsp; &nbsp; <span class="re0">$actionMethod</span> = <span class="st0">&quot;do&quot;</span> . <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span> = <span class="kw2">new</span> <span class="re0">$class</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$controller</span>, <span class="re0">$actionMethod</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="co1">//e.g. $controller-&gt;doIndex();</span><br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="re0">$actionMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$view</span> = PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$page</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$action</span> . <span class="st0">&quot;View.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$view</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">include</span> <span class="re0">$view</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now lets just remove that yucky output from the action controller ;)</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> GuestbookActions <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doCreatePost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>You should basically have the same results as before when you accessed those earlier URLs in your browser, except now our presentation logic is in the right place.</p>
<p>As it stands all we&#8217;ve done is introduced some naming conventions and gotten our website to run a contoller method before it includes our usual file.  So how do we get some variables into the template?  I&#8217;m going to add some logic to the GuestbookActions class which I&#8217;d like to be available to other action controllers.  Time to refactor a little.</p>
<h3>Passing data to the view</h3>
<p>The first change I&#8217;d ike to make is to ensure all action controller classes can share some common features.  I&#8217;ll create an ActionController class and I&#8217;ll make it abstract because it should only extended, not instantiated.  I&#8217;ll move the responsibility for running it&#8217;s own action into this class too. This should clean up our front controller a little.</p>
<p><strong>ActionController.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>abstract <span class="kw2">class</span> ActionController <span class="br0">&#123;</span><br />
&nbsp; protected <span class="re0">$name</span>;<br />
&nbsp; protected <span class="re0">$viewData</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; public <span class="kw2">function</span> setName<span class="br0">&#40;</span><span class="re0">$name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">name</span> = <span class="re0">$name</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getName<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">name</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> setVar<span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">viewData</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span> = <span class="re0">$value</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getVar<span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/array_key_exists"><span class="kw3">array_key_exists</span></a><span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$this</span>-&gt;<span class="me1">viewData</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">viewData</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> dispatchAction<span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$actionMethod</span> = <span class="st0">&quot;do&quot;</span> . <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$this</span>, <span class="re0">$actionMethod</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="re0">$actionMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">displayView</span><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> displayView<span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span>PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$this</span>-&gt;<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$action</span> . <span class="st0">&quot;View.php&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="co1">//Create variables for the template</span><br />
&nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">viewData</span> <span class="kw1">as</span> <span class="re0">$key</span> =&gt; <span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="re0">$$key</span> = <span class="re0">$value</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">include</span> PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$this</span>-&gt;<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$action</span> . <span class="st0">&quot;View.php&quot;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now we can tidy up the dispatch() method in our front controller.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw1">require_once</span> <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&quot;/ActionController.php&quot;</span>;</p>
<p><span class="kw2">class</span> FrontController <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">//&#8230; snip &#8230;</span><br />
&nbsp; public <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$page</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;home&quot;</span>;<br />
&nbsp; &nbsp; <span class="re0">$action</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;index&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. HomeActions</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span> . <span class="st0">&quot;Actions&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. pages/home/HomeActions.php</span><br />
&nbsp; &nbsp; <span class="re0">$file</span> = PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$page</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$class</span> . <span class="st0">&quot;.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">require_once</span> <span class="re0">$file</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span> = <span class="kw2">new</span> <span class="re0">$class</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="me1">dispatchAction</span><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now if we make our GuestbookActions class extend the ActionController class we can get it to pass data into the template ;)</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> GuestbookActions extends ActionController <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">setVar</span><span class="br0">&#40;</span><span class="st0">&quot;hello&quot;</span>, <span class="st0">&quot;World!&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doCreatePost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">setVar</span><span class="br0">&#40;</span><span class="st0">&quot;hello&quot;</span>, <span class="st0">&quot;Universe!&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Cross your fingers and hope it works.</p>
<p><strong>pages/guestbook/indexView.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">Index action called. The controller said hello to <span class="kw2">&lt;?php</span> <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$hello</span> <span class="kw2">?&gt;</span></div>
</div>
<p><strong>pages/guestbook/createPostView.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">CreatePost action called. The controller said hello to <span class="kw2">&lt;?php</span> <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$hello</span> <span class="kw2">?&gt;</span></div>
</div>
<p>Did it work? :)</p>
<p>Ok, great, for me personally I&#8217;d get sick of typing setVar() and getVar() everytime I want to make something available to the view so I&#8217;m going to add in a controversial setter/getter system.  We&#8217;re using PHP 5 right?</p>
<p>The __set() and __get() methods are invoked whenever you try to access a property that doesn&#8217;t exist.</p>
<p><strong>ActionController.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>abstract <span class="kw2">class</span> ActionController <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// &#8230; snip &#8230;</span><br />
&nbsp; public <span class="kw2">function</span> __set<span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$value</span><span class="br0">&#41;</span>&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">setVar</span><span class="br0">&#40;</span><span class="re0">$key</span>, <span class="re0">$value</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> __get<span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">getVar</span><span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now in our GuestbookActions class we can simply pass data to the view by assigning it to non-existent properties.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">class</span> GuestbookActions extends ActionController <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> doIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">hello</span> = <span class="st0">&quot;World!&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doCreatePost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">hello</span> = <span class="st0">&quot;Universe!&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>This is great, it works nicely and we&#8217;ve got a clean separation of presentation logic and controller logic.  One thing that&#8217;s missing though is a common featureset available to <em>all</em> components which form part of the controller layer.  The controller layer will usually contain methods for getting a handle on the database, getting the request &#038; response objects and getting the session.  I&#8217;m not going to write all the code out for those components because I&#8217;m sure you can use your imagination so I&#8217;m just going to add some pseudo code at this point.</p>
<h3>All controllers belong to the controller layer</h3>
<p>Lets make an abstract class called quite simply &#8220;Controller&#8221;.  Here we can add anything which needs to be available everywhere in the controller layer.</p>
<p><strong>Controller.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>abstract <span class="kw2">class</span> Controller <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> getRequest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getResponse<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getSession<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//I tend to prefer $this-&gt;getRequest()-&gt;getSession(), but whatever!</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Just change FrontController and ActionController to extend this class and we&#8217;ve provided a complete new set of features everywhere in the controller layer.</p>
<p><strong>FrontController.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw1">require_once</span> <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&quot;/Controller.php&quot;</span>;<br />
<span class="kw1">require_once</span> <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&quot;/ActionController.php&quot;</span>;</p>
<p><span class="kw2">class</span> FrontController extends Controller <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// &#8230; snip &#8230;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>ActionController.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>abstract <span class="kw2">class</span> ActionController extends Controller <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// &#8230; snip &#8230;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>There&#8217;s just one last thing I&#8217;d like to to introduce which sets this sort of architecture apart from procedural programming.  Wouldn&#8217;t it be nice if t wasn&#8217;t only the front controller who could dispatch actions at runtime?  I mean, it would be pretty cool if we could forward our request from one action controller to another right?</p>
<h3>Enhancing program flow at the controller level</h3>
<p>Imagine in our guestbook we block users temporarily if they submit more than say 2 posts in a minute.  We could redirect away from our &#8220;createPost&#8221; action and send them to a &#8220;tryLater&#8221; action, but then the browser would have taken them away from the page.  No, we can infact make this far more streamlined and &#8220;forward()&#8221; from one action to another.</p>
<p>We just created our Controller class, so why not stick a forward() method in there and rather than have FrontController locate and dispatch the action, we&#8217;ll get it to forward() to it.  Effectively I&#8217;m just refactoring code here.</p>
<p><strong>Controller.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p>abstract <span class="kw2">class</span> Controller <span class="br0">&#123;</span><br />
&nbsp; public <span class="kw2">function</span> getRequest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getResponse<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> getSession<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> forward<span class="br0">&#40;</span><span class="re0">$page</span>, <span class="re0">$action</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//e.g. HomeActions</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span> . <span class="st0">&quot;Actions&quot;</span>;<br />
&nbsp; &nbsp; <span class="co1">//e.g. pages/home/HomeActions.php</span><br />
&nbsp; &nbsp; <span class="re0">$file</span> = PAGE_DIR . <span class="st0">&quot;/&quot;</span> . <span class="re0">$page</span> . <span class="st0">&quot;/&quot;</span> . <span class="re0">$class</span> . <span class="st0">&quot;.php&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/is_file"><span class="kw3">is_file</span></a><span class="br0">&#40;</span><span class="re0">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st0">&quot;Page not found&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">require_once</span> <span class="re0">$file</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span> = <span class="kw2">new</span> <span class="re0">$class</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$controller</span>-&gt;<span class="me1">dispatchAction</span><span class="br0">&#40;</span><span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>I&#8217;ve just moved the code and added a call to exit(), but now see what we can achieve.</p>
<p>Simplfy our Front Controller:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw1">require_once</span> <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> . <span class="st0">&quot;/ActionController.php&quot;</span>;</p>
<p><span class="kw2">class</span> FrontController <span class="br0">&#123;</span><br />
&nbsp; <span class="co1">//&#8230; snip &#8230;</span><br />
&nbsp; public <span class="kw2">function</span> dispatch<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$page</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;page&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;home&quot;</span>;<br />
&nbsp; &nbsp; <span class="re0">$action</span> = !<a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&quot;action&quot;</span><span class="br0">&#93;</span> : <span class="st0">&quot;index&quot;</span>;<br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">forward</span><span class="br0">&#40;</span><span class="re0">$page</span>, <span class="re0">$action</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>I&#8217;m not going to write the logic for tracking how many times the user has posted in the past minute I&#8217;ll just show that you can direct the flow of the application by invoking forward() from the action controller classes.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw2">&lt;?php</span></p>
<p><span class="kw2">class</span> GuestbookActions extends ActionController <span class="br0">&#123;</span><br />
&nbsp; protected <span class="re0">$maxPostsPerMin</span> = <span class="nu0">2</span>;<br />
&nbsp; public <span class="kw2">function</span> doIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">hello</span> = <span class="st0">&quot;World!&quot;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doCreatePost<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$posts_this_minute</span> = <span class="nu0">3</span>; <span class="co1">//yeah whatever</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$posts_this_minute</span> &gt; <span class="re0">$this</span>-&gt;<span class="me1">maxPostsPerMin</span><span class="br0">&#41;</span>&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">forward</span><span class="br0">&#40;</span><span class="st0">&quot;guestbook&quot;</span>, <span class="st0">&quot;tryLater&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">hello</span> = <span class="st0">&quot;Universe!&quot;</span>; <span class="co1">//Never executes this</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; public <span class="kw2">function</span> doTryLater<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">max</span> = <span class="re0">$this</span>-&gt;<span class="me1">maxPostsPerMin</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Add a new template:<br />
<strong>pages/guestbook/tryLaterView.php</strong></p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">Sorry you<span class="st0">&#8216;ve submitted too many posts in the last minute.<br />
Maximum number of posts per minute is &lt;?php echo $max ?&gt;. </span></div>
</div>
<p>Beautiful!  This sort of layout provides an immense amount of flexibility for creating highly scalable websites.  Of course, this is only the groundwork but it should be enough to give you an understanding of the front controller design pattern (and aspects of MVC).  Go away and play with the code. Starting by writing the code for getRequest() and getResponse() would be an idea ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5/feed</wfw:commentRss>
		</item>
		<item>
		<title>Virtual E-mail domains with Exim4 and Dovecot</title>
		<link>http://www.w3style.co.uk/virtual-domains-with-exim-and-dovecot</link>
		<comments>http://www.w3style.co.uk/virtual-domains-with-exim-and-dovecot#comments</comments>
		<pubDate>Mon, 28 May 2007 16:26:32 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[Linux &#038; Servers]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/virtual-e-mail-domains-with-exim4-and-dovecot</guid>
		<description><![CDATA[If, like myself you&#8217;ve got one server from which you want to run many different domains for web hosting and email, you may be realising just how tricky it can be to provide email for non-system users.  The typical SMTP/IMAP setup on a linux server is ignorant about what domain is being used when [...]]]></description>
			<content:encoded><![CDATA[<p>If, like myself you&#8217;ve got one server from which you want to run many different domains for web hosting and email, you may be realising just how tricky it can be to provide email for non-system users.  The typical SMTP/IMAP setup on a linux server is ignorant about what domain is being used when the request comes in.  Take the email address &#8220;joe@example.com&#8221;.  Traditionally this literally means &#8220;email for a user account &#8216;joe&#8217; on &#8216;example.com&#8217;&#8221;.  It makes the assumption that whichever server manages email for example.com has a user account called joe on it.  example.com does not traditionally care that it&#8217;s example.com; all it cares about is <em>who</em> the email is for.  Other servers use the example.com part purely to direct the email to the appropriate server.</p>
<p>This is perfectly sufficient if &#8220;joe&#8221; has a user account on that server (i.e. a record in /etc/shadow or /etc/passwd) and if that server does not belong to any domains other than &#8220;exmaple.com&#8221;.  However, when you&#8217;re running a single server with perhaps only one IP address mapping to several domains and you want to have a different set of email addresses for each domain this becomes a problem.  You may also want to have email addresses such as sales@yourdomain.com and support@yourdomain.com but you don&#8217;t need system accounts called &#8220;sales&#8221; and &#8220;support&#8221;.  In this case we need to start hacking at the mail server configuration in order to allow the delivery of the email.</p>
<p>I have no doubts whatsoever that <a href="http://www.exim.org/">Exim4</a> (Exim4 is <em>significantly</em> different from Exim3!) is the best MTA/MX software to use for things like this.  In fact, I would hands down say that you should use Exim for any MX server.  It&#8217;s advanced, and very complicated to get working I&#8217;ll give it that.  But with the complexity comes great flexibility.  You can practically program the entire configuration and can modify the behaviour almost without restriction.</p>
<p>Lets not forget however, that your IMAP or POP3 server also needs to be hackable if you really want this to work.  For years I used a <a href="http://vimap.sourceforge.net/">patched version</a> of <a href="http://www.washington.edu/imap/">UW-IMAP</a> which did the job nicely but there was no configuration avaliable for it and whilst easy to get running this way, it was too restrictive because it tied me down to fixed paths and directory layouts.  UW-IMAP also would not work with Maildir setups without <a href="http://www.davideous.com/imap-maildir/">even further patching</a>.  This article will focus on an IMAP/POP3 server called <a href="http://www.dovecot.org/">Dovecot</a>.  Dovecot has a very flexible configuration just like Exim, albeit much simpler to make sense of!  However, the principles of virtual email hosting apply whichever software you use.</p>
<p>It&#8217;s very unlikely that there will not already be packages for exim4 and dovecot provided by your linux distro.  If you can do so, install the packages as provided by your distro.  If your distro does not provide suitable packages you can get them here:</p>
<ul>
<li><strong>Exim4:</strong>
<ul>
<li>Website: <a href="http://www.exim.org/">http://www.exim.org/</a></li>
<li>Documentation: <a href="http://www.exim.org/docs.html">http://www.exim.org/docs.html</a></li>
</ul>
</li>
<li><strong>Dovecot:</strong>
<ul>
<li>Website: <a href="http://www.dovecot.org/">http://www.dovecot.org/</a></li>
<li>Documentation: <a href="http://wiki.dovecot.org/">http://wiki.dovecot.org/</a></li>
</ul>
</li>
</ul>
<h3>Who is that article targetted at?</h3>
<p>This article from hereon in will assume you have a solid grasp of linux server administration, but not necessarily a deep understanding of Exim or Dovecot.  If you&#8217;re not comfortable working on a linux server, editting configurtion files and making educated guesses then this article will likely be of no help to you.</p>
<p>Any Linux admin will know that this article is not going to give you copy and paste configuration directives (though if you&#8217;re lucky it might do) because linux package maintainers love to over-complicate things by adding their own sprinkle of salt to the configuration.  I set this up on a Debian (sarge, then subsequently etch) server, however I have set this up in almost the same manner on a Gentoo server and an ArchLinux server previously.  You may find that what&#8217;s written here does not directly apply to your situation but if you read the entire article rather than just the configuration excerpts you should be able to apply the principles on your own server.</p>
<p>Ok, lets go!</p>
<h3>Principles involved</h3>
<p>Traditionally, both SMTP and IMAP/POP3 will look in /etc/passwd or /etc/shadow for users.  This actually works really well so we virtually copy this approach, except we provide a different passwd file for each domain we need to support.  Although this article does not delve into it, both Exim and Dovecot will allow you to store your users in other ways; in a database such as MySQL for example.</p>
<p><a href="http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/">passwd files</a> are structured like this:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">username:password:uid:gid:[gecos]:home:[shell]:[extra]</div>
</div>
<p>gecos, shell and extra are ignored in our case so they&#8217;ll appear empty, though the surrounding colon will appear.</p>
<p>For example, if &#8220;fred&#8221; on your server (UID=1001, GID=100) owns the domain &#8220;example.com&#8221; and wants a new email address setting up called &#8220;pete@example.com&#8221; there might be a file located at /etc/vmail/example.com/passwd with the contents:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">pete:petes-password:1001:100::/home/fred::</div>
</div>
<p>From this we can check if pete is found in the passwd file for example.com, we can also see the uid, gid and home directory of pete, the domain owner.  This is enough information for us to save the incoming email.</p>
<p>A domain can have multiple users, and if two domains have the same user it does not mean they are the same email account.  In order to determine where the mail goes, the home directory part in the passwd file will specify where the email gets saved.  passwd files also conveniently provide UID and GID values so the mail can easily be saved with the correct permissions!</p>
<p>In this article we&#8217;ll look at how to store passwd information in directories we&#8217;ll create at /etc/vmail/ (i.e. /etc/vmail/domain1.com/passwd, /etc/vmail/domain2.tld/passwd) and save emails to mbox style files at ~/mail/domain.tld/user/inbox (i.e. ~/vmail/example.com/joe/inbox).</p>
<p>This makes the assumption that each domain is owned by one of your system users.  A single system user could, for example be a specially created account called &#8220;vmail&#8221; to manage all virtual domains.  However, you can allow individual users to administer their own domains since emails are stored in the $HOME directory of the allocated domain owner.</p>
<p>Both Exim and Dovecot will read from the same passwd files for this to work so we hack them both in a similar fashion.  By the way, when I say &#8220;hack&#8221;, these systems have been written with the intention of being hacked ;)</p>
<h3>Defining your first virtual domain and user</h3>
<p>Create a directory named /etc/vmail.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">mkdir /etc/vmail</div>
</div>
<p>Within that, create a directory for a domain which has a MX record pointing to your server (make sure it&#8217;s all in lowercase).</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">mkdir /etc/vmail/example.com</div>
</div>
<p>Now create a passwd file in there containing the username in the address and the uid, gid, home directory of the owner.  Don&#8217;t worry about the password bit just yet.  I&#8217;m just setting it to &#8220;xxx&#8221; here because incmoming mail does not require a password. You&#8217;ll see why pete@example.com cannot log in with the password &#8220;xxx&#8221; until we change this later.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">echo &quot;pete:xxx:1001:100::/home/fred:: &gt;&gt; /etc/vmail/example.com/passwd</div>
</div>
<p>The remainder of the article will make use of this file.</p>
<p>Now create the place where these emails will be stored (in fred&#8217;s $HOME directory).</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">mkdir -p /home/fred/mail/example.com/pete<br />
chown -R pete:users /home/fred/mail<br />
chmod -R 0700 /home/fred/mail</div>
</div>
<p>Of course, if you&#8217;re going to be doing this regularly you&#8217;d write a tool for creating such files since it can be quite tedious.  I may post a command line tool myself, but as it stands I still do it by hand.</p>
<h3>Setting up Exim</h3>
<p>Be very sure that you have installed Exim4, not Exim3.  Also make sure that any other SMTP server software has been removed to prevent conflicts; remember to look in inetd or xinetd if you&#8217;ve never checked there before.</p>
<p>There are two ways to install Exim4.  You can install with a single configuration file exim.conf, or you can install with separate smaller config files in a conf.d style layout.  I advise using the latter option in the interest of maintainability, but this article will apply either way.  The single config file is simply a glued-together version of the conf.d layout.</p>
<p>The single config file will most likely reside at /etc/exim4/exim.conf.  Inside that file there are sections which start with the keyword &#8220;begin&#8221;; for example &#8220;begin transports&#8221; and &#8220;begin routers&#8221;.  These are where the conf.d style layout gets broken down.  The conf.d style layout places sub-directories in /etc/exim4/conf.d.  For example; /etc/exim4/conf.d/transport, /etc/exim4/conf.d/router etc.</p>
<p>Once you have Exim installed, check the daemon actually runs by starting it from /etc/init.d or /etc/rc.d.  You should be able to telnet to localhost on port 25, getting a greeting message beginning with &#8220;220&#8243;.  Type &#8220;QUIT&#8221; to end the session.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">w3style.co.uk:~# telnet localhost 25<br />
Trying 127.0.0.1&#8230;<br />
Connected to mail.w3style.co.uk.<br />
Escape character is &#8216;^]&#8217;.<br />
220 mail.w3style.co.uk ESMTP Exim 4.63 Sun, 27 May 2007 16:19:15 +0100<br />
QUIT<br />
221 mail.w3style.co.uk closing connection<br />
Connection closed by foreign host.<br />
w3style.co.uk:~#</div>
</div>
<p>If telnet could not connect on port 25, or you get a greeting beginning with anything other than &#8220;220&#8243; something has gone wrong with your installation.  Unfortunately this article cannot delve into possible causes for the sake of brevity, but needless to say, you should consult the <a href="http://www.exim.org/docs.html">exim documentation</a>.  One really obvious thing to check would be the &#8220;local_interfaces&#8221; line in the configuration ;)</p>
<p>Exim has four major areas in its configuration: </p>
<ol>
<li><a href="http://www.exim.org/exim-html-4.50/doc/html/spec_7.html#SECT7.1"><strong>General</strong></a>; contains information such as what interface to listen on, what domains belong to the server and who can relay through it.  Essentially the general configuration sets up variables which are used in the other areas of the configuration.</li>
<li><a href="http://www.exim.org/exim-html-4.50/doc/html/spec_7.html#SECT7.2"><strong>ACLs</strong></a>; programmed logically to determine what happens at each stage in the SMTP processing. For example you can check if the sender is trying to spoof an address or if relaying should be denied from within the ACLs.</li>
<li><a href="http://www.exim.org/exim-html-4.50/doc/html/spec_7.html#SECT7.4"><strong>Transports</strong></a>; specifies how the emails should be delivered (written to a file on disk, added to a database, relayed to another server etc).</li>
<li><a href="http://www.exim.org/exim-html-4.50/doc/html/spec_7.html#SECT7.3"><strong>Routing</strong></a>; determines which transport should be used.</li>
</ol>
<p>Apart from the General configuration, Exim is configured almost entirely by a series of expressions and conditions.  For programmers this might feel normal, but for non-programmers it can be a bit daunting.</p>
<p>We&#8217;ll work backwards here so you can more easily see the chain of how it all ties together.</p>
<h4>The transport configuration</h4>
<p>mbox files which we&#8217;re going to write to use a standard Exim driver called &#8220;appendfile&#8221;.  You can add new transports to Exim by simply adding a new transport declaration.  For the most part we can base the virtual domain transport on the mbox local transport.  If you use a single exim.conf file, scroll down to the transports section inside it.  If you use the conf.d layout, create a new file in /etc/exim4/conf.d/transport/virtual_transport (the name you use is up to you).</p>
<p>Now, add the following transport to the configuration:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">virtual_mail_spool:<br />
&nbsp; driver = appendfile<br />
&nbsp; user = ${extract{2}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}<br />
&nbsp; file = ${extract{5}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}/mail/$domain/$local_part/inbox<br />
&nbsp; group = ${extract{3}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}<br />
&nbsp; mode = 0700</div>
</div>
<p>I accept that it looks extremely complicated.  It is complicated.  But lets break it down a bit and suddenly it seems less intimidating.  The lines ending with a backslash are just partial lines.  The backslash indicates the whatever is on the following line should be appended to it.  You can remove the backslashes and place everything on on line if you wanted to but I find it harder to read.</p>
<p>The first line, &#8220;virtual_mail_spool:&#8221; specifies that you are declaring a new transport method.  You can rename this to suit your needs.</p>
<p>The following lines provide settings for the transport.  Lets break the rest of it down because it looks pretty scary right? :)</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">driver = appendfile</div>
</div>
<p><a href="http://www.exim.org/exim-html-4.50/doc/html/spec_26.html#IX1970">appendfile</a> is the standard mbox driver in Exim.  If you used a Maildir (multiple files) instead of mbox then you&#8217;d have to include an additional &#8220;maildir_format&#8221; line too.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">user = ${extract{2}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}</div>
</div>
<p>Ok, I know, I know.  WTF is that?!  Overall, the line specifies the user permissions (username or uid) under which to write the file.  In this case it pulls out the uid from our passwd file.</p>
<p>The curly braces are used to set the order of precedence and to group expressions together.  The opening ${ indicates that what follows needs to be <a href="http://www.exim.org/exim-html-4.50/doc/html/spec_11.html#IX821">expanded/evaluated</a>.</p>
<p>The variables in there, $local_part and $domain are <a href="http://www.exim.org/exim-html-4.50/doc/html/spec_11.html#IX974">provided by exim</a>.  For an address pete@example.com, $local_part = pete and $domain = example.com.</p>
<p>The first ${<a href="http://www.exim.org/exim-html-4.50/doc/html/spec_11.html#IX822">extract</a>{2}{:}{ .. stuff &#8230; }} tells Exim to split &#8221; &#8230; stuff &#8230; &#8221; at every &#8220;:&#8221; character and then pull out the third chunk (indexing starts at zero).  For a single line in the passwd file this will directly extract the uid number.</p>
<p>Looking at &#8221; &#8230; stuff &#8230; &#8221; we&#8217;ve actually got the expression ${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}.  This finds a single line in the passwd file.</p>
<p>${<a href="http://www.exim.org/exim-html-4.50/doc/html/spec_11.html#IX822">lookup</a>{ &#8230; keyword .. }lsearch{ &#8230; file &#8230; }} is the bit which finds the line in question.  Here it&#8217;s looking for the file at /etc/vmail/example.com because $domain = exmaple.com.  It then looks for the line starting with &#8220;pete&#8221;, thus giving us the correct line in the file and allowing the extract{}{:}{} to provide the correct uid.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">file = ${extract{5}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}/mail/$domain/$local_part/inbox</div>
</div>
<p>If you understood how &#8220;user&#8221; was written, then this should hopefully make sense.  As menioned earlier we want to save files at ~/mail/domain/user/inbox.</p>
<p>This finds the home directory of the domain owner, then appends &#8220;/mail/example.com/pete/inbox&#8221; to it.  It specifies the location of the mbox file to which the mail will be saved.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">group = ${extract{3}{:}\<br />
&nbsp; &nbsp; {${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}}\<br />
&nbsp; &nbsp; }}</div>
</div>
<p>This is structurally identical to the &#8220;user&#8221; line.  It pulls out the gid of the domain owner from the passwd file.</p>
<p>Finally, the line &#8220;mode = 0700&#8243; provides the permissions mode to write the file in.  0700 gives full access to the owner and no access to anybody else.</p>
<p>Summarising all that in plain English it: checks which system user should own the email, determines what file to write the email to, checks which group the owner is in, specifies the permissions on the file to be 0700 (-rwx&#8212;&#8212;).</p>
<p>This is enough for our transport configuration.  However, the transport will never actually be used until a router points to it so we now need to create a router.</p>
<h4>The router configuration</h4>
<p>Essentially a router is just a set of conditions which, if evaluate true cause Exim to honour the transport it points to.  Our router will need to check if the user and domain are valid for our virtual configuration.  This is a simple case of looking to see if a directory exists for the domain in /etc/vmail and if the user line can be seen in the passwd file for that domain.</p>
<p>If you use a single exim.conf file, scroll to the top of routers section.  If you use a conf.d layout, create a new file at /etc/exim4/router/virtual_user.  Exim actually loads files in alphabetical order so you might want to prepend the filename with a number to cause it to be loaded before the other files in a conf.d setup.</p>
<p>Add the following to your configration:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">virtual_user:<br />
&nbsp; driver = accept<br />
&nbsp; domains = dsearch;/etc/vmail<br />
&nbsp; condition = ${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}\<br />
&nbsp; &nbsp; &nbsp; {yes}{no}}</div>
</div>
<p>The first line, &#8220;virtual_user:&#8221; declares that we are creating a new router.  You can call this whatever you like, the name is irrelevant provided it&#8217;s unique.</p>
<p>The line &#8220;driver = accept&#8221; specifies that this router can only be used if the email was accepted by the ACLs (we&#8217;ll look at this later).</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">domains = dsearch;/etc/vmail</div>
</div>
<p>This line specifies which domains this router applies to.  You could hard-code a list by writing &#8220;domain1.com : domain1.org&#8221; etc etc, but that would not be easily extendible.  &#8220;<a href="http://www.exim.org/exim-html-4.50/doc/html/spec_9.html#IX649">dsearch;</a>&#8221; is an expression which returns all files in the directory following the semi-colon.  In our case this line evaluates to all domains we virtual host for because that&#8217;s what the directories in /etc/vmail are.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">condition = ${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}\<br />
&nbsp; &nbsp; &nbsp; {yes}{no}}</div>
</div>
<p>This line must evaluate to &#8220;true&#8221; (or &#8220;yes&#8221;) if the router is to be used.  It looks for a line for the user in the passwd file.  If one is found then the condition returns &#8220;yes&#8221;.  If none is found then the condition returns &#8220;no&#8221; and this router will not be used.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">transport = virtual_mail_spool</div>
</div>
<p>Finally, this specifies the transport we created previously.  It tells Exim that if the conditions are true our new transport should be used.  If you used a different name for the transport remember to modify this line accordingly.</p>
<p>Summarising this in plain English it says: Only use this router if the mail was already accepted and the domain is a virtual domain in /etc/vmail.  If the user cannot be found in /etc/vmail/example.com/passwd, this router should not be used (unroutable address).  The transport this router directs to is &#8220;virtual_mail_spool&#8221;.</p>
<h4>Nearly done! Just one quick test</h4>
<p>Start (or restart) the Exim4 daemon running from /etc/init.d or rc.d you should be able to telnet to it on port 25.  Issue the following commands:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">EHLO example.com<br />
MAIL FROM: <span class="sc2">&lt;you@localhost&gt;</span><br />
RCPT TO: <span class="sc2">&lt;pete@example.com&gt;</span><br />
DATA<br />
Something<br />
.<br />
QUIT</div>
</div>
<p>You should see something like this:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">w3style.co.uk:~# telnet localhost 25<br />
Trying 127.0.0.1&#8230;<br />
Connected to mail.w3style.co.uk.<br />
Escape character is &#8216;^]&#8217;.<br />
220 mail.w3style.co.uk ESMTP Exim 4.63 Sun, 27 May 2007 17:54:20 +0100<br />
EHLO example.com<br />
250-mail.w3style.co.uk Hello localhost [127.0.0.1]<br />
250-SIZE 52428800<br />
250-PIPELINING<br />
250-AUTH CRAM-MD5<br />
250 HELP<br />
MAIL FROM: <span class="sc2">&lt;you@localhost&gt;</span><br />
250 OK<br />
RCPT TO: <span class="sc2">&lt;pete@example.com&gt;</span><br />
250 Accepted<br />
DATA<br />
354 Enter message, ending with &quot;.&quot; on a line by itself<br />
Something<br />
.<br />
250 OK id=1HsM1G-0008Tg-5c<br />
QUIT<br />
221 mail.w3style.co.uk closing connection<br />
Connection closed by foreign host.</div>
</div>
<p>Assuming everything&#8217;s working you should get something similar to that above.  If you&#8217;re not getting this then something has gone wrong.</p>
<p>If it worked, you should see a file at /home/fred/mail/example.com/pete/inbox.  If you open this file it will contain the email.</p>
<p>This is great, it works!  If you have MX records for example.com pointing to this server the server will happily accept mail for the virtual user pete@example.com.  Of course, there&#8217;s no way to read these emails remotely as yet, unless of course you SSH to the server and open the inbox file directly.</p>
<p>Wait, there&#8217;s something else we need to do. If someone tries to email &#8220;no-such-user@example.com&#8221; they will get the same result during SMTP, but they&#8217;ll later get a bounced email because no router was found for &#8220;no-such-user&#8221; after it was accepted.  This is quite normal and I&#8217;m sure we&#8217;ve all had lots these emails in our inboxes over time.  However, if you&#8217;re providing email for a lot of domains this would be wasting valuable bandwidth.</p>
<h4>Defining a new ACL</h4>
<p>It&#8217;s possible to deny a request at the &#8220;RCPT TO:&#8221; phase in SMTP.  In order to do this we create a new ACL (access control list).  Our ACL will check if the domain is a vitual domain, and will then check that it can find a user for that domain.  If it is a virtual domain but no user exists, the ACL will deny the command, thus giving a 550 response.</p>
<p>ACLs are a series of rules.  Each ACL ruleset is evaluated in order until a conclusive result has been decided (accept, or deny).  In reality there are other results such as &#8220;warn&#8221; and &#8220;require&#8221; but we don&#8217;t need to delve into them here.</p>
<p>If you&#8217;re using the single exim.conf file scroll to the ACL section.  For the conf.d style layout, create a new file at /etc/exim4/conf.d/acl/check_virtual_rcpt.  Now add the following to the configuration file:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">acl_check_virtual_rcpt:</p>
<p>&nbsp; # deny RCPT TO if it&#8217;s a virtual domain and no user is found<br />
&nbsp; deny<br />
&nbsp; &nbsp; message = Unknown Recipient<br />
&nbsp; &nbsp; domains = dsearch;/etc/vmail<br />
&nbsp; &nbsp; !condition = ${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}\<br />
&nbsp; &nbsp; &nbsp; {yes}{no}}</p>
<p>&nbsp; # accept otherwise<br />
&nbsp; accept</div>
</div>
<p>The first line, &#8220;acl_check_virtual_rcpt:&#8221; defines that you are creating a new ACL ruleset.</p>
<p>The line &#8220;deny&#8221; specifies that the conditions which follow are assessing whether to deny the request.  If all the following conditions evaluate true the request will be denied (our 550 response in RCPT).</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">message = Unknown Recipient</div>
</div>
<p>This is simply a message which will be displayed in SMTP.  You can change it to something like &#8220;Sorry, I don&#8217;t know about this user&#8221; if you really wanted to.  If the request is denied the error will say &#8220;550 Unknown Recipient&#8221; in our case.  I think this is quite sensible ;)</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">domains = dsearch;/etc/vmail</div>
</div>
<p>As with our router configuration, this line indicates that the ruleset only applies if the domain is a virtual domain found in /etc/vmail.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">!condition = ${lookup{$local_part}lsearch{/etc/vmail/$domain/passwd}\<br />
&nbsp; &nbsp; &nbsp; {yes}{no}}</div>
</div>
<p>This is actually the same line I used in the router, except for the leading &#8220;!&#8217; character.  It looks to see if the user is found in the passwd file.  If the user is in the passwd file the condition returns true.  However, the &#8220;!&#8221; before the &#8220;condition&#8221; keyword indicates that the response should be negated, so if no such user is found, the line evaluates to &#8220;yes&#8221; rather than &#8220;no&#8221; and therefore all conditions were satisfied which concludes that the request should be denied.</p>
<p>The final line &#8220;accept&#8221; simply indicates that if the request was not denied, our ACL ruleset can accept the email.  However, another ruleset somewhere else may still deny it ;)</p>
<p>As it stands, Exim does not know to run our ACL yet.  In order to do that we need to specify it with the existing ACL for &#8220;acl_smtp_rcpt&#8221;.  Go to the general configuration section and find the line starting with &#8220;acl_smtp_rcpt = &#8220;.  If no such line exists, create it, but before any &#8220;begin&#8221; sections in the configuration file.  For example, the line might look like this:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">acl_smtp_rcpt = acl_check_rcpt</div>
</div>
<p>This line basically tells Exim that during the RCPT phases in SMTP it should carry out the ACL checks defined in the &#8220;acl_check_rcpt&#8221; ruleset.  If there isn&#8217;t a line already there then simply set &#8220;acl_smtp_rcpt = acl_check_virtual_rcpt&#8221; and nothing more is needed.  If there is a line there, look at what is assigned to it (acl_check_rcpt for example), then find that acl ruleset.  At the top of the ruleset it points to, add these lines:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">deny<br />
&nbsp; !acl = acl_check_virtual_rcpt</div>
</div>
<p>That basically gets the other ACL to run the checks in your ACL too.</p>
<p>Restart exim and try running those SMTP commands over telnet again, this time replacing the address in RCPT TO with &#8220;nobody-here@example.com&#8221;  You should get an immediate &#8220;550&#8243; response.  This is good.  It will cut out a lot of wated bandwidth from bounced messages when spammers start sending emails to addresses that don&#8217;t really exist.</p>
<p>That&#8217;s it for the Exim configuration.  Phew!  Now we need some way to allow our users to open up their inboxes remotely.  Thankfully the Dovecot configuration is far less complicated than the Exim configuration ;)</p>
<h3>Setting up Dovecot</h3>
<p>Make sure you have uninstalled any other imap servers which may be present on your system.  Remember to check in inetd or xinetd if you&#8217;ve never checked there before ;)</p>
<p>Unlike Exim which is seriously complicated to configure unless you&#8217;re a programmer (like myself), Dovecot is a lot friendlier.  It&#8217;s still flexible enough for our needs but for the most part, getting it to do what we want is trivial.  The main dovecot configuration is stored in a single file at /etc/dovecot/dovecot.conf.</p>
<h4>Making dovecot listen for IMAP requests</h4>
<p>The first thing you want to do is edit the line starting with &#8220;protocols =&#8221;:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">protocols = imap</div>
</div>
<p>Now we&#8217;ll just run a quick test to make sure it&#8217;s working.  Start (or restart) dovecot from /etc/init.d or rc.d.  The telnet to localhost, port 143.  You should get a Dovecot greeting.  Type &#8220;a01 LOGOUT&#8221; to end the session:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">w3style.co.uk:~# telnet localhost 143<br />
Trying 127.0.0.1&#8230;<br />
Connected to mail.w3style.co.uk.<br />
Escape character is &#8216;^]&#8217;.<br />
* OK Dovecot ready.<br />
a01 LOGOUT<br />
* BYE Logging out<br />
a01 OK Logout completed.<br />
Connection closed by foreign host.</div>
</div>
<p>If telnet cannot connect, or if you get some sort of error message then your imap installation is not working correctly.  You&#8217;ll need to consult the dovecot documentation for further help before continuing with the configuration.</p>
<h4>Specifying the mail location</h4>
<p>Like Exim, Dovecot provides some variables for us to use.  Scroll down to the section &#8220;Mailbox locations and namespaces&#8221; then edit the line starting with &#8220;mail_location = &#8220;:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">mail_location = mbox:%h/mail/%d/%n</div>
</div>
<p>This is actually pretty straightforward.  &#8220;mbox:&#8221; defines that we&#8217;re using mbox files rather than maildir.  If you use maildir you&#8217;re change it to &#8220;maildir:&#8221;.  %h is the variable for $HOME of the domain owner.  This is /home/fred in our example.  %d is the domain name and %n is the local part of the address (i.e. for pete@example.com; %n = pete, %d = example.com).</p>
<p>Make sure Dovecot is going to use the correct file permissions (0700, the same as Exim) by specifying, in the &#8220;Mail processes&#8221; section:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">umask = 0077</div>
</div>
<h4>Configuring the login process</h4>
<p>We&#8217;ve already got a passwd file for pete@example.com which we created earlier.  Dovecot can use this just like Exim can.  Dovecot does however need to know what pete&#8217;s password is.  Up until now we haven&#8217;t considered what goes in the password part of the passwd file.  The official passwd/shadow format expects a hashed password as returned by crypt() with a salt.  Dovecot allows you to use plain text passwords however.  I&#8217;m not condoning the use of plain text passwords, but for the sake of simplicity in this article I&#8217;ll show you how it&#8217;s done.</p>
<p>Open up /etc/vmail/example.com/passwd.</p>
<p>Edit the &#8220;xxx&#8221; part where the password should be to say:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">pete:{PLAIN}petes-password:1001:100::/home/fred::</div>
</div>
<p>The {PLAIN} part at the start of the password specifies that the password is stored in plain text.  In this case, pete can log in with the username &#8220;pete@example.com&#8221; and the password &#8220;petes-password&#8221;.  If you don&#8217;t specify the {PLAIN} part then the password will be assumed to be hashed with crypt().  There are tools available for producing such hashes and in the interest of security it would be advisable to do this.</p>
<p>Now scroll down the the &#8220;Authentication processes&#8221; section in dovecot.conf.  Edit the line starting with &#8220;auth_username_format&#8221;.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">auth_username_format = %Lu</div>
</div>
<p>This causes usernames to always be in lowercase.</p>
<p>Inside the block starting with &#8220;auth default {&#8221; make sure &#8220;plain&#8221; is in the list of mechanisms.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">auth default {<br />
&nbsp; # Space separated list of wanted authentication mechanisms:<br />
&nbsp; #&nbsp; &nbsp;plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi<br />
&nbsp; mechanisms = plain</div>
</div>
<p>Without this our {PLAIN} part in the passwd file will not work.</p>
<p>There are two phases in authentication: Password verification, and user lookup.  These are called &#8220;passdb&#8221; and &#8220;userdb&#8221; in dovecot.  Scroll a little further down in the configuration and change the section &#8220;passdb passwd-file {&#8221;.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">passdb passwd-file {<br />
&nbsp; &nbsp; # File contains a list of usernames, one per line<br />
&nbsp; &nbsp; args = /etc/vmail/%d/passwd<br />
&nbsp; &nbsp; deny = no<br />
&nbsp; }</div>
</div>
<p>This specifies that for verifying passwords, dovecot can look in our passwd file for the virtual domain.  Notice the %d variable in there again.  &#8220;deny = no&#8221; specifies that dovecot should grant access if the password matches.</p>
<p>Now scroll a little further and edit the section &#8220;userdb passwd-file {&#8221;:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">userdb passwd-file {<br />
&nbsp; &nbsp; # Path for passwd-file<br />
&nbsp; &nbsp; args = /etc/vmail/%d/passwd<br />
&nbsp; }</div>
</div>
<p>This simply allows dovecot to use the same passwd file to find the user information after verifying the password.  We should be done now :)</p>
<h4>A final test!</h4>
<p>Restart dovecot.  Now try to telnet to localhost on port 143 again.  This time logging in as &#8220;pete@example.com&#8221; with the password &#8220;petes-password&#8221;.  Then we&#8217;ll try selecting pete&#8217;s inbox.  The command sequence is:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">a01 LOGIN pete@example.com petes-password<br />
a02 SELECT inbox<br />
a03 LOGOUT</div>
</div>
<p>You should see something like this:</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">w3style.co.uk:~# telnet localhost 143<br />
Trying 127.0.0.1&#8230;<br />
Connected to mail.w3style.co.uk.<br />
Escape character is &#8216;^]&#8217;.<br />
* OK Dovecot ready.<br />
a01 LOGIN pete@example.com petes-password<br />
a01 OK Logged in.<br />
a02 SELECT inbox<br />
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk $NotJunk $Junk JunkRecorded $MDNSent $Forwarded)<br />
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk $NotJunk $Junk JunkRecorded $MDNSent $Forwarded \*)] Flags permitted.<br />
* 1 EXISTS<br />
* 0 RECENT<br />
* OK [UIDVALIDITY 1164274548] UIDs valid<br />
* OK [UIDNEXT 2] Predicted next UID<br />
a02 OK [READ-WRITE] Select completed.<br />
a03 LOGOUT<br />
* BYE Logging out<br />
a03 OK Logout completed.<br />
Connection closed by foreign host.</div>
</div>
<p>Notice the line &#8220;* 1 EXISTS&#8221;.  This is the email we sent when we were testing Exim.  This means it worked!  If you see &#8220;0 EXISTS&#8221; or you cannot actually log in then something has gone wrong and Dovecot is looking in the wrong place for the emails and/or login files.</p>
<p>If you were to try setting up your email client such as thunderbird or evolution using your server as an imap server with those credentials you should be able to read your emails successfully.  If someone sends an email to that email address, assuming there&#8217;s a MX record pointing it to your server then you should be able to access the email over IMAP.</p>
<p>To add new domains simply create new passwd files in /etc/vmail/domain.tld.  To change passwords or add users simply edit the contents of the relevant passwd file.</p>
<p>Good luck! :D</p>
]]></content:encoded>
			<wfw:commentRss>http://www.w3style.co.uk/virtual-domains-with-exim-and-dovecot/feed</wfw:commentRss>
		</item>
		<item>
		<title>A MySQL Class/Iterator for PHP5</title>
		<link>http://www.w3style.co.uk/a-mysql-classiterator-for-php5</link>
		<comments>http://www.w3style.co.uk/a-mysql-classiterator-for-php5#comments</comments>
		<pubDate>Wed, 26 Jul 2006 22:10:22 +0000</pubDate>
		<dc:creator>d11wtq</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.w3style.co.uk/a-mysql-classiterator-for-php5</guid>
		<description><![CDATA[After some discussion over at DevNetwork Forums regarding MySQL classes I was given an idea which changes the way I&#8217;ve always written MySQL wrappers.  Previously there had always been just one class.  That class returned result resources and provided the wrapper methods for working with these.
The inspiration I was given in this scenario [...]]]></description>
			<content:encoded><![CDATA[<p>After some discussion over at DevNetwork Forums regarding MySQL classes I was given an idea which changes the way I&#8217;ve always written MySQL wrappers.  Previously there had always been just one class.  That class returned result resources and provided the wrapper methods for working with these.</p>
<p>The inspiration I was given in this scenario however was to work with two objects.  One which holds the connection, runs the quer