<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laneolson.ca &#187; Programming</title>
	<atom:link href="http://www.laneolson.ca/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.laneolson.ca</link>
	<description></description>
	<lastBuildDate>Fri, 07 Jan 2011 23:06:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Compiling PHP 5.3 on Ubuntu 9.10</title>
		<link>http://www.laneolson.ca/2010/02/19/compiling-php-5-3-on-ubuntu-9-10/</link>
		<comments>http://www.laneolson.ca/2010/02/19/compiling-php-5-3-on-ubuntu-9-10/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 21:11:03 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.laneolson.ca/?p=177</guid>
		<description><![CDATA[<p>This guide will outline how to compile PHP 5.3 from source for use with Apache.  The instructions in this guide have been tested with Ubuntu 9.10 and should work on other debian based distros.  This guide is a slight modification of the <a href="http://vimeo.com/8005503" target="_blank">video guide on setting up Apache, PHP 5.3 with xdebug, MongoDB, and Lithium</a> created by Jon Adams. Before proceeding with the instructions below you will need Apache installed on your computer.  If you do not yet have apache you can follow my <a href="http://www.laneolson.ca/2010/02/11/installing-and-configuring-apache-for-multiple-sites-on-ubuntu/">guide for setting up Apache on Ubuntu 9.10</a>.</p>
<p>The first step for installing PHP 5.3 is to get the prerequisites.  For this guide you will need the following libraries in order to compile php with the options that I use.  If you have some custom libraries/extensions that you want included in your build of PHP, now is the time to install them if you haven&#8217;t already.</p>
<p>The following command will install the libraries that I will need:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libmcrypt-dev libxml2 libxml2-dev libbz2-dev libcurl4-gnutls-dev libjpeg62-dbg libpng12-dev libxslt1-dev libbz2-dev</pre></div></div>

<p>Once our required libraries have been installed we can go to http://www.php.net/downloads.php and choose to download the latest 5.3.x build in tar.gz format.  I chose to copy the download link and use wget to download it to the tmp directory, but you can do whatever you like.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp

<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ca2.php.net<span style="color: #000000; font-weight: bold;">/</span>get<span style="color: #000000; font-weight: bold;">/</span>php-5.3.1.tar.gz<span style="color: #000000; font-weight: bold;">/</span>from<span style="color: #000000; font-weight: bold;">/</span>this<span style="color: #000000; font-weight: bold;">/</span>mirror</pre></div></div>

<p>now untar the file and go into the directory</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvf</span> php-5.3.1.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> php-5.3.1</pre></div></div>

<p>Now comes the fun part.  Here we will run the configure script that says what kind of extensions we want php built with...</p>]]></description>
			<content:encoded><![CDATA[<p>This guide will outline how to compile PHP 5.3 from source for use with Apache.  The instructions in this guide have been tested with Ubuntu 9.10 and should work on other debian based distros.  This guide is a slight modification of the <a href="http://vimeo.com/8005503" target="_blank">video guide on setting up Apache, PHP 5.3 with xdebug, MongoDB, and Lithium</a> created by Jon Adams. Before proceeding with the instructions below you will need Apache installed on your computer.  If you do not yet have apache you can follow my <a href="http://www.laneolson.ca/2010/02/11/installing-and-configuring-apache-for-multiple-sites-on-ubuntu/">guide for setting up Apache on Ubuntu 9.10</a>.</p>
<p>The first step for installing PHP 5.3 is to get the prerequisites.  For this guide you will need the following libraries in order to compile php with the options that I use.  If you have some custom libraries/extensions that you want included in your build of PHP, now is the time to install them if you haven&#8217;t already.</p>
<p>The following command will install the libraries that I will need:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libmcrypt-dev libxml2 libxml2-dev libbz2-dev libcurl4-gnutls-dev libjpeg62-dbg libpng12-dev libxslt1-dev libbz2-dev</pre></div></div>

<p>Once our required libraries have been installed we can go to http://www.php.net/downloads.php and choose to download the latest 5.3.x build in tar.gz format.  I chose to copy the download link and use wget to download it to the tmp directory, but you can do whatever you like.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ca2.php.net<span style="color: #000000; font-weight: bold;">/</span>get<span style="color: #000000; font-weight: bold;">/</span>php-5.3.1.tar.gz<span style="color: #000000; font-weight: bold;">/</span>from<span style="color: #000000; font-weight: bold;">/</span>this<span style="color: #000000; font-weight: bold;">/</span>mirror</pre></div></div>

<p>now untar the file and go into the directory</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvf</span> php-5.3.1.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> php-5.3.1</pre></div></div>

<p>Now comes the fun part.  Here we will run the configure script that says what kind of extensions we want php built with.  At the beginning of the tutorial we installed a bunch of libraries (for example libbz2) and because of that we are able to build PHP with support for these libraries by specifying the &#8211;with-%library% flag.  To see the options available for configuring php type:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--help</span></pre></div></div>

<p>You should be fine using what I used below, but feel free to make adjustments as you see fit:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr <span style="color: #660033;">--with-config-file-path</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">--with-config-file-scan-dir</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">--with-apxs2</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apxs2 <span style="color: #660033;">--with-bz2</span> <span style="color: #660033;">--with-curl</span> <span style="color: #660033;">--with-gd</span> <span style="color: #660033;">--with-iconv</span> <span style="color: #660033;">--with-mcrypt</span> <span style="color: #660033;">--with-mysql</span> <span style="color: #660033;">--with-mysqli</span> <span style="color: #660033;">--with-openssl</span> <span style="color: #660033;">--with-pcre-regex</span> <span style="color: #660033;">--with-pdo-mysql</span> <span style="color: #660033;">--with-pear</span> <span style="color: #660033;">--with-xmlrpc</span> <span style="color: #660033;">--with-xsl</span> <span style="color: #660033;">--with-zlib</span> <span style="color: #660033;">--enable-ftp</span> <span style="color: #660033;">--enable-mbstring</span> <span style="color: #660033;">--enable-soap</span> <span style="color: #660033;">--enable-sockets</span></pre></div></div>

<p>If all goes well with the configure script you can continue to run the make file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #660033;">-i</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Once the make script is finished running we have to manually create the modules for Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods-available
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> php5.load</pre></div></div>

<p>The contents of this file should be:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LoadModule php5_module <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>libphp5.so</pre></div></div>

<p>Now create the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> php5.conf</pre></div></div>

<p>with the contents: </p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;</span>IfModule mod_php5.c<span style="color: #000000; font-weight: bold;">&gt;</span>
	AddType application<span style="color: #000000; font-weight: bold;">/</span>x-httpd-php .php .phtml .php3
	AddType application<span style="color: #000000; font-weight: bold;">/</span>x-httpd-php-source .phps
<span style="color: #000000; font-weight: bold;">&lt;/</span>IfModule<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>Once those two files have been created we can enable the php5 module for Apache, then restart Apache for the new module to load.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> a2enmod php5
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>Now go into your web directory (if you followed my apache guide earlier go to ~/www/sandbox.loc/httpdocs) and create a file called test.php</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">nano</span> ~<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>sandbox.loc<span style="color: #000000; font-weight: bold;">/</span>httpdocs<span style="color: #000000; font-weight: bold;">/</span>test.php</pre></div></div>

<p>with the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">phpinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now point your web browser to the new file you created.  Which should be at http://sandbox.loc/test.php, you should get a phpinfo page if all went according to plan.</p>
<p>You have now successfully installed php 5.3.  On the phpinfo page if you scroll down to the date section you will probably see an error saying you have to set your timezone.  You can do this with the php.ini file.  To ceate the php in file first create the /etc/php5 directory and copy the php.ini-production file included in the php-5.3.1 tar file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>php-5.3.1<span style="color: #000000; font-weight: bold;">/</span>php.ini-production <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>php.ini</pre></div></div>

<p>Now edit the php.ini file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php5<span style="color: #000000; font-weight: bold;">/</span>php.ini</pre></div></div>

<p>and find the line beginning with &#8220;;date.timezone&#8221;, remove the semi-colon and set the value to your desired time zone.  There is a <a href="http://ca.php.net/manual/en/timezones.php">list of supported timezones</a> in the PHP manual.  For example, I changed mine to:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">date.timezone = <span style="color: #ff0000;">&quot;America/Edmonton&quot;</span></pre></div></div>

<p>You should now be set to start using PHP 5.3 with Apache!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2010/02/19/compiling-php-5-3-on-ubuntu-9-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced Regular Expressions: Some Tools and an Example</title>
		<link>http://www.laneolson.ca/2010/02/09/advanced-regular-expressions-some-tools-and-an-example/</link>
		<comments>http://www.laneolson.ca/2010/02/09/advanced-regular-expressions-some-tools-and-an-example/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 20:49:47 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.laneolson.ca/?p=151</guid>
		<description><![CDATA[<p>I’ve found myself having to build a few somewhat complex (in my opinion) regular expressions over the last few days in order to index certain fields for Splunk.  A good friend of mine pointed me in the direction of a regular expression testing tool a while ago and it has proved to be extremely useful.  The tool, <a target="_blank" href="http://gskinner.com/RegExr/">RegExr</a>, gives a good overview of examples, special characters, and even community submitted regular expressions for you to use.  Most importantly it lets you test your regular expression on a sample of user submitted text.</p>
<p>This is a great tool for <a target="_blank" href="http://www.splunk.com/">Splunk</a>.  All you have to do is copy an event that you want to capture a custom field in, paste it in the tool, then work with the regular expression until it captures that data you need.  One example of a regular expression that I built is this monster:</p>

<div class="wp_syntax"><div class="code"><pre style="font-family: monospace;" class="bash"><span style="color: rgb(122, 8, 116); font-weight: bold;">(</span>http<span style="color: rgb(0, 0, 0); font-weight: bold;">&#124;</span>https<span style="color: rgb(122, 8, 116); font-weight: bold;">)</span>:<span style="color: rgb(0, 0, 0); font-weight: bold;">//</span><span style="color: rgb(122, 8, 116); font-weight: bold;">(</span><span style="color: rgb(122, 8, 116); font-weight: bold;">(</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z0-<span style="color: rgb(0, 0, 0);">9</span>\.\-<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(0, 0, 0); font-weight: bold;">*</span><span style="color: rgb(122, 8, 116); font-weight: bold;">)</span>?\.<span style="color: rgb(122, 8, 116); font-weight: bold;">)</span>?<span style="color: rgb(122, 8, 116); font-weight: bold;">(</span>?<span style="color: rgb(0, 0, 0); font-weight: bold;">&#60;</span>domain_name<span style="color: rgb(0, 0, 0); font-weight: bold;">&#62;</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z0-<span style="color: rgb(0, 0, 0);">9</span>\-<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z0-<span style="color: rgb(0, 0, 0);">9</span>\-<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(0, 0, 0); font-weight: bold;">*</span>\.<span style="color: rgb(122, 8, 116); font-weight: bold;">(</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span>\.<span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span><span style="color: rgb(0, 0, 0); font-weight: bold;">&#124;</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span>A-Za-z<span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">2</span>,<span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span><span style="color: rgb(122, 8, 116); font-weight: bold;">)</span><span style="color: rgb(0, 0, 0); font-weight: bold;">&#124;</span><span style="color: rgb(122, 8, 116); font-weight: bold;">[</span><span style="color: rgb(0, 0, 0);">0</span>-<span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">1</span>,<span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span>\.<span style="color: rgb(122, 8, 116); font-weight: bold;">[</span><span style="color: rgb(0, 0, 0);">0</span>-<span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">1</span>,<span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span>\.<span style="color: rgb(122, 8, 116); font-weight: bold;">[</span><span style="color: rgb(0, 0, 0);">0</span>-<span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">1</span>,<span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span>\.<span style="color: rgb(122, 8, 116); font-weight: bold;">[</span><span style="color: rgb(0, 0, 0);">0</span>-<span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(122, 8, 116); font-weight: bold;">]</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span><span style="color: rgb(0, 0, 0);">1</span>,<span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(122, 8, 116); font-weight: bold;">}</span><span style="color: rgb(122, 8, 116); font-weight: bold;">)</span></pre></div></div>

<p>I’ll explain what this one does in a second, but you can probably guess by looking at it.  With Splunk I am indexing all data that goes through the HTTP proxy on the firewall.  Each event that Splunk indexes from the proxy includes the address processessed, right down to the file name.  However, I’m more interested in pooling all of the events by domain name to get total amount of requests, time spent, etc by domain name.  So, I needed a regular expression to extract the domain name; enter the mess of characters from above...</p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found myself having to build a few somewhat complex (in my opinion) regular expressions over the last few days in order to index certain fields for Splunk.  A good friend of mine pointed me in the direction of a regular expression testing tool a while ago and it has proved to be extremely useful.  The tool, <a href="http://gskinner.com/RegExr/" target="_blank">RegExr</a>, gives a good overview of examples, special characters, and even community submitted regular expressions for you to use.  Most importantly it lets you test your regular expression on a sample of user submitted text.</p>
<p>This is a great tool for <a href="http://www.splunk.com/" target="_blank">Splunk</a>.  All you have to do is copy an event that you want to capture a custom field in, paste it in the tool, then work with the regular expression until it captures that data you need.  One example of a regular expression that I built is this monster:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span>http<span style="color: #000000; font-weight: bold;">|</span>https<span style="color: #7a0874; font-weight: bold;">&#41;</span>:<span style="color: #000000; font-weight: bold;">//</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\.\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>?\.<span style="color: #7a0874; font-weight: bold;">&#41;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?<span style="color: #000000; font-weight: bold;">&lt;</span>domain_name<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>I&#8217;ll explain what this one does in a second, but you can probably guess just by looking at it.  With Splunk I am indexing all data that goes through the HTTP proxy on the firewall.  Each event that Splunk indexes from the proxy includes the address processessed, right down to the file name.  However, I&#8217;m more interested in pooling all of the events by domain name to get total amount of requests, time spent, etc by domain name.  So, I needed a regular expression to extract the domain name; enter the mess of characters from above.</p>
<p>The regular expression above will break down a URL and capture its domain name.  For example the regex will capture example.com from the following url:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">http:<span style="color: #000000; font-weight: bold;">//</span>www.example.com<span style="color: #000000; font-weight: bold;">/</span>some_folder<span style="color: #000000; font-weight: bold;">/</span>filename.php?<span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">id</span></span>=<span style="color: #000000;">34</span><span style="color: #000000; font-weight: bold;">&amp;</span><span style="color: #007800;">name</span>=something</pre></div></div>

<p>This is actually simple enough to capture but became more complex when you considered the following:
<ul>
<li>Subdomains (whatever.example.com)</li>
<li>Odd domain extensions (ab.ca, co.uk)</li>
<li>IP Address domains (64.75.34.12)</li>
</ul>
<p>Here is a break down of the regular expression mentioned above.</p>
<h4>Capture group 1:</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span>http<span style="color: #000000; font-weight: bold;">|</span>https<span style="color: #7a0874; font-weight: bold;">&#41;</span>:<span style="color: #000000; font-weight: bold;">//</span></pre></div></div>

<p>Pretty straight forward, the url must begin with http or https.  This is a HTTP/S proxy so I know that it will begin with either of these two values.  If you were to use this for an FTP proxy you could easily put in ftp.  The http/s is always followed by ://.</p>
<h4>Capture Group 2:</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\.\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#41;</span>?</pre></div></div>

<p>This part of the expression is to capture the subdomain.  It looks for any number containing characters in the range A-Z, a-z, 0-9, ., and – followed by a . (dot).  The question mark at the end of this part of the expression means that it is optional, that is, not all URL&#8217;s have subdomains.  Now that we have the subdomain, the next thing to process will be the domain name.</p>
<h4>Capture Group 3 (this one&#8217;s a doosey):</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>2s,<span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>This one is so long because it looks for a domain name, or an IP address.  Here is the part that captures a domain name:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span>,<span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>A domain name must be at lease 2 characters, the first part takes care of that: [A-Za-z0-9\-]{2}.</p>
<p>The expression that follows that captures the remaining characters in the domain up until the “.” before the domain extension:  [A-Za-z0-9\-]*\.</p>
<p>The final part captures the domain extension, which can sometimes be a provice/state followed by a country code (ab.ca or fl.us which is ([A-Za-z]{2}\.[A-Za-z]{2}) or (|) an extension from two characters (ie: .ca) to six characters (ie: .museum) which is represented by [A-Za-z]{2,6}.</p>
<p>I also wanted to capture the IP address if the http request used an IP instead of a host name.  That&#8217;s what the last part captures:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>This one is fairly easy as well.  An Ipv4 address is just a series of 1 to 3 digits, a dot, 1 to 3 digits, a dot, 1 to 3 digits, a dot, then 1 to 3 digits.  That is what the above captures.</p>
<p>To reiterate, the full regular expression is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span>http<span style="color: #000000; font-weight: bold;">|</span>https<span style="color: #7a0874; font-weight: bold;">&#41;</span>:<span style="color: #000000; font-weight: bold;">//</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\.\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>?\.<span style="color: #7a0874; font-weight: bold;">&#41;</span>?<span style="color: #7a0874; font-weight: bold;">&#40;</span>?<span style="color: #000000; font-weight: bold;">&lt;</span>domain_name<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z0-<span style="color: #000000;">9</span>\-<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>A-Za-z<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">2</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>You may be wondering what the ?<domain_name> is for.  That is just a way to assign the value of the 4th capture group a variable named “domain_name” in Splunk.  If you take out the ?<domain_name> capture group 4 will still contain the domain name.</p>
<p>I feel that learning and becoming comfortable with regular expressions is extremely important if you do any kind of programming.  At first they may seem a little daunting, but once you get the patterns down they are actually quite simple to write.  They allow you to parse almost any kind of data and nearly every programming language has some kind of implementation for them.  Using a tool like <a href="http://gskinner.com/RegExr/" target="_blank">RegExr</a> is a great way to learn to write regular expressions and also test them out once you get the hang of it.  You can also find a large library of regular expressions at <a href="http://regexlib.com/" target="_blank">regexlib.com</a>.</p>
<p><strong>Regular Expression Links:</strong></p>
<ul>
<li><a href="http://gskinner.com/RegExr/" target="_blank">RegExr</a> &#8211; Regular Expression Testing Tool</li>
<li><a href="http://regexlib.com/" target="_blank">RegExLib.com</a> &#8211; A collection of regular expressions</li>
<li><a href="http://www.aivosto.com/vbtips/regex.html" target="_blank">An Introduction to Regular Expressions</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2010/02/09/advanced-regular-expressions-some-tools-and-an-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Page Not Found When Posting or Editing WordPress Posts</title>
		<link>http://www.laneolson.ca/2009/05/22/page-not-found-when-posting-or-editing-wordpress-posts/</link>
		<comments>http://www.laneolson.ca/2009/05/22/page-not-found-when-posting-or-editing-wordpress-posts/#comments</comments>
		<pubDate>Fri, 22 May 2009 19:15:10 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://laneolson.ca/?p=47</guid>
		<description><![CDATA[Today I was copying over some old posts I had on another WordPress install and for some reason when I went to edit one of the posts I was redirected to a 404 error.  I thought this was odd, so I tried to repost the item as a new post.  I still received the 404 error!  After some Googling I found this blog post which outlined some solutions to this problem.  It appears that some people have had some luck using those solutions.  However, none of them worked for me&#8230; even encoding the whole post into HTML entities!  
It is indeed a problem with a security module in Apache.  If you are as unlucky as I was to still have the problem after trying out the solutions in the blog post above, then you have the joy of tracking down the specific word that appears to be the problem.  In my case, I was posting a code snippet from my post on Caching Data with CakePHP.  For some reason mod_security was flagging my use of the word &#8220;settings&#8221;.  Once I changed the variable names in the post it worked flawlessly.
]]></description>
			<content:encoded><![CDATA[<p>Today I was copying over some old posts I had on another WordPress install and for some reason when I went to edit one of the posts I was redirected to a 404 error.  I thought this was odd, so I tried to repost the item as a new post.  I still received the 404 error!  After some Googling I found <a href="http://abing.gotdns.com/posts/2006/wordpress-error-404-when-publishing-or-saving-post/" target="blank">this blog post</a> which outlined some solutions to this problem.  It appears that some people have had some luck using those solutions.  However, none of them worked for me&#8230; even encoding the whole post into HTML entities!  </p>
<p>It is indeed a problem with a security module in Apache.  If you are as unlucky as I was to still have the problem after trying out the solutions in the blog post above, then you have the joy of tracking down the specific word that appears to be the problem.  In my case, I was posting a code snippet from my post on Caching Data with CakePHP.  For some reason mod_security was flagging my use of the word &#8220;settings&#8221;.  Once I changed the variable names in the post it worked flawlessly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2009/05/22/page-not-found-when-posting-or-editing-wordpress-posts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching Data Queries in CakePHP</title>
		<link>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/</link>
		<comments>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/#comments</comments>
		<pubDate>Fri, 22 May 2009 16:44:44 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://laneolson.ca/?p=33</guid>
		<description><![CDATA[In a recent CMS project in CakePHP I was loading in some data from a model to use nearly on every page.  This data is not updated very often, so I figured I would cache the query.  The method of caching data I am going to outline can be done not just for queries&#8230; but for any kind of data really.  Lets get started!
The Cache Function
CakePHP&#8217;s cache function is quite easy to use.  The code snippet below shows how to use it.

1
2
3
4
5
6
7
8
9
10
11
$cache_name = 'query';
$cache_data = Cache::read&#40;$cache_name&#41;;
if &#40;empty&#40;$cache_data&#41;&#41;
&#123;
	$dataModel = $this-&#62;Model-&#62;find&#40;'first'&#41;;
	Cache::write&#40;$cache_name, $dataModel, array&#40;'config' =&#62; 'File', 'duration' =&#62; 7200&#41;&#41;;
&#125;
else
&#123;
	$dataModel = $cache_data;
&#125;

The code above attempts to read in the data from the cache, given the $cache_name.  If the data is not found, the query is executed and then written to the cache.  As you can see in the Cache::write I am using the File engine for caching, but you can use whichever you desire.  I also set the duration to 7200 seconds (2 hours).  If the cache data IS found then the query is skipped and the cache data is assigned to $dataModel.  Pretty nifty eh?
I also believe that when you do a save operation on the Settings model Cake knows to automatically remove that cache data, so you don&#8217;t end up with out of sync info!  If for some reason you want to manually delete the cached data you can use: Cache::delete($key) for a specific key, or Cache::clear() to delete everything in the cache.
]]></description>
			<content:encoded><![CDATA[<p>In a recent CMS project in CakePHP I was loading in some data from a model to use nearly on every page.  This data is not updated very often, so I figured I would cache the query.  The method of caching data I am going to outline can be done not just for queries&#8230; but for any kind of data really.  Lets get started!</p>
<h3>The Cache Function</h3>
<p>CakePHP&#8217;s cache function is quite easy to use.  The code snippet below shows how to use it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cache_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'query'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cache_data</span> <span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$dataModel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Cache<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dataModel</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'File'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'duration'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">7200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$dataModel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache_data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The code above attempts to read in the data from the cache, given the $cache_name.  If the data is not found, the query is executed and then written to the cache.  As you can see in the Cache::write I am using the File engine for caching, but you can use whichever you desire.  I also set the duration to 7200 seconds (2 hours).  If the cache data IS found then the query is skipped and the cache data is assigned to $dataModel.  Pretty nifty eh?</p>
<p>I also believe that when you do a save operation on the Settings model Cake knows to automatically remove that cache data, so you don&#8217;t end up with out of sync info!  If for some reason you want to manually delete the cached data you can use: Cache::delete($key) for a specific key, or Cache::clear() to delete everything in the cache.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Share Your CakePHP Core and Application Files Server Wide</title>
		<link>http://www.laneolson.ca/2008/12/10/share-your-cakephp-core-server-wide/</link>
		<comments>http://www.laneolson.ca/2008/12/10/share-your-cakephp-core-server-wide/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 23:52:41 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[code reuse]]></category>

		<guid isPermaLink="false">http://laneolson.wordpress.com/?p=15</guid>
		<description><![CDATA[If you are deploying several Cake applications server wide you may want to keep the Cake core in one central place so that it can be easily updated.  In order to do this place the cake core directory somewhere on your server where is can be accessed.  For example /usr/lib/php/cake.  Then, when you create your applications open up your root index file  and replace the following:

1
2
3
if &#40;!defined&#40;'CAKE_CORE_INCLUDE_PATH'&#41;&#41; &#123;
	define&#40;'CAKE_CORE_INCLUDE_PATH', ROOT&#41;;
&#125;

with this:

1
2
3
if &#40;!defined&#40;'CAKE_CORE_INCLUDE_PATH'&#41;&#41; &#123;
	define&#40;'CAKE_CORE_INCLUDE_PATH', DS . 'usr' . DS . 'lib' . DS . 'php' . DS . 'cake'&#41;;
&#125;

You are now using the common cake core.  When a new update comes out to the library, just replace the files in /usr/lib/php/cake with the new version!
If you want to go a step further and share some of your own models, views, controllers, behaviors, helpers, or components between applications you can use a similar technique.  For example create a folder in &#8216;/usr/lib/php/models&#8217; and put all your common models in that folder, then in your application open up bootstrap.php and add the following:

1
2
3
$modelPaths = array&#40;
DS . 'usr' . DS . 'lib' . DS . 'php' . DS . 'models'
&#41;;

Like I said the same thing can be done with views, controllers, helpers, behaviors or components.  The cookbook says how!  This is pretty handy for when you find a bug in a model and you have several applications using that model!

]]></description>
			<content:encoded><![CDATA[<p>If you are deploying several Cake applications server wide you may want to keep the Cake core in one central place so that it can be easily updated.  In order to do this place the cake core directory somewhere on your server where is can be accessed.  For example /usr/lib/php/cake.  Then, when you create your applications open up your root index file  and replace the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CAKE_CORE_INCLUDE_PATH'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CAKE_CORE_INCLUDE_PATH'</span><span style="color: #339933;">,</span> ROOT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>with this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CAKE_CORE_INCLUDE_PATH'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CAKE_CORE_INCLUDE_PATH'</span><span style="color: #339933;">,</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'usr'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'lib'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'php'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'cake'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You are now using the common cake core.  When a new update comes out to the library, just replace the files in /usr/lib/php/cake with the new version!</p>
<p>If you want to go a step further and share some of your own models, views, controllers, behaviors, helpers, or components between applications you can use a similar technique.  For example create a folder in &#8216;/usr/lib/php/models&#8217; and put all your common models in that folder, then in your application open up bootstrap.php and add the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$modelPaths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'usr'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'lib'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'php'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'models'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span class="keyword">Like I said the same thing can be done with views, controllers, helpers, behaviors or components.  The <a href="http://book.cakephp.org/view/36/Additional-Class-Paths">cookbook says how</a>!  This is pretty handy for when you find a bug in a model and you have several applications using that model!<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2008/12/10/share-your-cakephp-core-server-wide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tip: Build Forms Quicker in CakePHP</title>
		<link>http://www.laneolson.ca/2008/12/09/tip-build-forms-quicker-in-cakephp/</link>
		<comments>http://www.laneolson.ca/2008/12/09/tip-build-forms-quicker-in-cakephp/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 02:53:39 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Form Helper]]></category>

		<guid isPermaLink="false">http://laneolson.wordpress.com/?p=8</guid>
		<description><![CDATA[The form helper is great and has saved me many hours of manually coding form inputs.  This article will show you how to use the $form-&#62;inputs() function to automagically build a form.]]></description>
			<content:encoded><![CDATA[<p>I first saw use of the $form->inputs() function for building a form on <a href="http://snook.ca/archives/cakephp/contact_form_cakephp/" target="_blank">Johnathan Snook&#8217;s blog post about creating a contact form</a>.  After investigating further into the function, I found that it can be used to save quite a bit of time.</p>
<h3>Automagic Forms!</h3>
<p>Using $form->input() is great, but when you have many fields or you are just creating a quick form there is a better way!  The key is to set up your database table structure properly.  If you follow the <a href="http://book.cakephp.org/complete/22/CakePHP-Conventions" target="_blank">naming conventions</a> creating a form is as easy as using the following code in the view (this is for a Contact model&#8230; obviously):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>php
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Contact'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inputs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Save Contact&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is pretty nice, but what happens if you have fields that you don&#8217;t want the user to fill out?  Use the blacklist!  The following code removes the fieldset and legend along with the fields id, created, and modified.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>php
<span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fieldset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'legend'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$blacklist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'created'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'modified'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Contact'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$blacklist</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Save Contact&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Not too bad.  Fully functional form in 5 lines of code!  But wait&#8230; There&#8217;s more!  You may still want to customize the form a little more to add certain options to some fields.  This can be done with the $inputs array.  Basically all you have to do is add a key in the $inputs array with the field name, and then assign the optional attributes as you would if you were using the $form->input() function.  Here is an example of what the array would look like to add a hint after the &#8220;first name&#8221; field.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'fieldset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'legend'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'first_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'after'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'i.e. Lane'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The only drawback with assigning the optional attributes is that now the $inputs array must contain a key for every input you want to have a form field for.  I&#8217;m sure that there is a better way to do this, but you could do something like this in the controller:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>php
<span style="color: #000000; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$schema</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Contact</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">schema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$schema</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$field</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'schema'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and then in the view do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fieldset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'legend'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$schema</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$inputs</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'after'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'i.e. Lane'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This method would get the job done, but would really only be practical if you had a whole bunch of fields.  Otherwise creating each $form->input() might be the way to go if you have to assign some options.  Nonetheless,  I&#8217;ve found that you can bypass having to set the $options array for specific fields by following the naming conventions, and when that is the case there is nothing better than $form->inputs()!</p>
<h3>Related Links</h3>
<ul>
<li><a href="http://book.cakephp.org/complete/182/Form" target="_blank">The Form Helper</a></li>
<li><a href="http://api.cakephp.org/class_form_helper.html" target="_blank">Form Helper API</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2008/12/09/tip-build-forms-quicker-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Beginning CakePHP: The MVC Mindset</title>
		<link>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/</link>
		<comments>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 20:35:59 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://laneolsonblog.wordpress.com/?p=3</guid>
		<description><![CDATA[The manual for CakePHP has a pretty basic explanation of what MVC is.  This article will explain things in a little more detail because the MVC pattern is crucial when it comes to using CakePHP.  If you are not familiar with the MVC design pattern, read on...]]></description>
			<content:encoded><![CDATA[<p>The manual for CakePHP has a <a href="http://book.cakephp.org/view/10/Understanding-Model-View-Controller" target="_blank">pretty basic explanation</a> of what MVC is.  This article will explain things in a little more detail because the MVC pattern is crucial when it comes to using CakePHP.  If you are not familiar with the MVC design pattern, read on&#8230;</p>
<h3>What is MVC anyway?</h3>
<p>MVC stands for Model-View-Controller.  MVC is a well known design pattern that basically keeps that data handling, application flow, and presentation seperate from each other.  At first it may seem a little confusing, but it makes coding much easier and efficient.  I will explain each aspect of the MVC below relative to a simple application to manage contacts.</p>
<h4>The Model</h4>
<p>The model is responsible for handling all of the data.  In CakePHP each model usually corresponds to a table in the database.  So if in our database we have a table named &#8220;contacts&#8221; to store all of our contacts then there will be a model named &#8220;Contact&#8221;.  The plural and singular convention is important but I will not be explaining it at this time.  I recommend you read the <a href="http://book.cakephp.org/complete/22/CakePHP-Conventions">CakePHP conventions</a> before you actually start using Cake because they will save you time and headaches.  You can just trust me that the model name should be singular, and the database table should be plural.  Here is a rough example of what a model would look like in CakePHP.  This file would be stored at &#8220;/app/models/contact.php&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000000; font-weight: bold;">class</span> Contact <span style="color: #000000; font-weight: bold;">extends</span> AppModel 
<span style="color: #009900;">&#123;</span> 
     <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Contact'</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is actually all you need to get started with a basic model in CakePHP.  As you can see the class Contact extends the class AppModel so it inherits all of the functions that AppModel has access to.  The AppModel has functions like find, save, delete, etc that allow you to interact with the database.  The model should be responsible for all interaction with the database.</p>
<p>So in review, anything to do with manipulating data should be done in the model.  Whether it is a query to CRUD (create, read, update, delete) operations to the database you should be doing it from the model.  Extending Cake&#8217;s AppModel class allows you to use the built in functions for CRUD operations so that you should never have to write an SQL query again!  If you would like to learn more about the model and the functions available to it check out the <a href="http://book.cakephp.org/complete/66/Models" target="_blank">Models page in the Cookbook</a>.</p>
<h4>The View</h4>
<p>The view is what it sounds like.  It is the presentation layer, everything that has to do with displaying the data is done in the view.  In cakePHP the view is set up in templates that displays data that is obtained from the model.  The view should be the <strong>only</strong> place where HTML resides.  This makes things a lot easier when it comes to modifying the look and feel of an application without having to touch the guts.  The view is basically a template and in CakePHP they have the extension &#8220;ctp&#8221;.  Here is what and example of what the view would look like to display a list of contacts&#8230; lets call it &#8220;index.ctp&#8221;.  In an application setting this file would be stored in the folder &#8220;/app/views/contacts&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;contacts&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contacts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$contact</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;span class=&quot;contact-name&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Contact'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/span&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></td></tr></table></div>

<p>Of course this is an extremely basic example of printing out a list of contact names.  In the view we would definitely want to present more information in a nicer way&#8230; the best part is in order to do that the only file we will have to edit is the view.  You&#8217;re probably wondering where the $contacts variable came from, this brings us to the controller.</p>
<h4>The Controller</h4>
<p>Lastly, there is the controller.  The controller manages the flow of the application.  It is responsible for handling requests and passing the proper information for the given request.  The controller consists of several functions, called &#8220;actions&#8221;, which determine what should be done.  Each action (usually) has an associated view.  So for example, if we have an action called &#8220;index&#8221; in our controller we will need a view called &#8220;index.ctp&#8221; that displays the information for the given action.  The view in the example above uses a variable called $contact.  That variable is set using the controller.  Here is an example that ties in with the above view, this file would be &#8220;/app/controllers/contacts_controller.php&#8221; in an application setting.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> ContactsController <span style="color: #000000; font-weight: bold;">extends</span> AppController
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Contacts'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$contacts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Contact</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contacts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The function in the controller above asks the Contact model to find all of the entries in the associated contacts table and then assigns it to the variable $contacts.   Afterwards the controller sets the variable $contacts so that the view can now use the $contacts variable.  The built in libraries will automatically search for the file &#8220;/app/views/contacts/index.ctp&#8221; and use it to display the information passed. The best convention is to keep the code in the controllers to a minimum and instead put the bulk of the code for manipulating the data into the model (see <a href="http://www.littlehart.net/atthekeyboard/2007/04/27/fat-models-skinny-controllers/" target="_blank">Fat Models, Skinny Controllers</a> by Chris Hartjes).</p>
<h2>Putting It All Together</h2>
<p>So there you have it, we just created an extremely simple Model, View, and Controller for listing contacts.  Of course from here you would build on creating actions in the controller for add, view, edit, etc. and views to go with those actions.  But hopefully this gives you some insight in to how it works.  If you have never followed this design practice before you are probably wondering some of the <a href="http://book.cakephp.org/view/12/Benefits">benefits of the MVC</a> practice.  It may not be immediately apparent, but there are several.  First of all, separating code in this method allows the basic functionality of the code to be reused over and over again.  When it comes to modifying the way the application looks you only have to edit the views instead of all of the code.  This allows someone who is familiar with HTML to customize the look of things without having to search endless lines of code that may not be familiar to them.  It also makes debugging much easier because you can pinpoint exactly where the problems may be coming from.  If the data isn&#8217;t correct, it must be the model&#8230; If something is funky with the way it is displayed, it has got to be the view&#8230; and so on.  This is of course just one of the great design patterns used by Cake and there are several others that will save time and headaches.</p>
<h2>Additional MVC Articles</h2>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Model-view-controller#PHP" target="_blank">MVC on Wikipedia </a></li>
<li><a href="http://book.cakephp.org/complete/10/Understanding-Model-View-Controller" target="_blank">The CakePHP Manual MVC documentation</a></li>
<li><a href="http://book.cakephp.org/complete/66/Models" target="_blank">Cookbook entry for Models</a></li>
<li><a href="http://book.cakephp.org/complete/94/Views" target="_blank">Cookbook entry for Views</a></li>
<li><a href="http://book.cakephp.org/complete/49/Controllers" target="_blank">Cookbook entry for Controllers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

