我正在使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!