Cron 调度器 CakePHP 2.0

2023-01-19php开发问题
6

本文介绍了Cron 调度器 CakePHP 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 CakePHP 2.0 并且一直在尝试设置一个 cronjob 以在控制器中执行一个方法.我一直在发疯,查看各种教程和随机网站,看看我是否能找到解决方案.

I am using CakePHP 2.0 and have been trying to setup a cronjob to execute a method in a controller. I've been going nuts, looking over various tutorials and random sites to see if I could find a solution.

我收到的错误是:

Undefined variable: argc [APP/webroot/cron_dispatcher.php, line 83

这是我的 app/webroot/ 目录中 cron_dispatcher.php 文件的底部.

Here is the bottom of the cron_dispatcher.php file in my app/webroot/ directory.

if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
        trigger_error("CakePHP core could not be found.  Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php.  It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
    }
    if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
    return;
    } else {
      define('CRON_DISPATCHER',true);
    if($argc >= 2) {
        $Dispatcher= new Dispatcher();
        $Dispatcher->dispatch($argv[1]);
        }
    }

我找不到这些变量($argv 和 $argc)的定义位置.它们没有在 dispatcher.php 文件本身的任何地方定义.我搜索了谷歌无济于事.我不是 100% 确定 Dispatcher 是如何工作的,但任何帮助将不胜感激.谢谢.

I cannot find where these variables ($argv and $argc) are defined. They aren't defined anywhere in the dispatcher.php file itself. I searched Google to no avail. I'm not 100% sure how the Dispatcher works, but any help would be greatly appreciated. Thanks.

== 更新GoDaddy 的共享主机不允许您更改 argc argv 的设置.

== UPDATE GoDaddy's shared hosting doesn't allow you to change the settings of argc argv.

推荐答案

如果其他人有兴趣,

在新的 CakePHP 2.0.5 中,您将在 webroot 文件夹中创建一个 index.php:

In the new CakePHP 2.0.5, you will an index.php in webroot folder:

复制这个文件,命名为cron_dispatcher.php,放到同一个目录下(webroot)

Copy this file and name it cron_dispatcher.php, and place it into the same directory (webroot)

您会在最底部找到此代码:

You will find this code at the very bottom:

$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

将底部改为

define('CRON_DISPATCHER',true);
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest($argv[1]), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

您在这里做了两件事:将 CRON_DISPATCHER 设置为 true,并传递环境变量 ($argv[1]).

You're doing two things here: Setting CRON_DISPATCHER to true, and passing environment variables ($argv[1]).

在你的控制器中,在你做任何其他事情之前添加这一行:

In your controller, add this line before you do anything else:

if (!defined('CRON_DISPATCHER')) { $this->redirect('/'); exit(); }

这将确保访问 yoursite.com/controller/cronaction 的人将无法运行您的脚本.

This will ensure people going to yoursite.com/controller/cronaction won't be able to run your script.

在 webroot 中的 htaccess 文件中,添加:

In your htaccess file in webroot, add this:

<Files  "cron_dispatcher.php">
    Order deny,allow
    Deny from all
</Files>

这将确保访问 yoursite.com/cron_dispatcher.php 的人无法运行它.

This will ensure poeple going to yoursite.com/cron_dispatcher.php won't be able teo run it.

现在使用以下命令设置 cron 作业:

Now set up the cron job using something like the command:

php /home/yoursite/public_html/cakephp/app/webroot/cron_dispatcher.php /controller/cronjobaction

这篇关于Cron 调度器 CakePHP 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17