Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...