0
  # Cron Job for Cakephp #

I have create 1 file cron_scheduler.php which is copy of wamp/www/projectname/app/webroot/index.php file and it is also in the same folder

i have tried to run throw command prompt but didn't get any result.

how to run cronjob for Cakephp ? i don't have any idea if anyone knows , please help me. Thanks in advance.


 <?php
    if (!defined('DS')) {
            define('DS', DIRECTORY_SEPARATOR);
    }
   /**
      * These defines should only be edited if you have cake installed in
      * a directory layout other than the way it is distributed.
      * Each define has a commented line of code that explains what you
        would change.
      *
      */
        if (!defined('ROOT')) {
            //define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS
            //LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
            //You should also use the DS define to seperate your directories
            define('ROOT', dirname(dirname(__FILE__)));
    }
    if (!defined('APP_DIR')) {
            //define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
            define('APP_DIR', basename(dirname(__FILE__)));
    }
      /**
        * This only needs to be changed if the cake installed libs are located
        * outside of the distributed directory structure.
       */
           if (!defined('CAKE_CORE_INCLUDE_PATH'))
              {
            //define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE
            //CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
             //You should also use the DS define to seperate your directories
             define('CAKE_CORE_INCLUDE_PATH', ROOT);
              }

            ///////////////////////////////
            //DO NOT EDIT BELOW THIS LINE//
            ///////////////////////////////

            if (!defined('WEBROOT_DIR')) 
             {
            define('WEBROOT_DIR', 'webroot');
    }
    if (!defined('WWW_ROOT')) {
            //define('WWW_ROOT', dirname(__FILE__) . DS);
            define('WWW_ROOT', dirname(__FILE__) . DS . 'webroot' . DS);
    }
    if (!defined('CORE_PATH')) {
            if (function_exists('ini_set')) {
                    ini_set('include_path', CAKE_CORE_INCLUDE_PATH . 
                    PATH_SEPARATOR .
                    ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'));
                    define('APP_PATH', null);
                    define('CORE_PATH', null);
            } else {
                    define('APP_PATH', ROOT . DS . APP_DIR . DS);
                    define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
            }
    }
    require CORE_PATH . 'cake' . DS . 'bootstrap.php';


    define('CRON_DISPATCHER',true);
    if($argc == 2) 
    {
            $Dispatcher= new Dispatcher();
            $Dispatcher->dispatch($argv[1]);
    }

          ?>

> i have tried to run throw command promt but didn't get any result 
> how to run cronjob for Cakephp ? i don't have any idea
  if anyone knows , please help me.
  Thanks 
Dolly
  • 1,042
  • 4
  • 27
  • 47
  • possible duplicate of [How to setup cronjobs in cake php?](http://stackoverflow.com/questions/13949539/how-to-setup-cronjobs-in-cake-php) – AD7six Jun 11 '14 at 11:55

2 Answers2

2

Step1: Create a shell file with name ReminderShell.php and Path should be PROJECT_DIR_PATH/PROJECT_NAME/app/Console/Command/ReminderShell.php Copy below script and paste it

class ReminderShell extends Shell { 
         var $tasks = array('Mail'); 
         function main() { 
               $this->Mail->enroll_reminder(); 
                         }
          }

Step2: Create Task file with name MailTask.php and path should be PROJECT_DIR_PATH/PROJECT_NAME/app/Console/Command/Task/MailTask.php

<?php

App::uses('CakeEmail', 'Network/Email');

class MailTask extends Shell {

var $uses = array('Contact');

public function enroll_reminder() {
    $Email = new CakeEmail();
    $Email->config('default');
    $reminder = $this->Contact->find('all');
    if (!empty($reminder)) {
        foreach ($reminder as $val) {
        $id = $val['Contact']['id'];
        $name = $val['Contact']['first_name'];
        $email = $val['Contact']['email'];
        $Email->template('reminder')
            ->viewVars(array('fname' => $name))
            ->emailFormat('html')
            ->subject('xyz.com: Enrollment Reminder')
            ->to($email)
            ->from('noreply@xyz.com');
        if ($Email->send()) {
            $update_val['Contact']['id'] = $id;
            $update_val['Contact']['enroll_reminder'] = 'sent';
            $update_val['Contact']['enroll_reminder_date'] = date("Y-m-d H:i:s");
            $this->Contact->save($update_val['Contact']);
            $this->out($email.' Mail Sent');
        }
        }
    }
}

Step3: Create a email template with name reminder.ctp and path should be PROJECT_DIR_PATH/PROJECT_NAME/app/View/Emails/html/reminder.ctp

Step4: Create email.php in config directory

Step5: Run below command in Terminal: Console/cake Reminder

PROJECT_DIR_PATH/PROJECT_NAME/app Console/cake Reminder 

For complete files checkout from https://github.com/pankajkumarjha2010/cronjob-in-cakephp2.3.x

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Pank
  • 13,800
  • 10
  • 32
  • 45
0

For cronjobs in CakePHP, you may have a look at http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html. This works fine for me.

But please note, sometimes there are not all the environmental parameters available. I ran into this problem when accessing env('HTTP_HOST') within the cronjob methods. But when considering this, it should be no problem.

mixable
  • 1,068
  • 2
  • 12
  • 42