Cron Dispatcher CakePHP 2.0

In case anyone else is interested,

In the new CakePHP 2.0.5, you will an index.php in webroot folder:

Copy this file and name it cron_dispatcher.php, and place it into the same directory (webroot)

You will find this code at the very bottom:

$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

change the bottom to

define('CRON_DISPATCHER',true);
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest($argv[1]), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

You’re doing two things here: Setting CRON_DISPATCHER to true, and passing environment variables ($argv[1]).

In your controller, add this line before you do anything else:

if (!defined('CRON_DISPATCHER')) { $this->redirect("https://stackoverflow.com/"); exit(); }

This will ensure people going to yoursite.com/controller/cronaction won’t be able to run your script.

In your htaccess file in webroot, add this:

<Files  "cron_dispatcher.php">
    Order deny,allow
    Deny from all
</Files>

This will ensure poeple going to yoursite.com/cron_dispatcher.php won’t be able teo run it.

Now set up the cron job using something like the command:

php /home/yoursite/public_html/cakephp/app/webroot/cron_dispatcher.php /controller/cronjobaction

Leave a Comment