<?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>TechnoGeeks &#187; CakePHP</title>
	<atom:link href="http://techno-geeks.org/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://techno-geeks.org</link>
	<description>A fusion of technology, music, and geekyness.</description>
	<lastBuildDate>Sat, 04 Sep 2010 08:17:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Goodbye CakePHP</title>
		<link>http://techno-geeks.org/2010/03/goodbye-cakephp/</link>
		<comments>http://techno-geeks.org/2010/03/goodbye-cakephp/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 13:57:21 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Employment]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=403</guid>
		<description><![CDATA[It&#8217;s been a long journey and it has finally come to an end. Due to professional changes in my life, it is time that I stop using a PHP based MVC framework for web applications. Where will I go, you ask? Its simple &#8211; Ruby on Rails! While the aim of this post is to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long journey and it has finally come to an end. Due to professional changes in my life, it is time that I stop using a PHP based MVC framework for web applications. Where will I go, you ask? Its simple &#8211; Ruby on Rails! While the aim of this post is to not convert people who use CakePHP to start using something else, I do plan on elaborating some of my reasonings for this switch.</p>
<p><strong>1. My New Job</strong><br />
It cannot make much more sense than this. I recently got a new job (started Feb 1st). The place I work is a pure ruby shop and support several financial based web applications. I had 10 days to learn the syntax/basics of Ruby and loved every minute of it. I was ready for something new and fresh in my life.</p>
<p><strong>2. Maturity</strong><br />
Rails was first! As a result, the project has matured more and seems to have a lot more support.</p>
<p><strong>3. Package / Plugin management</strong><br />
Ruby has RubyGems, which is amazing. CakePHP just released something to handle plugins, but I have a feeling it is not a full package manager (think apt!).</p>
<p><strong>4. Other Little Differences</strong><br />
Several small differences make it a better choice. I found a recent <a href="http://www.slideshare.net/gautamrege/rails-vs-cakephp">presentation</a> that quickly explains a few of them.</p>
<p>That being said, PHP with CakePHP and Ruby with Rails both have their pros and cons. Each framework will always have its place in my heart. Hopefully I will have time soon to setup a rails environment and start pushing through some personal projects! Until then, I will continue gaining experience at work whilst I triage the problems that I was hired to fix <img src='http://techno-geeks.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2010/03/goodbye-cakephp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CakePHP eCommerce Done Right: Mocha Shopping Cart</title>
		<link>http://techno-geeks.org/2009/09/cakephp-ecommerce-done-right-mocha-shopping-cart/</link>
		<comments>http://techno-geeks.org/2009/09/cakephp-ecommerce-done-right-mocha-shopping-cart/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 19:57:12 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mocha]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=262</guid>
		<description><![CDATA[ few months back I posted on CakePHP eCommerce and the lack of good solutions. Well that is now no longer the case. I would like to be one of the first people to introduce you to Mocha Shopping Cart. We have been working day and night (literally) to come up with a solution for [...]]]></description>
			<content:encoded><![CDATA[<p><div class="wp-caption alignleft" style="width: 171px"><a href="http://mochashoppingcart.com"><img alt="Mocha Shopping Cart" src="http://mochashoppingcart.com/img/mocha-logo-white.gif" title="Mocha Shopping Cart" width="161" height="83" /></a><p class="wp-caption-text">Mocha Shopping Cart</p></div>A few months back I posted on CakePHP eCommerce and the lack of good solutions. Well that is now no longer the case. I would like to be one of the first people to introduce you to <a href="http://mochashoppingcart.com">Mocha Shopping Cart</a>. We have been working day and night (literally) to come up with a solution for the small and medium sized business owners to sell their products online easily. We also have geared Mocha to be extend-able and easy for CakePHP designers and developers to add in their own twists for clients.</p>
<p>Mocha is written entirely in CakePHP and jQuery and is certainly the best CakePHP eCommerce app out there. Why do I say that?</p>
<p><strong>Mocha Shopping Cart is&#8230;</strong></p>
<ul>
<li>Fully hosted, secure, pre-installed</li>
<li>Very easy to set up and configure</li>
<li>Themable and Modular (Plug ins docs in the works)</li>
<li>Works with most popular payment and shipping apis (UPS, USPS, Authorize.net, Paypal, etc.)</li>
<li>Charges no transaction fees and no setup fees</li>
</ul>
<p>If you still don&#8217;t believe me, Mocha is offering a limited time only <a href="http://bit.ly/HTSbo">Pre-Launch coupon code</a> that gives you the first month <strong>FREE</strong>. You can try it for yourself and see how you like it. I think you will agree that it beats CakeCart and BakeSale, HANDS DOWN! And no, Mocha is not free (after the first 30 days). The costs help to speed up development, pay for server and hosting fees, and pay for that cup of coffee that us developers need when its 8 AM and we are having to talk with &lt;insert_API_name_here&gt; first thing in the morning.</p>
<p>Official Website: <a href="http://mochashoppingcart.com">Mocha Shopping Cart</a><br />
Blog with coupon code: <a href="http://bit.ly/HTSbo">Get a free trial of Mocha Shopping Cart</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/09/cakephp-ecommerce-done-right-mocha-shopping-cart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy JSON with CakePHP and jQuery</title>
		<link>http://techno-geeks.org/2009/08/easy-json-with-cakephp-and-jquery/</link>
		<comments>http://techno-geeks.org/2009/08/easy-json-with-cakephp-and-jquery/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 19:00:52 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=250</guid>
		<description><![CDATA[I am not sure if this is the fanciest way to get the job done but it works REALLY well. What job am I referring to? I am referring to outputting data as JSON in a CakePHP view and then parsing it in a jQuery AJAX response. Its fairly easy to accomplish with the JSON [...]]]></description>
			<content:encoded><![CDATA[<p>I am not sure if this is the fanciest way to get the job done but it works REALLY well. What job am I referring to? I am referring to outputting data as JSON in a CakePHP view and then parsing it in a jQuery AJAX response. Its fairly easy to accomplish with the <a href="http://www.json.org/js.html">JSON parser provided by json.org</a>. Scroll to the bottom and download/include <a href="http://www.json.org/json2.js">json2.js</a>.</p>
<p>Controller example:</p>
<pre class="brush: php;">
				$sizeArray = getimagesize('/path/to/really_cool_image.jpg');

				$outputArray = array(
					'file' =&gt; 'really_cool_image.jpg',
					'width' =&gt; $sizeArray[0],
					'height' =&gt; $sizeArray[1],
				);

				$this-&gt;set('result', $outputArray);

				$this-&gt;render(null, 'ajax');
</pre>
<p>AJAX View example:</p>
<pre class="brush: php;">
Configure::write('debug', 0);
echo $javascript-&gt;object($result);
</pre>
<p>jQuery AJAX response handler example:</p>
<pre class="brush: jscript;">
	  function(response, status) {
	  		imageDetails = JSON.parse(response);
	  		imageName = imageDetails.file;
	  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/08/easy-json-with-cakephp-and-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CakePHP: Including Models inside your Controller</title>
		<link>http://techno-geeks.org/2009/05/models-inside-controller/</link>
		<comments>http://techno-geeks.org/2009/05/models-inside-controller/#comments</comments>
		<pubDate>Tue, 26 May 2009 16:07:38 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=176</guid>
		<description><![CDATA[There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to [...]]]></description>
			<content:encoded><![CDATA[<p>There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to happen to me a lot. This post is basically a re-summary of <a href="http://groups.google.com/group/cake-php/browse_thread/thread/137c57b4eb010317">Gwoo&#8217;s response</a> in the <a href="http://groups.google.com/group/cake-php/browse_thread/thread/137c57b4eb010317">CakePHP Google Groups discussion</a>.</p>
<p><strong>1. Controller::loadModel();</strong><br />
This method actually calls ClassRegistry::init() and saves the instance of the model as a parameter in the controller.</p>
<p>Example:</p>
<pre class="brush: php;">
Controller::loadModel('Car');
$cars = $this-&gt;Car-&gt;find('all');
</pre>
<p><strong>2. ClassRegistry::init()</strong><br />
This method creates an instance of the Model and returns it for use.</p>
<p>Example:</p>
<pre class="brush: php;">
$car = ClassRegistry::init('Car');
$cars = $car-&gt;find('all');
</pre>
<p><strong>3. App:import()</strong> (not recommended)<br />
This method only includes the file. This requires you to create an instance each time.</p>
<p>Example:</p>
<pre class="brush: php;">
App::import('Car');
$car = new Car();
$cars = $car-&gt;find('all');
</pre>
<p>As you can see this is really simple and basic stuff. However, I felt the need to re-post this information. If for nothing else, I will be using this as a reference point for myself <img src='http://techno-geeks.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Update 5/27/09: Miles Johnson has a nice write up on this <a href="http://www.milesj.me/blog/read/16/loading-models-specific-to-certain-actions">on his blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/05/models-inside-controller/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CakePHP Character Encoding Checklist</title>
		<link>http://techno-geeks.org/2009/05/cakephp-character-encoding-checklist/</link>
		<comments>http://techno-geeks.org/2009/05/cakephp-character-encoding-checklist/#comments</comments>
		<pubDate>Fri, 15 May 2009 20:04:01 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=171</guid>
		<description><![CDATA[I was having some issues today with character encoding and I thought I&#8217;d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn&#8217;t hurt [...]]]></description>
			<content:encoded><![CDATA[<p>I was having some issues today with character encoding and I thought I&#8217;d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn&#8217;t hurt either.</p>
<p>1. Database Config</p>
<p>Add &#8216;encoding&#8217; => &#8216;utf8&#8242; to app/config/database.php</p>
<pre class="brush: php;">
	var $default = array(
		'driver' =&gt; 'mysql',
		'persistent' =&gt; false,
		'host' =&gt; 'localhost',
		'login' =&gt; 'user',
		'password' =&gt; 'password',
		'database' =&gt; 'database',
		'encoding' =&gt; 'utf8'
	);
</pre>
<p>2. Make sure your layout has the charset line in the <head></p>
<pre class="brush: php;">
	&lt;head&gt;
	 	&lt;?php echo $html-&gt;charset(); ?&gt;
	 	Other elements...
	&lt;/head&gt;
</pre>
<p>3. This one is set by default, but make sure App.encoding is set in app/config/core.php</p>
<pre class="brush: php;">
/**
 * Application wide charset encoding
 */
	Configure::write('App.encoding', 'UTF-8');
</pre>
<p>And thats it!</p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/05/cakephp-character-encoding-checklist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peep! A friendly GTD compliant productivity tool for you.</title>
		<link>http://techno-geeks.org/2009/05/peep/</link>
		<comments>http://techno-geeks.org/2009/05/peep/#comments</comments>
		<pubDate>Mon, 11 May 2009 14:33:47 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Peep!]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=164</guid>
		<description><![CDATA[
About a year ago Rick (http://www.rickguyer.com) had me read the book Getting Things Done by David Allen. In his book, Allen teaches you how to use his strategies and procedures on managing your tasks (or actions) in a very organized and well thought out fashion. His strategy is unique and calls for a very unique [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://trypeep.com"><img alt="" src="http://trypeep.com/img/peep-logo-plain.png" class="alignleft" width="146" height="80" /></a><br />
About a year ago Rick (<a href="http://www.rickguyer.com">http://www.rickguyer.com</a>) had me read the book <a href="http://en.wikipedia.org/wiki/Getting_things_done">Getting Things Done</a> by David Allen. In his book, Allen teaches you how to use his strategies and procedures on managing your tasks (or actions) in a very organized and well thought out fashion. His strategy is unique and calls for a very unique tool to accompany his GTD system. When I originally read his book, Rick and I spent several days searching for a different program, service, or web app that would allow us to incorporate Allen&#8217;s GTD system in an electronic fashion. After a solid week of effort I gave up after finding nothing that I felt was easy to use and completely GTD compliant. </p>
<p>Finally, after over a year, we decided to create this perfect GTD compliant web service that will help people, like us, be able to incorporate Allen&#8217;s system and become more productive. <a href="http://trypeep.com">Peep!</a> is &#8220;a simple and easy-to-use tool to help you focus and start getting things done.&#8221; I highly suggest you all try it and let me know what you think.</p>
<p>Head over to <a href="http://trypeep.com">http://trypeep.com</a> and get started today!</p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/05/peep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coming Soon: Peep!</title>
		<link>http://techno-geeks.org/2009/04/coming-soon-peep/</link>
		<comments>http://techno-geeks.org/2009/04/coming-soon-peep/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 21:30:20 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[DualTech]]></category>
		<category><![CDATA[Peep!]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=148</guid>
		<description><![CDATA[We&#8217;ve been working hard at the office lately in efforts to launch a new application that will be aimed towards increasing one&#8217;s productivity. The app will follow the principles in the book Getting Things Done by David Allen. Check out the sneak peek below!

]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been working hard at the office lately in efforts to launch a new application that will be aimed towards increasing one&#8217;s productivity. The app will follow the principles in the book <a href="http://www.davidco.com/">Getting Things Done by David Allen</a>. Check out the sneak peek below!</p>
<p><img src="http://techno-geeks.org/wp-content/uploads/2009/04/peep-300x239.jpg" alt="peep" title="peep" width="300" height="239" class="aligncenter size-medium wp-image-149" /></p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/04/coming-soon-peep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authenticating a CakePHP App Against a CakePHP Webservice</title>
		<link>http://techno-geeks.org/2009/03/authenticating-a-cakephp-app-against-a-cakephp-webservice/</link>
		<comments>http://techno-geeks.org/2009/03/authenticating-a-cakephp-app-against-a-cakephp-webservice/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 15:42:57 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=87</guid>
		<description><![CDATA[Update 03/27: I fixed the code formatting and added the Cakeservice Behavior
Rick and I figured out a nice solution to having one CakePHP application authenticate a user (and thus acodeessing data) from a different CakePHP application setup as an XML webservice. The authenticating app POSTs an XML string to the web service, which is automatically [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 03/27</strong>: I fixed the code formatting and added the Cakeservice Behavior</p>
<p>Rick and I figured out a nice solution to having one CakePHP application authenticate a user (and thus acodeessing data) from a different CakePHP application setup as an XML webservice. The authenticating app POSTs an XML string to the web service, which is automatically parsed by CakePHP and put into $this->data. It is then used in a custom xmlLogin() function that uses the Auth Component to log the user in. This solution handles all cookies appropriately so you wont have to worry about much there.</p>
<p>This solution would most likely be better if it were a DataSource instead of a ModelBehavior. We chose a ModelBehavior first because we are not familiar with writing custom data sources yet and wanted a simple, quick, and effective solution. This is a work in progress so I hope to expand on it more later and eventually have it act as a data source instead.</p>
<p>I am assuming:</p>
<p>    * You have the latest CakePHP (1.2.1) installed and connected to the database<br />
    * You have the Auth Component installed and configured to the defaults<br />
    * You have at least one user record with a valid password hash<br />
    * You have the XML Helper enabled in your app_controller.php</p>
<p>Step 1: Make CakePHP Act as a Webservice (service side)</p>
<p>1. Add similar code in app/config/routes.php:</p>
<pre class="brush: php;">
Router::mapResources(array('users', 'acodeounts'));
Router::parseExtensions();
</pre>
<p>2. Add RequestHandler to your components array in app_controller:</p>
<pre class="brush: php;">
var $components = array('RequestHandler');
</pre>
<p>3. Add an xml folder to each view directory, with .ctp files for each action you would like to have. This is an example of app/views/users/xml/index.ctp:</p>
<pre class="brush: xml;">
&lt;users&gt;
&lt;?php echo $xml-&gt;serialize($users); ?&gt;
&lt;/users&gt;
</pre>
<p>See the CakeBook for more information on step 1.</p>
<p>Step 2: Setup CakePHP Errors for Reponse XML (service side)</p>
<p>We decided to use the built in custom errors system inside Cake for displaying Reponse XML for Webservice calls. I know that not all XML responses will be errors (it should actually be the opposite) but I like the way CakePHP handles custom error pages so we leveraged this functionality for this topic.</p>
<p>1. Create app/views/errors/serviceresponse.ctp</p>
<pre class="brush: xml;">
&lt;responses&gt;
  &lt;response&gt;
    &lt;code&gt;&lt;?php echo $errorCode; ?&gt;&lt;/code&gt;
    &lt;message&gt;&lt;?php echo $errorMessage; ?&gt;&lt;/message&gt;
  &lt;/response&gt;
&lt;/response&gt;
</pre>
<p>2. Create app/app_error.php</p>
<pre class="brush: php;">
&lt;?php
Class AppError extends ErrorHandler {

    function serviceResponse($params) {
        $this-&gt;controller-&gt;set('errorCode', $params['errorCode']);
        $this-&gt;controller-&gt;set('errorMessage', $params['errorMessage']);
        $this-&gt;controller-&gt;layout = 'xml/default';
        $this-&gt;_outputMessage('serviceresponse');
    }
}
?&gt;
</pre>
<p>Now this can be called by using this code in the controller as needed</p>
<pre class="brush: php;">
$this-&gt;cakeError('serviceResponse', array('errorMessage' =&gt; $errorMessage, 'errorCode' =&gt; $errorCode));
</pre>
<p>Step 3: Create Custom Login Function (service side)</p>
<p>1. Add xmlLogin() to app/controllers/users_controller.php</p>
<pre class="brush: php;">
    function xmlLogin() {
        // Verify request is from a webservice
        if ($this-&gt;Session-&gt;check('webserviceRequest')) {
            $this-&gt;Session-&gt;delete('webserviceRequest');

            // Default response
            $errorMessage = 'Login is required to acodeess this resource';
            $errorCode = 'login_required';

            // Check to see if data was posted
            if (!empty($this-&gt;data)) {
                // Parse the XML object into an array
                $data = Set::reverse($this-&gt;data);

                // Hash the passwords
                $this-&gt;data = $this-&gt;Auth-&gt;hashPasswords($data['Users']);

                // Login
                if($this-&gt;Auth-&gt;login($this-&gt;data)) {
                    $errorMessage = 'Logged in';
                    $errorCode    = 'auth_sucodeess';
                } else {
                    $errorMessage = 'Invalide username or password';
                    $errorCode = 'auth_fail';
                }

            }

            // Generate the response
            $this-&gt;cakeError('serviceResponse', array('errorMessage' =&gt; $errorMessage, 'errorCode' =&gt; $errorCode));
        }
    }
</pre>
<p>2. Modify the default login()</p>
<pre class="brush: php;">
    function login() {
        if ($this-&gt;Session-&gt;check('webserviceRequest')) {
            $this-&gt;Session-&gt;delete('webserviceRequest');

            $this-&gt;cakeError('serviceResponse', array('errorMessage' =&gt; 'Login is required to acodeess this resource', 'errorCode' =&gt; 'login_required'));
        }
    }
</pre>
<p>3. Add in the webserviceRequest check to beforeFilter() in app/app_controller.php</p>
<pre class="brush: php;">
    function beforeFilter() {
        if ($this-&gt;params['url']['ext'] == 'xml') {
            $this-&gt;Session-&gt;write('webserviceRequest', true);
        }
    }
</pre>
<p>4. Allow xmlLogin() to bypass Auth. Add the following to the top of your users_controller.php:</p>
<pre class="brush: php;">
	var $preAuth = array();
	function beforeFilter() {
		parent::beforeFilter();

		$this-&gt;preAuth['password'] = $this-&gt;data['User']['password'];
		$this-&gt;Auth-&gt;allow('loginTest');
	}
</pre>
<p>Step 4: Install the Cakeservice Behavior (app side)</p>
<p>1. Download cakeservice.php and put it in app/models/behaviors/cakeservice.php</p>
<pre class="brush: php;">
&lt;?php
/**
 * A simple WebService Behavior class that eases requests to foreign pages
 * Entirely PHP based, does not require and modules or cURL.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * Based on the WebserviceModel authored by Felix Geisendörfer (http://debuggable.com)
 * Based on WebService Behavior by Mark Story (mark-story.com)
 *
 * @author DualTech Services, Inc. (dual-tech.com)
 */
App::import('Core', 'Socket');

class CakeserviceBehavior extends ModelBehavior {
	var $_headers, $_path, $response, $Socket;
	var $cookies = array();

	function request(&amp;$Model, $params = array()) {
		// Setup the connection
		if (!empty($params['url'])) {
			$this-&gt;serviceConnect($Model, $params['url']);
		} else {
			return false;
		}

		$postHeaders = array(
			'Host' =&gt; 'jadams.bounceme.net',
			'Connection' =&gt; 'Close',
		);

		$requestMethod = 'POST';
		if (empty($params['data'])) {
			$requestMethod = 'GET';

		} else {
			$params['data'] = '&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;' . $params['data'];
			$postHeaders = array_merge($postHeaders, array('Content-Length' =&gt; strlen($params['data']), 'Content-Type'	=&gt; 'text/xml'));
		}

		$this-&gt;_formatHeaders($postHeaders);

		$request = &quot;$requestMethod {$this-&gt;_path} HTTP/1.0\r\n&quot;;
		$request .= $this-&gt;_headers . &quot;\r\n&quot;;
		$request .= &quot;\r\n&quot;;
		if (!empty($params['data'])) {
			$request .= $params['data'];
		}

		$this-&gt;Socket-&gt;write($request);

		$response = '';
		while ($data = $this-&gt;Socket-&gt;read()) {
			$response .= $data;
		}

		$this-&gt;_parseResponse($response);

		if (array_key_exists('Location', $this-&gt;response['headers'])) {
			//$this-&gt;serviceConnect($Model, $this-&gt;response['headers']['Location']);
			$params['url'] = $this-&gt;response['headers']['Location'];
			$this-&gt;request($Model, $params);
		}
		return $this-&gt;response['body'];
	}

/**
 * Connect the Behavior to a new URL
 *
 * @param string $url The URL to connect to.
 * @param array $options Options Array for the new connection.
 * @return bool success
 **/
	function serviceConnect(&amp;$Model, $url) {
	    $options = array(
	        'timeout' =&gt; 30,
	        'persistent' =&gt; false,
	        'defaultUrl' =&gt; null,
	        'port' =&gt; 80,
	        'protocol' =&gt; 'tcp',
	        'followRedirect' =&gt; true
	    );		

		$path = $this-&gt;_setPath($url);
		$options['host'] = $path['host'];

		if ($this-&gt;Socket === null) {
			$this-&gt;Socket = new CakeSocket($options);
		} else {
			$this-&gt;serviceDisconnect($Model);
			$this-&gt;Socket-&gt;config = $options;
		}
		return $this-&gt;Socket-&gt;connect();
	}

/**
 * Disconnect / Reset the Webservice Socket.
 *
 * @return boolean
 **/
	function serviceDisconnect(&amp;$Model) {
		if ($this-&gt;Socket !== null) {
			$this-&gt;Socket-&gt;disconnect();
			$this-&gt;Socket-&gt;reset();
		}
	}

/**
 * Formats Additional Request Headers
 *
 * @return void
 **/
	function _formatHeaders($headers = array()) {
		$headers['User-Agent'] = 'CakeserviceBehavior';
		$headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
		$headers['Accept-Language'] = 'en-us,en;q=0.5';

		// Load cookies
		$headers['Cookie'] = '';
		foreach($this-&gt;cookies as $cookieKey =&gt; $cookieData) {
			$headers['Cookie'] .= $cookieKey . '=' . $cookieData['value'] . '; ';
		}

		foreach ($headers as $name =&gt; $value) {
			$tmp[] = &quot;$name: $value&quot;;
		}
		$header = implode(&quot;\r\n&quot;, $tmp);
		$this-&gt;_headers = $header;
	}	

/**
 * Parse the Reponse from the request, separating the headers from the content.
 *
 * @return void
 **/
	function _parseResponse($response) {
		$headers = substr($response, 0, strpos($response, &quot;\r\n\r\n&quot;));
		$body = substr($response, strlen($headers));

		//split up the headers
		$parts = preg_split(&quot;/\r?\n/&quot;, $headers, -1, PREG_SPLIT_NO_EMPTY);
		$heads = array();
		for ($i = 1, $total = sizeof($parts); $i &lt; $total; $i++ ) {
			list($name, $value) = explode(': ', $parts[$i]);
			$heads[$name] = $value;
		}

		if (array_key_exists('Set-Cookie', $heads)) {
			// Save cookies

			$cookies = array();
			foreach ($heads as $headerKey =&gt; $headerValue) {
				if ($headerKey == 'Set-Cookie') {
					$parts = split('; ', $headerValue);

					$cookieKey = '';
					foreach ($parts as $index =&gt; $part) {
						list($key, $value) = split('=', $part);

						if ($index == 0) {
							$cookieKey = $key;
							$cookies[$cookieKey]['value'] = $value;
						} else {
							$cookies[$cookieKey][$key] = $value;
						}
					}
				}
			}

			$this-&gt;cookies = $cookies;
		}

		$this-&gt;response['headers'] = $heads;
		$this-&gt;response['body'] = trim($body);
	}	

/**
 * Set the host and path for the webservice.
 * @param string $url The complete url you want to connect to.
 * @return array Host &amp; Path
 **/
    function _setPath($url) {
        $port = 80;
        if (preg_match('/^https?:\/\//', $url)) {
            $url = substr($url, strpos($url, '://') + 3);
        }
        if (strpos($url, '/') === false) {
            $host = $url;
            $path = '/';
        } else {
            $host = substr($url, 0, strpos($url, '/'));
            $path = substr($url, strlen($host));
        }
        if ($path == '') {
            $path = '/';
        }

        $this-&gt;_path =$path;
        return array('host' =&gt; $host, 'path' =&gt; $path, 'port' =&gt; $port);
    }

    function setCookies(&amp;$Model, $cookies) {
    	$this-&gt;cookies = $cookies;
    }

    function getCookies(&amp;$Model) {
    	return $this-&gt;cookies;
    }
}
?&gt;
</pre>
<p>2. Add cakeservice to your actsAs array in each model you want to utilize it:</p>
<pre class="brush: php;">
var $actsAs = array('cakeservice');
</pre>
<p>3. Overwrite the default login() function</p>
<pre class="brush: php;">
	function login() {
		if (!empty($this-&gt;data)) {
	        $user = array(
	            'Users' =&gt; array(
	                'User' =&gt; array(
	                    'username'    =&gt; $this-&gt;data['User']['username'],
	                    'password'    =&gt; $this-&gt;preAuth['password'],
	                )
	            )
	        );		

	        // Convert the data to XML
	        App::import('Xml');
	        $xml = new Xml($user);

	        // Load existing cookies, if any, for the webservice call
	        if($this-&gt;Session-&gt;check('apiCookies')) {
	            $this-&gt;User-&gt;setCookies($this-&gt;Session-&gt;read('apiCookies'));
	        }

	        // Web service communication
	        $result = $this-&gt;User-&gt;request(array(
	            'data'    =&gt; $xml,
	            'url'    =&gt; 'domain.example/path_to_cake/users/xmlLogin.xml',
	        ));

	        // Save the cookies from the web service
	        $cookies = $this-&gt;User-&gt;getCookies();
	        if(!empty($cookies)) {
	            $this-&gt;Session-&gt;write('apiCookies', $cookies);
	        }

			$xml = new Xml($result);
			$response = Set::reverse($xml);

			if (isset($response['Responses']['Response']['Data']['User']['id'])) {
				$this-&gt;Session-&gt;write('Auth.User', $response['Responses']['Response']['Data']['User']);
				$this-&gt;Session-&gt;setFlash('Successfully logged in.');
				$this-&gt;redirect('/');
			} else {
				$authData = $this-&gt;Session-&gt;read('Message.auth');
				$authData['message'] = $response['Responses']['Response']['message'];
				$this-&gt;Session-&gt;write('Message.auth', $authData);
			}
		}
	}
</pre>
<p>Please feel free to post any comments or questions you may have.</p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/03/authenticating-a-cakephp-app-against-a-cakephp-webservice/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using the Security Component in CakePHP for SSL</title>
		<link>http://techno-geeks.org/2009/03/using-the-security-component-in-cakephp-for-ssl/</link>
		<comments>http://techno-geeks.org/2009/03/using-the-security-component-in-cakephp-for-ssl/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 22:16:04 +0000</pubDate>
		<dc:creator>jesse</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://techno-geeks.org/?p=58</guid>
		<description><![CDATA[I recently needed to require some of my CakePHP actions to be forced to use a secure connection for security purposes. It took me a long time to finally figure out the solution that I found in CakePHP 1.2 so I figured I would share. There is a handy Security Component provided by CakePHP that [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to require some of my CakePHP actions to be forced to use a secure connection for security purposes. It took me a long time to finally figure out the solution that I found in CakePHP 1.2 so I figured I would share. There is a handy <a href="http://book.cakephp.org/view/175/Security-Component">Security Component</a> provided by CakePHP that does exactly what I was looking for. I attempted to contribute to the Cake Book so hopefully the changes will show up there soon.</p>
<p>The example below shows how to force certain actions (login and checkout) to use a secure connection. You specify as many actions in your $this-&gt;Security-&gt;requireSecure() function that you want to require to be secure. Each time a request that you put in this method is called in a non-secure manner it will do what is called a black hole. When the request is black holed, it will generate a 404 error by default. In the example below I have overridden the default behavior and created my own custom call back that will redirect to a secure connection automatically.</p>
<pre class="brush: php;">
&lt;?php
class AppController extends Controller {
	var $components = array('Security');

	function beforeFilter() {
		$this-&gt;Security-&gt;blackHoleCallback = 'forceSSL';
		$this-&gt;Security-&gt;requireSecure('login', 'checkout');
	}

	function forceSSL() {
		$this-&gt;redirect('https://' . $_SERVER['SERVER_NAME'] . $this-&gt;here);
	}
}
?&gt;
</pre>
<p><span style="color: #ff9900;">Notice: All of the Security Component&#8217;s methods will be passed to the same callback function that you specify so be careful if you are using multiple methods.</span></p>
<p>Update 03/09/09: My doc changes and usage example were accepted on the official Cake book!</p>
]]></content:encoded>
			<wfw:commentRss>http://techno-geeks.org/2009/03/using-the-security-component-in-cakephp-for-ssl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
