0

我正在尝试通过我的应用程序中的 cron 作业向某些用户发送电子邮件通知。

经过几个小时的阅读,我了解到最好的方法是使用 Shell。

请有人可以帮助我了解如何做到这一点,我如何使用一个 myShell 类的不同操作来发送不同的通知?我的意思是如何让 cron 访问 myShell 不同的操作。

例如。

<?php
  class MyShell extends Shell { 

    function send_task_notifications(){
      .... //this must send email every day at 00:00 am
    }

    function send_new_post_notifications() {
      .... //this must send email every week//
    }

  }
?>

这两个动作都在 MyShell 类中。

那么我如何通过 Cron 调用其中一个,并且这个 MyShell 类是否可以通过 URL 访问?

4

1 回答 1

1

您的外壳需要按以下方式更改,您需要根据该参数传递一个参数,它将执行电子邮件通知/推送通知。将您的功能移动到它将起作用的组件

<?php
  class MyShell extends Shell { 

    function main()
    {
        $option = !empty($this->args[0]) ? $this->args[0] : ”;
        echo ‘Cron started without any issue.’;

        App::import(‘Component’, 'MyOwnComponent');
        $this->MyOwnComponent = &new MyOwnComponent();
        switch ($option)
        {
            case 'task_notifications':
                    $this->MyOwnComponent->send_task_notifications();
                break;
           case 'post_notifications':
                    $this->MyOwnComponent->send_new_post_notifications();
                break;
            default:
            echo 'No Parameters passed .';
        }

    }
  }
?>

您的组件文件如下

<?php
class MyOwnComponent extends Object
{
 function send_task_notifications(){
      .... //this must send email every day at 00:00 am
    }

 function send_new_post_notifications() {
      .... //this must send email every week//
    }
}

?>

有关更多详细信息,请参阅链接http://cakephpsaint.wordpress.com/2013/05/15/6-steps-to-create-cron-jobs-in-cakephp/

于 2013-05-17T10:41:59.650 回答