以cli的方式运行Yii

  1. 首先在项目的protected 目录下找到yii.php。打开并修改引入yiic.php的路径(结合自己情况修改)
  2. 在命令行下切换到protected 目录下,输入yiic(linux 系统下为php yiic, 在windows下php.exe所在路径应该添加到环境变量path中)
    默认的情况下
  3. 编写自己的类
    cli 引入的配置文件为config目录下的 console.php。根据自己的需要去配置该文件
    配置文件示例
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Console Application',
    // preloading 'log' component
    'preload'=>array('log'),
    'commandPath' => 'cli',//默认为commands

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),

    // application components
    'components'=>array(
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=bennett',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_',
        ),
        'cache'=>array(  
             'class'=>'system.caching.CFileCache',
             'directoryLevel'=>2,
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning, info',
                ),
            ),
        ),
    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>require(dirname(__FILE__).'/params.php'),
);

在cli文件夹下添加TestCommand.php

class TestCommand extends CConsoleCommand
{
    public function run($args)
    {
        print_r($args);
        echo 'test';
    }
}

然后cli下运行输入yiic(windows)
运行结果
发现多出来一个test。在cli下输入yiic test abc 就能看到结果了。
命令行模式下同样也可以调用定义的component来帮助我们完成需要的业务逻辑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值