2016-10-18 20:19:04 4360次浏览 4条回答 0 悬赏 15 金钱

1.问题:需要利用cron定时执行console任务,在本地测试环境中执行正常,但在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);

// fcgi doesn't have STDIN and STDOUT defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));

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

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

return [
    'id' => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log', 'gii'],
    'controllerNamespace' => 'app\commands',
    'modules' => [
        'gii' => 'yii\gii\Module',
    ],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'api' => [
            'class' => 'app\components\Api'

        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
    ],
    'params' => $params,
];

4.配置文件Webroot/commands/CronController.php

namespace app\commands;
 
use yii\console\Controller;
use Yii;
use yii\helpers\Url;
use yii\db\Query;

class CronController extends Controller
{
   public static function actionCron()
    {
	//code	
    }
}

5.本地测试环境执行正常

php Webroot/yii cron/cron

6.godaddy的web空间执行php Webroot/yii cron/cron错误结果

This is Yii version 2.0.5.

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.

- cron                         
    cron/index (default)

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


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
  • 回答于 2016-10-18 21:32 举报

    controller文件里的action不能用static,另外是不是空间不支持cli模式?

    2 条回复
    回复于 2016-10-18 21:46 回复

    感谢,我修改试试,不过本地虚拟机测试却可以,不知道为什么?
    不过空间是支持cli的,反馈的信息表明执行了cli并且打印了帮助信息,只是路由没有找到。

    回复于 2016-10-19 08:03 回复

    修改了static也是不行啊

  • 回答于 2016-10-19 10:39 举报

    这怎么看也不是错误结果呀?

    1 条回复
    回复于 2016-10-19 17:30 回复

    是路由没找到,输出了控制台的执行帮助信息。

  • 回答于 2016-10-19 10:42 举报

    从提示信息里面看 cron 只包含了 index 方法啊, 。。。 代码没同步?

    1 条回复
    回复于 2016-10-19 17:34 回复

    已经在CronController.php里实现了actionCron()方法,代码是在本地执行正常才上传的,不存在没同步的问题。

  • 回答于 2016-10-19 10:54 举报

    本机是WINDOWS, 而服务器是Linux吧

    1 条回复
    回复于 2016-10-19 17:31 回复

    都是Linux

您需要登录后才可以回答。登录 | 立即注册
39142
等待激活

39142

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

热门问题