<?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>DerEuroMark</title>
	<atom:link href="http://www.dereuromark.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dereuromark.de</link>
	<description>A blog about Frameworks (CakePHP), MVC, Snippets, Tips and more</description>
	<lastBuildDate>Sat, 28 Aug 2010 12:47:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Useful hidden functions</title>
		<link>http://www.dereuromark.de/2010/08/28/useful-hidden-functions/</link>
		<comments>http://www.dereuromark.de/2010/08/28/useful-hidden-functions/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 12:47:04 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Common]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=325</guid>
		<description><![CDATA[There are some handy cake functions you might not have used or even heard of, yet. Models save, saveField, saveAll(), deleteAll(), &#8230; return usually true/false or the record itself. Sometimes it would be nice to know how many records have been deleted/modified. $this-&#62;Model-&#62;getAffectedRows&#40;&#41;; This will do the trick. The integer result can then be used [...]]]></description>
			<content:encoded><![CDATA[<p>There are some handy cake functions you might not have used or even heard of, yet.</p>
<h3>Models</h3>
<p>save, saveField, saveAll(), deleteAll(), &#8230; return usually true/false or the record itself.<br />
Sometimes it would be nice to know how many records have been deleted/modified.</p>
<pre class="php" style="font-family:monospace;"><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;">getAffectedRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>This will do the trick. The integer result can then be used in the flash message.</p>
<h3>Logging</h3>
<p>If you want to log certain events, you can easily use the build in functions:</p>
<pre class="php" style="font-family:monospace;">CakeLog<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'geocode'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Address \''</span><span style="color: #339933;">.</span><span style="color: #000088;">$address</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\' has been geocoded'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>In this example every time the geocode webservice geocodes an address string, it will be logged in /tmp/logs/geocode.log. Quite handy in some cases &#8211; especially for debugging purposes.</p>
<h3>Other</h3>
<p>It helps to open the core files and have a look inside. You might discover some functions you might have written yourself although it is available in the core.<br />
Examples are string.php with tokenize() and insert() or set.php with filter(), flatten() or countDim().</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/08/28/useful-hidden-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Development vs. Productive Setup</title>
		<link>http://www.dereuromark.de/2010/08/17/development-vs-productive-setup/</link>
		<comments>http://www.dereuromark.de/2010/08/17/development-vs-productive-setup/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 00:25:41 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Deployment]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=311</guid>
		<description><![CDATA[If you want to deploy your cakephp app you usually have to change a few lines of code. But we want to minimize that. My example setup: - development: Windows (>= Vista) - productive: Linux (Debian) Database Setup Create a library file and extend this from your DATABASE_CONFIG. This way you can let your app [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to deploy your cakephp app you usually have to change a few lines of code. But we want to minimize that.</p>
<p>My example setup:<br />
- development: Windows (>= Vista)<br />
- productive: Linux (Debian)</p>
<h3>Database Setup</h3>
<p>Create a library file and extend this from your DATABASE_CONFIG. This way you can let your app automatically select the correct database var at runtime.</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BASE_CONFIG <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$environments</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sandbox'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'online'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$default</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * switch between local and live site(s) automatically by domain
	 * or manually by Configure::write('Environment.name')
	 * 2009-05-29 ms
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$environment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEnvironmentName</span><span style="color: #009900;">&#40;</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: #000088;">$environment</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$environment</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_merge"><span style="color: #990000;">array_merge</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$environment</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">test</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">test</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'prefix'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'zzz_'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
 	<span style="color: #000000; font-weight: bold;">function</span> getEnvironmentName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$environment</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Environment.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><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$environment</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$server</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>String<span style="color: #009900;">&#41;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">environments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'environment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'environment'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$server</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$environment</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$environment</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Example for your database.php:</p>
<pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Lib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'BaseConfig'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DATABASE_CONFIG <span style="color: #000000; font-weight: bold;">extends</span> BASE_CONFIG <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$default</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #666666; font-style: italic;">// localhost</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'default'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'environment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'driver'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mysqli'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'persistent'</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;">'host'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cake_app'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'xyz_'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'encoding'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'utf8'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$sandbox</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #666666; font-style: italic;">// online test</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'environment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'test.domain.com'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cake_app_test'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$online</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>	<span style="color: #666666; font-style: italic;">// online productive</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'online'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'environment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.domain.com'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cake_app'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The same config file on all 3 locations will now select the one corresponding to the environment url.<br />
You could override this, though, by using Configure::write(&#8216;Environment.name&#8217;) &#8211; but this is not neccessary if the domain doesnt change too often.</p>
<h3>Debug Mode</h3>
<p>Put this in your core.php (it should ALWAYS be 0 by default!):</p>
<pre class="php" style="font-family:monospace;">Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Enhancement
</span><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Debug mode is 0 for all online sites and 2 for your local development site.</p>
<h3>Custom &#8220;Overrides&#8221;</h3>
<p>There are many variables you need to switch from local to live apps (google.maps key, other api keys, email server credentials, &#8230;)<br />
The quick-and-dirty solution would be something like this:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Foo'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'www.domain.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Foo'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</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;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Foo'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Very fast very ugly.</p>
<p>A little bit cleaner is using two config files: &#8220;configs.php&#8221; and &#8220;configs_private.php&#8221;. The second one is not synched (or in SVN) &#8211; it contains passwords and environment specific content.<br />
Include it in your bootstrap AFTER you included the default configs.</p>
<p>As i just mentioned, it has another upside: Beeing a environment based tmp-file it does not store any sensitive information in the SVN (or whatever other backup tool you use).<br />
This way you can easily set up configuration &#8220;stubs&#8221; in your configs.php and insert the passwords in your private config file.</p>
<h3>Folder Setup and .htaccess</h3>
<p>You can use the same folder setup:<br />
- cake<br />
- app<br />
- vendors<br />
With &#8230;/app/webroot/ as &#8220;visible folder&#8221; and &#8230;/app/webroot/index.php as dispatching script.</p>
<p>In my htaccess file i use &#8220;!^localhost&#8221; to avoid redirects locally:</p>
<pre class="php" style="font-family:monospace;">RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^www\<span style="color: #339933;">.</span> <span style="color: #009900;">&#91;</span>NC<span style="color: #009900;">&#93;</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^localhost <span style="color: #009900;">&#91;</span>NC<span style="color: #009900;">&#93;</span>
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.%{HTTP_HOST}/$1 [R=301,L]</span></pre>
<p>This way the same file can be used for both environments.</p>
<h3>Linux/Windows differences</h3>
<p>Use the constant DS (= Directory Separator) anywhere you can. If you hardcode it with / (Linux) oder \ (Windows) it might break some code. Usually it doesn&#8217;t, but it is cleaner to use DS anyway.</p>
<p>Example:</p>
<pre class="php" style="font-family:monospace;"><a href="http://www.php.net/file_put_contents"><span style="color: #990000;">file_put_contents</span></a><span style="color: #009900;">&#40;</span>TMP<span style="color: #339933;">.</span><span style="color: #0000ff;">'xyz'</span><span style="color: #339933;">.</span>DS<span style="color: #339933;">.</span><span style="color: #0000ff;">'file.txt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'some text'</span><span style="color: #009900;">&#41;</span></pre>
<p>Set up a WINDOWS constant in your bootstrap.php &#8211; this way you can dynamically decide what function to use or to not use specific lines of code (like console &#8220;exec&#8221; commands).</p>
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">PHP_OS</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'WIN'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WINDOWS'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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>
	<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WINDOWS'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Example:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>WINDOWS<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//whatever</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<a href="http://www.php.net/exec"><span style="color: #990000;">exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$something</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<h3>Uploading your changes</h3>
<p>I use a shell tool i wrote &#8211; which uses &#8220;rsynch&#8221; and only uploads the delta (changes made so far).<br />
Make sure you DON&#8217;T upload any tmp stuff or even worse, override environment based files like uploads, cached files etc.<br />
Using batch scripts you can usually exclude some directories.</p>
<h3>Final Tips</h3>
<p>Remember that linux needs explicit folder permissions. So you need to manually (or per batch script) set /tmp to 777 recursivly as well as any other folder which you want to write into from your scripts.</p>
<p>Also clear the cache after updating! Otherwise your model relations as well as some constants will be outdated and cause errors or complete failure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/08/17/development-vs-productive-setup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to implement Captchas properly</title>
		<link>http://www.dereuromark.de/2010/08/09/how-to-implement-captchas-properly/</link>
		<comments>http://www.dereuromark.de/2010/08/09/how-to-implement-captchas-properly/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 12:15:23 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Captcha]]></category>
		<category><![CDATA[Spam Protection]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=233</guid>
		<description><![CDATA[What is a captcha? They protect forms on websites from spammers and bots (@see Wikipedia for details). The main idea: Display some kind of code a human can easily read and submit but a computer can not. How NOT to implement captchas This part is even more important, because there is not only one correct [...]]]></description>
			<content:encoded><![CDATA[<h3>What is a captcha?</h3>
<p>They protect forms on websites from spammers and bots (@see <a href="http://en.wikipedia.org/wiki/CAPTCHA">Wikipedia</a> for details). The main idea: Display some kind of code a human can easily read and submit but a computer can not.</p>
<h3>How NOT to implement captchas</h3>
<p>This part is even more important, because there is not only one correct way, but even more wrong ways to go here.</p>
<p>Don&#8217;t use sessions unless you really have to.<br />
Pages that use something like $_['Captcha']['field'] and override this one on every form, really freak me out! It makes working with two or more tabs impossible, because they override each others captcha values all the time, resulting in a ****** mess.<br />
You could use an array like structure, but your captcha session array can get pretty big in a short amount of time.</p>
<p>More helpful are captchas which use the current form and some hash values based on the fields + current timestamp. It should not be possible to &#8220;guess&#8221; or &#8220;calculate&#8221; the hash value. So there is no way to use future &#8220;hashs&#8221;.<br />
You could use older hashs (like from last week), though. But most bots are programmed to just post right away. They would have to save possible valid &#8220;scenarios&#8221; for later usage.<br />
Mabye we can come up with something fail-prove later on. For now we want to effectively prevent spam bots to submit their crap without annoying normal users.<br />
The second part is always the hardest. Me &#8211; for example &#8211; I really hate those image captchas which you can <u>barely</u> read. I often times have to repeat or reload it twice in order to succeed. Annoying comes not even close.</p>
<h3>Why do we need captchas</h3>
<p>First of all, they make sure that there is no bot (automatic program) posting &#8220;spam&#8221; or whatever.<br />
But sometimes you just want to add captchas to prevent users from doing some action too often (like friendship requests in a community site etc).</p>
<p>A good example what happens if you dont use captchas, is <a href="http://bakery.cakephp.org/">bakery.cakephp.org</a>.<br />
Some articles have like 36 comments, of which all 36 are SPAM. This is a desaster.<br />
And in this case you even have to be logged in to submit a comment.<br />
The argument that forms for which you need to be logged in don&#8217;t need captchas is not contemporary anymore.</p>
<h3>Passive Captchas</h3>
<p>I already talked about using some hash values based on the fields + current timestamp.<br />
This can be used to generate passive captchas. They are similar to the cake core component &#8220;security&#8221; which adds some hidden fields to make sure that the fields have not been tempered with.</p>
<p>Both are invisible to the user &#8211; they dont even notice the passive captcha. Bot bots will soon discover that they are facing a wall.<br />
The difference is, that passive captchas should only valid in a specific timeframe. Too fast (less than 2 seconds) is usually a sign for the work of a bot &#8211; humans cannot type that fast.<br />
Too late (> 12 hours?) means you need to revalidate anyway, so we would render the form invalid as well. Well designed forms will keep the posted content, so nothing gets lost.</p>
<p>Another aspect to improve security is to use other user specific fields for the hash value like browser agent (cannot change during posts, but is less secure because it can be modified), IP address (can only be modified by very skilled hackers and therefore is pretty secure), &#8230;</p>
<h3>Active Captchas</h3>
<p>Those are the most commonly used ones. Users either have to read an image, calculate numbers or<br />
interpret a sentence. The first one is not suitable for handicapped people.</p>
<p>Usually they are build as extension on top of passive captchas. We first validate the passive one. If the form is OK, we then validate the user input. If validation passes we render the captcha valid.</p>
<p>I decided to use math captchas. They keep you mentally fit and do what they are supposed to. The only important issue is to make it easy enough. I saw pages using / [division] or numbers above 20 in multiplication or even above 100 for summation &#8211; which is total overhead.<br />
But other ones could be used as well &#8211; simply by changing configuration settings.</p>
<h3>Captcha Behavior</h3>
<p>Ok, to sum it up, we want captchas that<br />
- don&#8217;t annoy users<br />
- protect as good as absolutely neccessary<br />
- can be used with tabs<br />
- can be easily implemented and configured</p>
<p>The idea is, that we want to add a single form field as well as attach a single behavior to our model.<br />
Thats all there is to it.<br />
That&#8217;s why a behaviour in combination with a helper does the trick perfectly.</p>
<p>The code is in my github tools plugin:<br />
<a href="http://github.com/dereuromark/tools/tree/master/models/behaviors/">captcha behavior</a><br />
<a href="http://github.com/dereuromark/tools/tree/master/views/helpers/">captcha helper</a></p>
<p>Helper usage (in the view):</p>
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Captcha</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// or input('Modelname') if model is different from the form model</span></pre>
<p>Behavior usage (in the controller):</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Behaviors</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Captcha'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre>
<p>Pretty straight forward, isn&#8217;t it?</p>
<h3></h3>
<p>Current weaknesses (apart from its strenghts):<br />
- possible &#8220;hash extraction&#8221; with unlimited use of those valid hashs (session or db to prevent?)</p>
<h3>Final notes</h3>
<p>Right now it is mainly used for math captchas (active captchas) and just passive captchas.<br />
Feel free to update the missing parts like providing more captcha types (image, sentence, &#8230;) or processing types (session, cookie, &#8230;).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/08/09/how-to-implement-captchas-properly/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extended core validation rules</title>
		<link>http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/</link>
		<comments>http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 21:56:05 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=203</guid>
		<description><![CDATA[I18n Translation Some translate the rules in the view &#8211; but it usually creates redundancy. In some projects this might be intentional. I like to keep the error messages centralized, though. For that, you can just override the core translation rule &#8211; add this to app_model.php: /** * Overrides the Core invalidate function from the [...]]]></description>
			<content:encoded><![CDATA[<h3>I18n Translation</h3>
<p>Some translate the rules in the view &#8211; but it usually creates redundancy. In some projects this might be intentional. I like to keep the error messages centralized, though.</p>
<p>For that, you can just override the core translation rule &#8211; add this to app_model.php:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Overrides the Core invalidate function from the Model class
 * with the addition to use internationalization (I18n and L10n)
 * @param string $field Name of the table column
 * @param mixed $value The message or value which should be returned
 * @param bool $translate If translation should be done here
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> invalidate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</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;">$value</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#41;</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//TODO: make more generic?</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$value</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;">$translate</span>?__<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># string %s %s string, trans1, trans2
</span>			<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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;">$value</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;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># string %s %s string, trans1, trans2
</span>			<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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;">$value</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;">$value</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>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># string %s string, trans1
</span>			<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</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;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</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: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Usage (some examples):</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'notEmpty'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notEmpty'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'valErrEmpty'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// some short form to translate</span>
			<span style="color: #666666; font-style: italic;">//normal sentences would of course work, too</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'pwd'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'between'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'between'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'valErrBetweenCharacters %s %s'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// short form string with variables</span>
			<span style="color: #666666; font-style: italic;">// maybe resulting in something like &quot;Between 6 and 30 chars&quot; as defined in locale.po</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'code'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'maxLength'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'maxLength'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The code cannot be longer than %s chars'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// normal text with a variable</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #339933;">...</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>I like the short forms like &#8220;valErrBetweenCharacters %s %s&#8221; &#8211; they don&#8217;t have a certain &#8220;grammar&#8221;, so if you want to change the translation result &#8220;Between %s and %s chars&#8221; to &#8220;Please insert between %s and %s chars&#8221; this can be done with one change in locale.po instead of changing all xxx places you used this rule.</p>
<h3>Custom validation rules</h3>
<p>Here is one example how to use a custom validation rule with one param (custom rules can be placed in the model for local validation or the app_model for global validation):</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'pwd_repeat'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'validateIdentical'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'validateIdentical'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pwd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// we want to compare this to &quot;pwd&quot;</span>
			<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'valErrPwdNotMatch'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in app_model.php:</span>
<span style="color: #009933; font-style: italic;">/**
 * checks if the content of 2 fields are equal
 * Does not check on empty fields! Return TRUE even if both are empty (secure against empty in another rule)!
* //TODO: make it more generic with Model.field syntax
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> validateIdentical<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$compareWith</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$compareWith</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <a href="http://www.php.net/array_shift"><span style="color: #990000;">array_shift</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<h3>Multiple validation rules</h3>
<p>The Cookbook states:<br />
&#8220;By default CakePHP tries to validate a field using all the validation rules declared for it and returns the error message for the last failing rule&#8221;<br />
I don&#8217;t really like this default behavior &#8211; what sense does it make to validate all if only the last error can be returned anyway. But as long as &#8220;last&#8221; is not true by default, we have to manually set it:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'login'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'loginRule-1'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'alphaNumeric'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'...'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'last'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
         <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'loginRule-2'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'minLength'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'...'</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>The rules are validated top down. So if the first one fails, it will now return the error right away. Usually the following validation rules rely on a positive result of the predecessor. At least, thats how you should arrange your rules.</p>
<p>One example:<br />
[email] (in this order &#8211; each with a specific error message)<br />
- notEmpty<br />
- email<br />
- undisposable (custom &#8211; per vendor)<br />
- notBlocked (custom &#8211; per webservice)</p>
<p>As you can see, it would not make sense to check all of them every time. It really slows down the validation &#8211; especially if the email address is not even valid. So we first want to check the simple stuff and then move on to the advanced (and sometimes more time consuming) rules. All rules get &#8220;last&#8221;=>true to achieve that.</p>
<h3>Other quite handy custom translation rules</h3>
<p>They can be put into app_model.php:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * checks a record, if it is unique - depending on other fields in this table (transfered as array)
 * example in model: 'rule' =&gt; array ('uniqueRecord',array('belongs_to_table_id','some_id','user_id')),
 * if all keys (of the array transferred) match a record, return false, otherwise true
 * @param ARRAY other fields
 * TODO: add possibity of deep nested validation (User -&gt; Comment -&gt; CommentCategory: UNIQUE comment_id, Comment.user_id)
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> validateUnique<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fieldName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$fieldValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// equals: $this-&gt;data[$this-&gt;alias][$fieldName]</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldValue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// return true, if nothing is transfered (check on that first)</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$conditions</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$fieldName</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$fieldValue</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Model.field =&gt; $this-&gt;data['Model']['field']</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.id !='</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#41;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// add ONLY if some content is transfered (check on that first!)</span>
			<span style="color: #000088;">$conditions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Validation'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// add ONLY if some content is transfered (check on that first!</span>
			<span style="color: #000088;">$conditions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Validation'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;"># manual query! (only possible on edit)
</span>			<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</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: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$conditions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dependingField</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</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: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// validateUrl and validateUndisposable are to come</span></pre>
<h3>More</h3>
<p>See the Cookbook for <a href="http://book.cakephp.org/view/125/Data-Validation">details on that matter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/07/19/extended-core-validation-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redirect Root Domain to WWW Subdomain</title>
		<link>http://www.dereuromark.de/2010/07/13/redirect-root-domain-to-www-subdomain/</link>
		<comments>http://www.dereuromark.de/2010/07/13/redirect-root-domain-to-www-subdomain/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 21:40:43 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[WebDevelopment]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=263</guid>
		<description><![CDATA[The Problem &#8220;When you have two different addresses pointing to the same page, like www.example.com/offers.html and example.com/offers.html, many search engines (or so we are led to believe) will treat those two URLs as two separate pages. When you, as a human, see those two pages and notice they are identical, you will automatically realise (correctly) [...]]]></description>
			<content:encoded><![CDATA[<h3>The Problem</h3>
<p>&#8220;When you have two different addresses pointing to the same page, like www.example.com/offers.html and example.com/offers.html, many search engines (or so we are led to believe) will treat those two URLs as two separate pages. When you, as a human, see those two pages and notice they are identical, you will automatically realise (correctly) that they are actually the same page. Apparently, the search engines do not make this assumption, and will regard those as different pages with duplicate content.&#8221;<br />
(source: <a href="http://www.thesitewizard.com/apache/redirect-domain-www-subdomain.shtml">www.thesitewizard.com</a>)</p>
<p>I personally think that apart from the SEO problem there shouldn&#8217;t be two different urls to the same content in the first place.</p>
<h3>So what do we do?</h3>
<p>The bad thing to do would be to disable one of the domains (root or www).<br />
We want to select one (www.example.com) as default domain and if someone just enters &#8220;example.com&#8221; he will be automatically redirected to our default domain.<br />
Again &#8211; the bad thing would be to use meta redirects. We want to use so called permanent redirects ( code 301) in order to notify search engines and browsers about the reason we want to redirect.</p>
<h3>Using Mod Rewrite</h3>
<p>For the cake app to be available per &#8220;www.example.com&#8221; (and a 301 redirect from mydomain.de) you just need to modify the htaccess file in the /app/webroot/ folder:</p>
<pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mod_rewrite.c&gt;
	<span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
&nbsp;
	<span style="color: #00007f;">RewriteCond</span> %{HTTP_HOST} !^www\. [NC]
	<span style="color: #00007f;">RewriteCond</span> %{HTTP_HOST} !^localhost [NC]
	<span style="color: #00007f;">RewriteRule</span> ^(.*)$ http://www.%{HTTP_HOST}/$<span style="color: #ff0000;">1</span> [R=<span style="color: #ff0000;">301</span>,L]
&nbsp;
	<span style="color: #00007f;">RewriteCond</span> %{REQUEST_FILENAME} !-d
	<span style="color: #00007f;">RewriteCond</span> %{REQUEST_FILENAME} !-f
	<span style="color: #00007f;">RewriteRule</span> ^(.*)$ index.php?url=$<span style="color: #ff0000;">1</span> [QSA,L]
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</pre>
<p>The &#8220;localhost&#8221; part is not neccessary &#8211; but it prevents your local htaccess file to redirect on your development computer. That is if you have your htaccess file in svn or git and therefore both versions (local and stage) are the same.</p>
<p>The reason why i did not lay out the other direction (www to root) is simple: I don&#8217;t think this makes sense. Most people not so comfortable with the internet usually type the complete address anyway &#8211; they just expect a &#8220;www&#8221; in front of the domain for the main website.<br />
And with the internet getting more and more complex, some guidelines should just remain. One is that you don&#8217;t use root domains for websites. (I could go into details here &#8211; about other side effects like &#8220;technical&#8221; cookie problems occuring with root domain sites etc &#8211; but i will leave that out for now)</p>
<h3>Using Apache Directives</h3>
<p>This is way faster because the server doesn&#8217;t have to open the htaccess files for it. It can directly use what it has to have available anyway. </p>
<p>Inside /etc/apache2/sites-available/ there should be your domain file.<br />
Between <code>&lt;Directory&gt;</code> and <code>&lt;/Directory&gt;</code> you can add the above lines.<br />
To actually increase the performance we now need to disable htaccess files for this domain.<br />
Add <code>AllowOverride None</code> directly above your new lines. This prevents the server from looking for, opening and processing htaccess files.</p>
<p>Note: Usually this is only available on root servers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/07/13/redirect-root-domain-to-www-subdomain/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>User Add Console Script</title>
		<link>http://www.dereuromark.de/2010/07/04/user-add-console-script/</link>
		<comments>http://www.dereuromark.de/2010/07/04/user-add-console-script/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 22:36:56 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Cake1.3]]></category>
		<category><![CDATA[Console]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=190</guid>
		<description><![CDATA[If you have a fresh setup of your app and no users in the database, its not easy to &#8220;register&#8221; a new one (with the admin role and everything). The other case would be if you changed the security salt. Now you need new passwords, as well. The second part is not yet covered by [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a fresh setup of your app and no users in the database, its not easy to &#8220;register&#8221; a new one (with the admin role and everything).<br />
The other case would be if you changed the security salt. Now you need new passwords, as well.</p>
<p>The second part is not yet covered by my script &#8211; the first one is, though.<br />
The idea is to insert an admin user in order to login for the first time. All you need is a name, an email and a password.</p>
<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> UserShell <span style="color: #000000; font-weight: bold;">extends</span> Shell <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$tasks</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//TODO: refactor (smaller sub-parts)</span>
	<span style="color: #000000; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Component'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'AuthExt'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AuthExtComponent<span style="color: #009900;">&#40;</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>
			App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Component'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Auth'</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;">Auth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AuthComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Set \'username\'?'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'y'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'n'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'y'</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: #000088;">$continue</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Abort'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$continue</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'n'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Username (2 characters at least)'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">while</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Password (2 characters at least)'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Role</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/is_object"><span style="color: #990000;">is_object</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$roles</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</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><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				pr <span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$roleIds</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Role'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$roleIds</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: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/method_exists"><span style="color: #990000;">method_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'roles'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$roles</span> <span style="color: #339933;">=</span> User<span style="color: #339933;">::</span><span style="color: #004000;">roles</span><span style="color: #009900;">&#40;</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><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				pr <span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$roleIds</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Role'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$roleIds</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$roles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Role found (either no table, or no data)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please insert a role manually'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pwd</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pwd</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'active'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #009900;">&#41;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'role_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$role</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		pr <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</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;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">in</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Continue? '</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'y'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'n'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'n'</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: #000088;">$continue</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Not Executed!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</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;">hr</span><span style="color: #009900;">&#40;</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User inserted! ID: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User could not be inserted (email, nick duplicate!!!)'</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: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<h3>Notes</h3>
<p>The code obviously needs some refactoring. And its written for my applications &#8211; it might not work out of the box with other cake apps. Maybe we could make it more generic, as well.</p>
<p>It assumes that you have either AuthExt or Auth running for Authentication, that you have a User and a Role model and that the fields are &#8220;username&#8221;, &#8220;email&#8221;, &#8220;password&#8221; and &#8220;role_id&#8221;. Additionally it sets &#8220;active&#8221; to 1 &#8211; this field is checked in my login procedure.<br />
But this script should be fairly easily adjustable for your needs.</p>
<p>Ideas for further improvement are:<br />
- user add (for adding)<br />
- user edit (for editing specific user/password)<br />
- user reset (to reset all passwords to 123 or whatever)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/07/04/user-add-console-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subversion and multiple CakePHP apps</title>
		<link>http://www.dereuromark.de/2010/07/02/subversion-and-multiple-cakephp-apps/</link>
		<comments>http://www.dereuromark.de/2010/07/02/subversion-and-multiple-cakephp-apps/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 10:20:16 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=41</guid>
		<description><![CDATA[A few days ago i already wrote about how to set up a svn for cake. In this second part i will cover the aspect of managing several apps. The Problem We have several apps, each with its own cake and vendor folder. If you need to update from 1.3.1 to 1.3.2 you would need [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago i already wrote about how to set up a svn for cake.<br />
In this second part i will cover the aspect of managing several apps.</p>
<h3>The Problem</h3>
<p>We have several apps, each with its own cake and vendor folder. If you need to update from 1.3.1 to 1.3.2 you would need to do that for each of your cake cores. Woudn&#8217;t it be nice if you changed one and all others would be updated automatically?</p>
<h3>The Solution</h3>
<p>Lets say, our apps have the following svn rep. urls:<br />
<code>http://123.456.789.000/app1</code><br />
and<br />
<code>http://123.456.789.000/app2</code></p>
<p>Both have the same structure:<br />
/root/trunk/app/&#8230;<br />
(/root/trunk/vendors/&#8230;)<br />
(/root/trunk/cake/&#8230;)</p>
<p>Both vendor and cake folder we now want to substitute with our shared folders</p>
<h3>Sharing Cake Core and Vendors</h3>
<p>We now put our cake core in svn under the following url:<br />
<code>http://123.456.789.000/cake13</code></p>
<p>and vendors:<br />
<code>http://123.456.789.000/vendors</code></p>
<p>In both apps, we need to edit the properties for the /trunk folder with the following schema:<br />
<code>svn:externals svn_url local_folder</code><br />
Result:<br />
svn:externals http://123.456.789.000/cake13 cake<br />
svn:externals http://123.456.789.000/vendors vendors</p>
<p>If you update the cake13 or vendors svn, both apps will get the updated version.<br />
Especially if you have more than 2 apps, this will be very helpful.</p>
<h3>Extending to other folders</h3>
<p>/app/plugins is a very common folder where you should use svn:externals instead of just copying your plugins to all apps. One change in any plugin version will affect the other ones.</p>
<p>/app/libs can have subfolders, too. So /app/libs/lib/&#8230; could contain a &#8220;library of libs&#8221; for all apps.<br />
The same goes for /app/views/helpers, /app/controller/components, /app/models/behaviors/ etc</p>
<p>Your test cases should be in &#8220;lib&#8221; folders then, too.<br />
E.g:<br />
/app/controller/components/lib/upload.php<br />
/app/tests/cases/components/lib/upload.php<br />
Both inside those shared lib folders which are attached with svn:externals (in this case parent folder &#8220;components&#8221;).</p>
<p>This might make sense in some cases. Most of the time, though, it&#8217;s better to create some kind of &#8220;tools&#8221; plugin where you can combine all your &#8220;library&#8221; files (common libs, helpers, components, behaviors, &#8230;).<br />
And the test cases are easier managed, as well.</p>
<h3>Final tips</h3>
<p>You don&#8217;t necessarily need seperate svns for each external folder. They could all reside in one.<br />
E.g with &#8220;common&#8221; svn:</p>
<p>svn:externals http://123.456.789.000/common/cake13 cake<br />
svn:externals http://123.456.789.000/common/subfolder1/subfolder2/vendors vendors</p>
<p>Any svn subfolder is valid</p>
<h3>NEW: Use github reps as external svns</h3>
<p>Now you can include github reps in svn reps. Its as easy as including<br />
<code>svn:externals https://svn.github.com/[user]/[repository]</code><br />
in your folder properties.</p>
<p>This is especially useful, if you want to include one of the many cake plugins available at github without manually including it (changes would not be adopted).<br />
Thats how i include my tools plugin in all my apps:<br />
<code>svn:externals https://svn.github.com/dereuromark/tools tools</code><br />
(in my /app/plugins folder properties)</p>
<p>Note: Currently it does not seem to work the other way around. Although planned to be available very soon, it didn&#8217;t work for me committing to such a git-svn rep externally included.<br />
But most of the time, a simple checkout is more than enough. If you need to change the rep, you can do that with git itself and then update the svn rep (it will get the changes then anyway).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/07/02/subversion-and-multiple-cakephp-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Code-Completion Console Script</title>
		<link>http://www.dereuromark.de/2010/06/28/code-completion-console-script/</link>
		<comments>http://www.dereuromark.de/2010/06/28/code-completion-console-script/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 21:39:54 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Cake1.3]]></category>
		<category><![CDATA[Code Completion]]></category>
		<category><![CDATA[Console]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=184</guid>
		<description><![CDATA[Many good IDEs for Webdevelopment cannot automatically understand the structure of a framework and its classes/objects. They need a small file initializing the objects so that they know in which context they appear. E.g.: $this->Session (Session Helper) its impossible for the IDE to know that the the helpers are inside the View class. Same goes [...]]]></description>
			<content:encoded><![CDATA[<p>Many good IDEs for Webdevelopment cannot automatically understand the structure of a framework and its classes/objects.<br />
They need a small file initializing the objects so that they know in which context they appear.</p>
<p>E.g.:<br />
$this->Session (Session Helper)<br />
its impossible for the IDE to know that the the helpers are inside the View class.<br />
Same goes for controller components and model behaviors</p>
<p>I wrote a little script which produces mockup code. My IDE &#8220;PHPDesigner&#8221; works very well with it.</p>
<h3>Usage</h3>
<p>Just type <code>cake cc</code> inside the cake shell.</p>
<h3>Code</h3>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Core'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Folder'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Core'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'File'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Code Completion
 * 2009-12-26 ms
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> CcShell <span style="color: #000000; font-weight: bold;">extends</span> Shell <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AutoComplete Dump'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//TODO: ask for version (1.2 etc - defaults to 1.3!)</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span> <span style="color: #339933;">=</span> APP<span style="color: #339933;">.</span><span style="color: #0000ff;">'code_completion__.php'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># get classes
</span>		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">models</span><span style="color: #009900;">&#40;</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;">components</span><span style="color: #009900;">&#40;</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;">helpers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//TODO: behaviors</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># write to file
</span>		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_dump<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'...done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @deprecated
	 * now use: Configure::listObjects()
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> __getFiles<span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Folder<span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$handleFiles</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$handle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$handleFiles</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> extractPathInfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</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><a href="http://www.php.net/mb_strrpos"><span style="color: #990000;">mb_strrpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <a href="http://www.php.net/mb_strlen"><span style="color: #990000;">mb_strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># ending with _ like test_.php
</span>				<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</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;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Inflector<span style="color: #339933;">::</span><span style="color: #004000;">camelize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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: #b1b100;">return</span> <span style="color: #000088;">$files</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _getFiles<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> App<span style="color: #339933;">::</span><span style="color: #004000;">objects</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># lib
</span>    <span style="color: #000088;">$paths</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#41;</span>App<span style="color: #339933;">::</span><span style="color: #004000;">path</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$libFiles</span> <span style="color: #339933;">=</span> App<span style="color: #339933;">::</span><span style="color: #004000;">objects</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paths</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: #0000ff;">'lib'</span> <span style="color: #339933;">.</span> DS<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$plugins</span> <span style="color: #339933;">=</span> App<span style="color: #339933;">::</span><span style="color: #004000;">objects</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'plugin'</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$plugins</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$plugins</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$plugin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$pluginFiles</span> <span style="color: #339933;">=</span> App<span style="color: #339933;">::</span><span style="color: #004000;">objects</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span> App<span style="color: #339933;">::</span><span style="color: #004000;">pluginPath</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$plugin</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$type</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'s'</span> <span style="color: #339933;">.</span> DS<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginFiles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pluginFiles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                  <span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$t</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//&quot;$plugin.$type&quot;;</span>
              <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_merge"><span style="color: #990000;">array_merge</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #339933;">,</span> <span style="color: #000088;">$libFiles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_unique"><span style="color: #990000;">array_unique</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$appIndex</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_search"><span style="color: #990000;">array_search</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'App'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$files</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: #000088;">$appIndex</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$appIndex</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># no test/tmp files etc (helper.test.php or helper.OLD.php)
</span>    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #990000;">strpos</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[\da-zA-Z_]+$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</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: #b1b100;">return</span> <span style="color: #000088;">$files</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> models<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//$files = App::objects('component', null, false);</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getFiles<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//$files = $this-&gt;_getFiles(COMPONENTS);</span>
&nbsp;
		<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> LF<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;?php'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** model start ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'class AppModel extends Model {'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prepModels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** model end ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> components<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getFiles<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'component'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> LF<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;?php'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** component start ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'class AppController extends Controller {'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prepComponents<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** component end ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> helpers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getFiles<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'helper'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> LF<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;?php'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** helper start ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'class AppHelper extends Helper {'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prepHelpers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/*** helper end ***/'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> _prepModels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
	/**
	* '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
	*/
	public $'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';
'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'	function __construct() {'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
		$this-&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = new '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'();'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> _prepComponents<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
	/**
	* '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Component
	*/
	public $'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';
'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'	function __construct() {'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
		$this-&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = new '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Component();'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> _prepHelpers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># new ones
</span>		<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
	/**
	* '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Helper
	*/
	public $'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';
'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'	function __construct() {'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'
		$this-&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = new '</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Helper();'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># old ones
</span>		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">/*
		foreach ($files as $name) {
		$res .= '
		$'.lcfirst($name).' = new '.$name.'Helper();
		';
		}
		$res .= LF;
		*/</span>
&nbsp;
		<span style="color: #000088;">$res</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'	}'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> _dump<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/file"><span style="color: #990000;">File</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;?php exit();'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'//Add in some helpers so the code assist works much better'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'//Printed: '</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d.m.Y, H:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?&gt;'</span><span style="color: #339933;">.</span>LF<span style="color: #339933;">;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<h3>Result</h3>
<p>The file will look like:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AppModel <span style="color: #000000; font-weight: bold;">extends</span> Model <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	* Address
	*/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$Address</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">...</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Address</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Address<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	* AclComponent
	*/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$Acl</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">...</span>
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AclComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">class</span> AppHelper <span style="color: #000000; font-weight: bold;">extends</span> Helper <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	* AjaxHelper
	*/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$Ajax</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">...</span>
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ajax</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AjaxHelper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">...</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<h3>Final notes</h3>
<p>Feel free to update the TODOs and send me the improved file. A test file would be awesome, too, i guess <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For LF constants see my article about &#8220;bootstrap goodies&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/06/28/code-completion-console-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Social Bookmark Helper</title>
		<link>http://www.dereuromark.de/2010/06/27/bookmark-helper/</link>
		<comments>http://www.dereuromark.de/2010/06/27/bookmark-helper/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 18:26:17 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Social Bookmarks]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=177</guid>
		<description><![CDATA[If you want to add social bookmarks to your cake projects, this helper will do the trick. Usage # Defaults: $this-&#62;Bookmark-&#62;getBookmarks&#40;&#41;; &#160; # All: $this-&#62;Bookmark-&#62;getBookmarks&#40;null, null, $this-&#62;Bookmark-&#62;availableBookmarks&#40;&#41;&#41;; &#160; # Custom: $custom = &#40;array&#41;Configure::read&#40;'Bookmarks'&#41;; //or $custom = array&#40;'Twitter', 'Facebook', ...&#41;; $this-&#62;Bookmark-&#62;getBookmarks&#40;null, null, $custom&#41;; Link to GIT-Rep cakephp-bookmark-helper Updates on bookmarks? Are there any outdated bookmarks &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to add social bookmarks to your cake projects, this helper will do the trick.</p>
<h3>Usage</h3>
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Defaults:
</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Bookmark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBookmarks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># All:
</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Bookmark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBookmarks</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Bookmark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">availableBookmarks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Custom:
</span><span style="color: #000088;">$custom</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#41;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bookmarks'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//or</span>
<span style="color: #000088;">$custom</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Twitter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Facebook'</span><span style="color: #339933;">,</span> <span style="color: #339933;">...</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;">Bookmark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBookmarks</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$custom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<h3>Link to GIT-Rep</h3>
<p><a href="http://github.com/dereuromark/cakephp-bookmark-helper">cakephp-bookmark-helper</a></p>
<h3>Updates on bookmarks?</h3>
<p>Are there any outdated bookmarks &#8211; or even some missing ones? Write me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/06/27/bookmark-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helper? Component? Lib?</title>
		<link>http://www.dereuromark.de/2010/06/26/helper-component-lib/</link>
		<comments>http://www.dereuromark.de/2010/06/26/helper-component-lib/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 14:05:23 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Cake1.3]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Helper]]></category>
		<category><![CDATA[Lib]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=207</guid>
		<description><![CDATA[Some ideas what to use if you want to add some additional feature. Feel free to comment on this below. Level of Independence We need to ask ourselves if this feature needs to interact with other cake elements, like controller, some components, models, &#8230; If it needs to save to the session, or if it [...]]]></description>
			<content:encoded><![CDATA[<p>Some ideas what to use if you want to add some additional feature.<br />
Feel free to comment on this below.</p>
<h3>Level of Independence</h3>
<p>We need to ask ourselves if this feature needs to interact with other cake elements, like controller, some components, models, &#8230;</p>
<p>If it needs to save to the session, or if it needs some controller functionality, it will have to be a component.<br />
With initialize(&#038;$controllerReference) and startup(&#038;$controllerReference) this is very easy to accomplish.</p>
<p>But with Cake13 libs have been introduced. Not every piece of &#8220;controller&#8221; code necessarily needs to be component anymore.<br />
So if you retrieve an RSS feed or get the weather from a weather channel web service you could just make a clean and independent lib class. No need to extend the cake object or even pass the controller reference. Less memory and dependency is a good thing. And its easier to test, anyway.</p>
<p>Helpers are used if the result is in relation to the view &#8211; in other words if it gets printed/echoed right away. If you want to retrieve some web service information and save it to the database use a component instead.</p>
<h3>Database related?</h3>
<p>Often times we need to adjust some model data, we either use a component first and then pass it to the model or we use beforeValidate() and beforeSave() in the model. Same goes for the other direction (from model to controller): afterFind() or a component call afterwards.<br />
This is fine for custom changes. As soon as it could be something useful for several models, it might make sense to build a behavior. The code gets cleaner and your models more powerful.</p>
<p>Examples would be:<br />
&#8220;Last Editor/Last Change&#8221;, &#8220;Geocoding&#8221;, &#8220;Auto-Capitalize first letter of name/title&#8221;, &#8220;Format/Localize Date/Time&#8221;, &#8230;</p>
<h3>Reducing code redundancy</h3>
<p>Now that we have a vague understanding where to use what type of tool, we should think about cutting down the redundancy.<br />
Lets say we use the vendor class &#8220;phpThump&#8221;. We would have to write two wrappers. one for the helper (display images in the view) and one for the component (uploading images and resizing), maybe even for some behavior (validating + uploading + resizing). This wrapper handles default values from Configure::read() and other cake related settings.<br />
In this scenario we should build one single library in /libs, maybe called &#8220;phpthumb_lib.php&#8221;.<br />
Here we put our wrapper with our custom functions.<br />
Then we build a helper (view side) as well as a component or a behavior (controller side). They will import and use the library file. This is a cleaner approach because changes in the library class will be available in all files it is used in.<br />
Bonus: The main library file is easier to test. And therefore testing the other classes afterwards is easier, too.</p>
<p>Generally speaking, all web services should be some kind of library file (some would make a data source out of it). It doesn&#8217;t matter then if we use it in components or helpers, because it will fit either way.<br />
A helper in a controller, though, is not really a nice thing.</p>
<p>Thats &#8211; by the way &#8211; something i don&#8217;t like about the core helpers. They have functionality which is often useful in the controller (replacing a timestamp with localized date in session flash messages).<br />
So there should be a library called &#8220;Time&#8221; or whatever which is then extended or used in the helper.<br />
But, if needed, it can be used in the controller, as well. Same goes for &#8220;Text&#8221; and &#8220;Number&#8221;.</p>
<h3>Plugin or not?</h3>
<p>If your feature is not site-specific but very generic it probably makes sense to build a plugin.<br />
This way all other apps can easily use the same plugin. Additionally, test cases, assets etc are all combined in one folder &#8211; clean and extendable.</p>
<p>Examples:<br />
A bookmark helper usually can be used in several apps, whereas a custom helper with two functions for this one app will not be very useful anywhere else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2010/06/26/helper-component-lib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
