Category Archives: CakePHP - Page 2

Better Forking for PHP

I recently needed to update a cron job written for CakePHP to make it multi-threaded, so that a job that would take months otherwise could finish in a reasonable amount of time. I originally planned to simply use pcntl_fork() to do this, but upon reading the comments on the function, I quickly realized it would be a major headache to write properly due to database and other socket connections involved in the process.
Read more »

CakePHP Twitter and Bitly Components

I promised more programming related posts, and here’s one for you.

As part of developing TweeterDiet.com, I needed to interface with Twitter and Bit.ly to post tweets and the links back to the public profiles on TweeterDiet. I searched around for a while, and didn’t immediately run across any good components for what I needed, so I wrote my own, and now I’d like to share them.
Read more »

TweeterDiet.com and CakePHP

First things first. As of 11:45 PM last night, my latest project launched. Go take a look at TweeterDiet.com Go ahead. I’ll still be here when you get back.

Yes, I know it’s very minimal right now. I’ll be remedying that over the next week or so, by adding in some sample screen-shots and more features. But for now, it at least functions, and I’ve been using it personally for the last couple weeks. Here’s a basic rundown of the idea for the site. As you may know, if you read my blog regularly, or happened to glance over to the right sidebar, I’m trying to lose a lot of weight. To help me out, I’m going to the gym, but most importantly, I’m tracking my weight. I’m using a method borrowed from The Hacker’s Diet, which is quite simple: Weigh yourself once a day, and calculate a trended weight from that. The trended weight smooths out the daily fluctuations, and gives you a much better picture of what’s actually happening with your body. From that trend, you can calculate your daily calorie deficit, and know if you need to change things to lose weight faster or slower.

Read more »

CakePHP mod_rewrite optimization

CakePHP is great, but the default installation has a performance issue that is very easy to resolve, and should be fixed in any production installation of a CakePHP application. The solution is very simple.

In the default install, there is a file in the webroot called .htaccess. It contains mod_rewrite rules, and you have to set you vhost up to allow overrides. This introduces a significant overhead to each request, as apache has to search for applicable htaccess files and merge their settings into the core apache config every request. On a production application this can become significant, especially if you get a significant amount of traffic.

Read more »

CakePHP Shells, and Cron Jobs

As part of the same project that resulted in my last posting, I needed to write several processes that would be managed by cron. In the past, at the Yahoo contract, we had come up with a way to do this involving a customized version of the basic CakePHP index.php file. However, his required us to place the cron code on the main controllers, and extra protections to ensure that the code was inaccessible from the main web interface. I was never thrilled with the solution, but it was the best we had at the time.

So for this project, I looked into the concept of ‘Shells’, which are basically command-line interfaces to a CakePHP application. It quickly became obvious that this was the best way to go about handling the cron jobs for a CakePHP application. It allows you to place all your sensitive cron code completely out of reach of the web facing part of the application, increasing security and decreasing complexity. It’s really quite simple as well.

The process is basically outlined in this section of the CakePHP manual, and should be simple enough to follow. The end result is code like this:

class BillingShell extends Shell {
	var $uses = array('OrderLineItem', 'Division');
 
	function main() {
		echo "Please specify which billing task to run. Currently, the only choice is 'daily'.\n\n";
	}
 
	function daily() {
		date_default_timezone_set('America/Denver');
		App::Import('Component','Cbg');
		$this->Cbg = new CbgComponent();
 
		$this->create_invoices();
		$this->process_invoices();
	}
 
	// Other functions not shown
}

And you can run it as a cron like this:

# m h  dom mon dow   command
*/5 * * * * /full/path/to/cakeshell billing daily -app /full/path/to/app