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('/'); 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
Cron job path for CakePHP
How should I write path to my crons controller?
You should not do that at all. If not properly protected anyone could trigger your controller from the web. Also that's not what a controller is made for.
Use a shell (or a shell command through cpanel for crons)
Assuming that the CakePHP core is in
/home/mysite/public_html/lib/
You should be able to run it like this:
/home/mysite/public_html/lib/Cake/Console/cake --app /home/mysite/public_html/app shellName
More details and examples are shown here.
How to setup cronjobs in cake php?
Use a shell
The 'Cake Way' of using a CakePHP application in cron jobs would be creating shell and then calling it as a cron job.
i.e. Create a shell to do the task, and then add it to crontab (crontab -e
on linux machine):
0 * * * * cd /path/to/app/ && Console/cake your_shell_name params
Creating shells and using them with cron is covered in the documentation.
Related Topics
How to Execute My SQL Query in Codeigniter
Get the Price of an Item on Steam Community Market with PHP and Regex
Xml Creation Using Codeigniter
"Class Xxx Is Not a Valid Entity or Mapped Super Class" After Moving the Class in the Filesystem
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
Error_Reporting(E_All) Does Not Produce an Error
Differencebetween ' and " in PHP
Should Lock_Ex on Both Read & Write Be Atomic
How Is an Array in a PHP Foreach Loop Read
MySQL Join Multiple Rows as Columns
When Do I Use PHP_Eol Instead of \N and Vice-Versa? Ajax/Jquery Client Problem
Php: Get List of All Filenames Contained Within My Images Directory