Written on 30 August 2012, 07:34pm
Tagged with: CakePHP, cron, shell
Just adapting a solution from 2006 to the 2012 version of CakePHP 🙂
Calling controller actions from cron and the command line
Step 1: copy app/webroot/index.php to app/cron.php
Step 2: edit app/cron.php
Change the last 3 lines of code as follows:
//---------THESE LINES:
App::uses('Dispatcher', 'Routing');
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest(),
new CakeResponse(array('charset' => Configure::read('App.encoding'))));
//-----------CHANGE TO:
App::uses('Dispatcher', 'Routing');
define('CRON_DISPATCHER',true);
if($argc == 2) {
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest($argv[1]),
new CakeResponse(array('charset' => Configure::read('App.encoding'))));
}
Step 3: create Controller/CronController.php
class CronController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->layout=null;
}
public function test() {
// Check the action is being invoked by the cron dispatcher
if (!defined('CRON_DISPATCHER')) { $this->redirect('/'); exit(); }
//no view
$this->autoRender = false;
//do stuff...
return;
}
}
Step 4: Run your cron using the command line:
# php ./app/cron.php /cron/test
Step 5: Test that loading the same script in browser is not allowed (for security reasons):
http://yourdomain/cron/test redirects to http://yourdomain/