問題描述
我正在使用 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!