如何在cakephp中执行cron job?

时间:2012-04-10 11:30:16

标签: cakephp-2.0

  # Cron Job for Cakephp #
  

我创建了1个文件cron_scheduler.php,它是wamp / www / projectname / app / webroot / index.php文件的副本,它也在同一个文件夹中

     

我试图运行throw命令提示符但没有得到任何结果。

     

如何为Cakephp运行cronjob?我什么都不知道         如果有人知道,请帮助我。         提前谢谢。


 <?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 

2 个答案:

答案 0 :(得分:2)

步骤1:创建一个名为ReminderShell.php的shell文件,Path应为PROJECT_DIR_PATH / PROJECT_NAME / app / Console / Command / ReminderShell.php复制下面的脚本并粘贴它

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

步骤2:创建名为MailTask​​.php的任务文件,路径应为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');
        }
        }
    }
}

步骤3:创建一个名为reminder.ctp的电子邮件模板,路径应为PROJECT_DIR_PATH / PROJECT_NAME / app / View / Emails / html / reminder.ctp

步骤4:在config目录中创建email.php

步骤5:在终端:控制台/蛋糕提醒

中运行以下命令
PROJECT_DIR_PATH/PROJECT_NAME/app Console/cake Reminder 

https://github.com/pankajkumarjha2010/cronjob-in-cakephp2.3.x

结帐的完整文件

答案 1 :(得分:0)

对于CakePHP中的cronjobs,您可以查看http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html。这对我来说很好。

但请注意,有时并非所有环境参数都可用。我在cronjob方法中访问env('HTTP_HOST')时遇到了这个问题。但考虑到这一点,应该没问题。