Cron Dispatcher CakePHP 2.0

Solution 1:

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('/'); 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

Solution 2:

$argc and $argv is environmental variables set in CLI.
You might need to check your PHP setting on register_argc_argv (which should NOT disabled)

Solution 3:

There is no need for a Cron Dispatcher

There is, and has never been, a need to create a cron dispatcher. The only thing required to use a CakePHP application via cron is to create a console command, and call it:

*/5  *    *    *    *  cd /full/path/to/app && Console/cake myshell myparam

At some point in the past, there was some poor advice in the book which has since been corrected.