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 
4

2 回答 2

2

Step1:创建一个名为 ReminderShell.php 的 shell 文件,路径应该是 PROJECT_DIR_PATH/PROJECT_NAME/app/Console/Command/ReminderShell.php 复制下面的脚本并粘贴它

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

Step2:创建名为 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');
        }
        }
    }
}

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

Step4:在config目录下创建email.php

Step5:在终端中运行以下命令:控制台/蛋糕提醒

PROJECT_DIR_PATH/PROJECT_NAME/app Console/cake Reminder 

从https://github.com/pankajkumarjha2010/cronjob-in-cakephp2.3.x查看完整的文件

于 2013-09-03T20:56:37.017 回答
0

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

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

于 2012-12-22T00:35:17.730 回答