<?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>Tony Thomas &#187; gtd</title>
	<atom:link href="http://anthonygthomas.com/tag/gtd/feed/" rel="self" type="application/rss+xml" />
	<link>http://anthonygthomas.com</link>
	<description>Father to two, husband to one, web developer and musician.</description>
	<lastBuildDate>Fri, 11 Nov 2011 14:00:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Roll Your Own CakePHP Components</title>
		<link>http://anthonygthomas.com/2009/05/22/roll-your-own-cakephp-components/</link>
		<comments>http://anthonygthomas.com/2009/05/22/roll-your-own-cakephp-components/#comments</comments>
		<pubDate>Fri, 22 May 2009 13:54:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[components]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[gtd]]></category>
		<category><![CDATA[mcv]]></category>

		<guid isPermaLink="false">http://anthonygthomas.com/?p=103</guid>
		<description><![CDATA[As someone who is not formally trained as a programmer, I often understand concepts long before actually putting them into practice. Don&#8217;t Repeat Yourself (DRY) seems simple enough. Of course I don&#8217;t want to repeat myself while programming. Who wants to dig through lines of code to find a litte snippet of logic you once [...]]]></description>
			<content:encoded><![CDATA[<p>As someone who is not formally trained as a programmer, I often understand concepts long before actually putting them into practice. <em>Don&#8217;t Repeat Yourself </em>(DRY) seems simple enough. Of course I don&#8217;t want to repeat myself while programming. Who wants to dig through lines of code to find a litte snippet of logic you once wrote? Still, when you&#8217;re pressed for time, sometimes you just have to <em>Get Things Done</em> (GTD). So best practices go through the window and you hammer out some spaghetti code so you can move on.</p>
<p>It&#8217;s only recently, since I&#8217;ve slowed down to finally understand how to write <a href="http://cakephp.org" target="_blank" onclick="pageTracker._trackPageview('/outgoing/cakephp.org?referer=');">CakePHP</a> <a href="http://book.cakephp.org/view/63/Introduction" onclick="pageTracker._trackPageview('/outgoing/book.cakephp.org/view/63/Introduction?referer=');">Components</a>, that I&#8217;ve realized that DRY enhances GTD. Now that I&#8217;ve got it all straight in my head, I&#8217;m a component <em>evangelist</em>.</p>
<p>At this point I&#8217;m going to assume that you&#8217;re familiar with MCV architecture and its benefits. Once in a while there&#8217;s a bit of logic that you find yourself coding into a controller that you realize you&#8217;re going to want to use in other controllers. It&#8217;s not specific to the model. That&#8217;s where components come in. They&#8217;re bits of logic that can be used by more than one controller. Let&#8217;s look at a simple one that converts mm/dd/yyyy dates to a Unix timestamp.</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DateComponent <span style="color: #000000; font-weight: bold;">extends</span> Object <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> mkTimestamp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sentdate</span><span style="color: #339933;">,</span> <span style="color: #000088;">$senttime</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$thedate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sentdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$senttime</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000088;">$thetime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$senttime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$newdate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thetime</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$thetime</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000088;">$newdate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$thedate</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$newdate</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The function itself is not all that complicated. The thing is that I&#8217;m going to want to use this where ever I need to convert dates. Since the component is called &#8220;Date&#8221;, it&#8217;s named with the CakePHP convention for components: DateComponent. The file is called date.php and is saved in <code>app/controllers/components</code>.</p>
<p>Now we have to tell our controller(s) to use it. I&#8217;m using it across several controllers, so I&#8217;m adding it to <code>app/app_controller.php</code> by adding it to the <code>$components</code> var: <code>var $components = array('Date');</code></p>
<p>Now I can access it in any controller like so: <code>$tmsmp = $this->Date->mkTimestamp($thedate, $thetime);</code></p>
<p>The whole point is that components don&#8217;t have to be complex, they&#8217;re just code you want to reuse that doesn&#8217;t necessary apply to one model. They will save you time and clean up your controllers.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://anthonygthomas.com/2009/05/22/roll-your-own-cakephp-components/" rel="bookmark" title="May 22, 2009">Roll Your Own CakePHP Components</a></li>
<li><a href="http://anthonygthomas.com/2009/07/22/simple-security-in-cakephp/" rel="bookmark" title="July 22, 2009">Simple Security in CakePHP</a></li>
<li><a href="http://anthonygthomas.com/2010/05/24/an-unexpected-problem-with-cakephp-and-email-elements/" rel="bookmark" title="May 24, 2010">An Unexpected Problem with CakePHP and Email Elements</a></li>
<li><a href="http://anthonygthomas.com/2008/12/10/use-functions-from-other-controllers-while-maintaining-mvc-architecture-in-cakephp/" rel="bookmark" title="December 10, 2008">Use Functions from Other Controllers While Maintaining MVC Architecture in CakePHP</a></li>
<li><a href="http://anthonygthomas.com/2010/02/18/cakephp-containable-behavior-is-your-friend/" rel="bookmark" title="February 18, 2010">CakePHP: Containable Behavior is Your Friend</a></li>
</ul>
<p><!-- Similar Posts took 28.288 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://anthonygthomas.com/2009/05/22/roll-your-own-cakephp-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

