Search code examples
phpcakephpcroncakephp-2.1cron-task

Best method for Cron in CakePhp


I am currently working on a project in CakePhp in which Cron job is required for sending emails & other activities, i am little bit confuse related to this if i created CRON using core php methods in custom controller then it work fine & also if i created Cron using CakePhp SHELL class then it also working as it in Core PHP method.

Now i am confuse which method is best for this job Core PHP method using in Shell OR Core PHP method using in Custom Controller, can any one help me about this which method is best in using & why?

Thank You in advance


Solution

  • I always use Shells for cronjobs, rather than controller actions. Simply because shells are meant for "bare" operations that don't need any graphical representation. Furthermore, most of my apps use Authentication, so you'd need to build a custom launcher for your cronjobs if you'd want the cron daemon to authenticate itself before calling the action. With shells, you can just access all your models and data without any authentication.

    And even if your app doesn't use Authentication, you surely wouldn't want any human or robot using your app to be triggering your cronjobs by simply calling the URL of the controller action that launches the cronjob, so also from a security point of view, Shells are the way to go.

    Also check http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html for more information on how to get your shells running as cronjobs, it's pretty simple.