- 首先在项目的
protected
目录下找到yii.php。打开并修改引入yiic.php的路径(结合自己情况修改) - 在命令行下切换到
protected
目录下,输入yiic
(linux 系统下为php yiic, 在windows下php.exe所在路径应该添加到环境变量path中)
- 编写自己的类
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来帮助我们完成需要的业务逻辑