Cron Dispatcher CakePHP 2.0 [英] Cron Dispatcher CakePHP 2.0

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

问题描述

我使用CakePHP 2.0并尝试设置一个cronjob在控制器中执行一个方法。我一直在努力,看看各种教程和随机网站,看看是否可以找到一个解决方案。



我收到的错误是:

 未定义变量:argc [APP / webroot / cron_dispatcher.php,第83行

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

  if(!include(CORE_PATH。'cake'。DS。'bootstrap.php')){
trigger_error(CakePHP core could not be found。它应该指向包含你的。DS。蛋糕核心目录和你的。DS。供应商根目录。,E_USER_ERROR)的目录中的CAKE_CORE_INCLUDE_PATH的值。
}
if(isset($ _ GET ['url'])&& $ _GET ['url'] ==='favicon.ico'){
return;
} else {
define('CRON_DISPATCHER'​​,true);
if($ argc> = 2){
$ Dispatcher = new Dispatcher();
$ Dispatcher-> dispatch($ argv [1]);
}
}



我找不到这些变量argc)。它们不是在dispatcher.php文件本身的任何位置定义的。我搜索Google没有效果。我不是100%确定Dispatcher如何工作,但任何帮助将非常感激。非常感谢。



== UPDATE
GoDaddy的共享托管不允许您更改argc argv的设置。



在新的CakePHP 2.0.5中,您将创建一个索引。 php在webroot文件夹中:



复制此文件并将其命名为cron_dispatcher.php,并将其放置到同一目录(webroot)



你会发现这个代码在底部:

  $ 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])。



在您的控制器中,在执行其他操作之前添加此行:

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

这将确保访问yoursite.com/controller/cronaction的用户将无法运行



在webroot的htaccess文件中,添加:

 < Filescron_dispatcher.php> 
命令拒绝,允许
拒绝所有
< / Files>

这将确保poeple进入yoursite.com/cron_dispatcher.php将无法运行它。



现在使用类似命令设置cron作业:

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


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.

The error I am receiving is this:

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

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]);
        }
    }

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.

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

解决方案

In case anyone else is interested,

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

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'))));

change the bottom to

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

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(); }

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

In your htaccess file in webroot, add this:

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

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

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 Dispatcher CakePHP 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆