命令行方式运行yii2程序

2021-07-19  本文已影响0人  zlchen

测试环境,yii 2.0.42.1版本

1、web访问方式,控制器放在controllers目录下 ,浏览器访问如下地址

http://127.0.0.1/index.php?r=[controller-name]/[method-name]

2、console访问方式,控制器放在commands目录下

./yii [command-name]/[method-name]

[root@iZbp1ff38278eoy1t5paezZ basic]# ./yii hello/index
hello world
[root@iZbp1ff38278eoy1t5paezZ basic]# ./yii hello/index hello-yii2.0
hello-yii2.0
[root@iZbp1ff38278eoy1t5paezZ basic]# ./yii hello/yii_version
2.0.42.1
// 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\console\ExitCode;

/**
 * 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.
     * @return int Exit code
     */
    public function actionIndex($message = 'hello world')
    {
        echo $message . "\n";

        return ExitCode::OK;
    }
    //  V2.0.42.1
    public function actionYiiVersion()
    {
        echo \Yii::getVersion(). "\n";
        
        return ExitCode::OK;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读