dook0034 2011-12-13 07:07
浏览 49
已采纳

Cron Dispatcher CakePHP 2.0

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.

  • 写回答

4条回答 默认 最新

  • douqianke7467 2012-01-04 05:42
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题