2016-10-20 00:31:16 2641次浏览 0条回答 0 悬赏 25 金钱

1.问题:需要利用cron定时执行console任务,用Yii basic 2.0.9版本模板做测试,在本地测试环境中执行正常,但在godaddy的web空间执行则出现异常,还请帮忙看一下,多谢。

2.入口脚本WEBROOT/yii

#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/config/console.php');

$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);

3.配置文件WEBROOT/config/console.php

<?php

Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

$config = [
    'id' => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'app\commands',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
    ],
    'params' => $params,
    /*
    'controllerMap' => [
        'fixture' => [ // Fixture generation command line.
            'class' => 'yii\faker\FixtureController',
        ],
    ],
    */
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;

4.控制器文件WEBROOT/commands/HelloController.php

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\commands;

use yii\console\Controller;
use Yii;
/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class HelloController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     */
    public function actionIndex($message = 'hello world')
    {
        //echo $message . "\n";
	Yii::warning("------Job_actionIndex ".$message);//输出信息,测试是否执行
    }
}

5.本地测试环境执行正常,以下两个命令都可以正常执行

php WEBROOT/yii hello
php WEBROOT/yii hello/index

6.利用godaddy的web空间Cron任务,执行php WEBROOT/yii hello异常结果

This is Yii version 2.0.9.

The following commands are available:

- asset                        Allows you to combine and compress your JavaScript and CSS files.
    asset/compress (default)   Combines and compresses the asset files according to the given configuration.
    asset/template             Creates template of configuration file for [[actionCompress]].

- cache                        Allows you to flush cache.
    cache/flush                Flushes given cache components.
    cache/flush-all            Flushes all caches registered in the system.
    cache/flush-schema         Clears DB schema cache for a given connection component.
    cache/index (default)      Lists the caches that can be flushed.

- fixture                      Manages fixture data loading and unloading.
    fixture/load (default)     Loads the specified fixture data.
    fixture/unload             Unloads the specified fixtures.

- gii                          This is the command line version of Gii - a code generator.
    gii/controller             Controller Generator
    gii/crud                   CRUD Generator
    gii/extension              Extension Generator
    gii/form                   Form Generator
    gii/index (default)
    gii/model                  Model Generator
    gii/module                 Module Generator

- hello                        This command echoes the first argument that you have entered.
    hello/index (default)      This command echoes what you have entered as the message.

- help                         Provides help information about console commands.
    help/index (default)       Displays available commands or the detailed information

- message                      Extracts messages to be translated from source files.
    message/config             Creates a configuration file for the "extract" command using command line options specified
    message/config-template    Creates a configuration file template for the "extract" command.
    message/extract (default)  Extracts messages to be translated from source code.

- migrate                      Manages application migrations.
    migrate/create             Creates a new migration.
    migrate/down               Downgrades the application by reverting old migrations.
    migrate/history            Displays the migration history.
    migrate/mark               Modifies the migration history to the specified version.
    migrate/new                Displays the un-applied new migrations.
    migrate/redo               Redoes the last few migrations.
    migrate/to                 Upgrades or downgrades till the specified version.
    migrate/up (default)       Upgrades the application by applying new migrations.

- serve                        Runs PHP built-in web server
    serve/index (default)      Runs PHP built-in web server

Status: 500 Internal Server Error
X-Powered-By: PHP/5.6.25
Content-type: text/html; charset=UTF-8

**Exception 'yii\base\InvalidConfigException' with message 'Unable to determine the entry script file path.'

in /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/Request.php:62

Stack trace:**
#0 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/Component.php(130): yii\base\Request->getScriptFile()
#1 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/controllers/HelpController.php(438): yii\base\Component->__get('scriptFile')
#2 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/controllers/HelpController.php(241): yii\console\controllers\HelpController->getScriptName()
#3 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/controllers/HelpController.php(67): yii\console\controllers\HelpController->getDefaultHelp()
#4 [internal function]: yii\console\controllers\HelpController->actionIndex(NULL)
#5 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
#6 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#7 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/Controller.php(119): yii\base\Controller->runAction('', Array)
#8 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/Module.php(454): yii\console\Controller->runAction('', Array)
#9 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('', Array)
#10 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('', Array)
#11 /home/xxxx/public_html/xxxx/yiibasic/basic/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
#12 /home/xxxx/public_html/xxxx/yiibasic/basic/yii(19): yii\base\Application->run()
#13 {main}

7.利用godaddy的web空间Cron任务,执行php WEBROOT/yii hello异常结果代码yii/vendor/yiisoft/yii2/base/Request.php:62

    /**
     * Returns entry script file path.
     * @return string entry script file path (processed w/ realpath())
     * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
     */
    public function getScriptFile()
    {
        if ($this->_scriptFile === null) {
            if (isset($_SERVER['SCRIPT_FILENAME'])) {
                $this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
            } else {
                **throw new InvalidConfigException('Unable to determine the entry script file path.');**
            }
        }

        return $this->_scriptFile;
    }

8.错误原因是$_SERVER['SCRIPT_FILENAME']没有被设置,怀疑是web空间执行console环境初始化设置的问题

9.修改入口脚本WEBROOT/yii,设置$_SERVER['SCRIPT_FILENAME']变量

#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/config/console.php');

if (isset($_SERVER['SCRIPT_FILENAME'])) 
{
    //输出信息
}
else
{
    $_SERVER['SCRIPT_FILENAME']="/home/xxxx/public_html/xxxx/yiibasic/basic/yii";
    //输出信息
}
$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);

10.利用godaddy的web空间Cron任务,执行php WEBROOT/yii hello,不再出现异常,但是控制器路由无法找到,输出的是console帮助信息

This is Yii version 2.0.9.

The following commands are available:

- asset                        Allows you to combine and compress your JavaScript and CSS files.
    asset/compress (default)   Combines and compresses the asset files according to the given configuration.
    asset/template             Creates template of configuration file for [[actionCompress]].

- cache                        Allows you to flush cache.
    cache/flush                Flushes given cache components.
    cache/flush-all            Flushes all caches registered in the system.
    cache/flush-schema         Clears DB schema cache for a given connection component.
    cache/index (default)      Lists the caches that can be flushed.

- fixture                      Manages fixture data loading and unloading.
    fixture/load (default)     Loads the specified fixture data.
    fixture/unload             Unloads the specified fixtures.

- gii                          This is the command line version of Gii - a code generator.
    gii/controller             Controller Generator
    gii/crud                   CRUD Generator
    gii/extension              Extension Generator
    gii/form                   Form Generator
    gii/index (default)
    gii/model                  Model Generator
    gii/module                 Module Generator

**- hello                        This command echoes the first argument that you have entered.
    hello/index (default)      This command echoes what you have entered as the message.**

- help                         Provides help information about console commands.
    help/index (default)       Displays available commands or the detailed information

- message                      Extracts messages to be translated from source files.
    message/config             Creates a configuration file for the "extract" command using command line options specified
    message/config-template    Creates a configuration file template for the "extract" command.
    message/extract (default)  Extracts messages to be translated from source code.

- migrate                      Manages application migrations.
    migrate/create             Creates a new migration.
    migrate/down               Downgrades the application by reverting old migrations.
    migrate/history            Displays the migration history.
    migrate/mark               Modifies the migration history to the specified version.
    migrate/new                Displays the un-applied new migrations.
    migrate/redo               Redoes the last few migrations.
    migrate/to                 Upgrades or downgrades till the specified version.
    migrate/up (default)       Upgrades the application by applying new migrations.

- serve                        Runs PHP built-in web server
    serve/index (default)      Runs PHP built-in web server


To see the help of each command, enter:

 ** yii help <command-name>**

X-Powered-By: PHP/5.6.25
Content-type: text/html; charset=UTF-8
    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
39142
等待激活

39142

注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
  • 粉丝0
  • 金钱0
  • 威望0
  • 积分0

热门问题