1、在根目录下面添加yii和yii.bat两个文件
其中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‘);
require(__DIR__ . ‘/common/bootstrap.php‘); // 加载别名配置
$config = require(__DIR__ . ‘/config/console.php‘);
$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);
其中yii.bat内容如下
@echo off
rem -------------------------------------------------------------
rem Yii command line bootstrap script for Windows.
rem
rem @author Qiang Xue <[email protected]>
rem @link http://www.yiiframework.com/
rem @copyright Copyright (c) 2008 Yii Software LLC
rem @license http://www.yiiframework.com/license/
rem -------------------------------------------------------------
@setlocal
set YII_PATH=%~dp0
if "%PHP_COMMAND%" == "" set PHP_COMMAND=D:\wamp\bin\php\php5.4.16\php.exe(主要修改的是这块php的位置)
"%PHP_COMMAND%" "%YII_PATH%yii" %*
@endlocal
其中common/bootstrap.php内容如下
<?php
//定义别名
Yii::setAlias(‘modules‘, dirname(__DIR__).‘\modules‘);
其中config/console.php的内容如下
<?php
Yii::setAlias(‘@tests‘, dirname(__DIR__) . ‘/tests‘);
$params = require(__DIR__ . ‘/params.php‘);
return [
‘id‘ => ‘basic-console‘,
‘basePath‘ => dirname(__DIR__),
‘bootstrap‘ => [‘log‘],
‘controllerNamespace‘ => ‘app\commands‘,
‘components‘ => [
‘FileCache‘ => [
‘class‘ => ‘yii\caching\FileCache‘,
],
‘log‘ => [
‘targets‘ => [
[
‘class‘ => ‘yii\log\FileTarget‘,
‘levels‘ => [‘error‘, ‘warning‘],
],
],
],
‘db‘ => [
‘class‘ => ‘yii\db\Connection‘,
‘dsn‘ => ‘mysql:host=localhost;dbname=test‘,
‘emulatePrepare‘ => true,
//‘enableProfiling‘ => true,
//‘enableParamLogging‘ => false,
‘username‘ => ‘test‘,
‘password‘ => ‘test‘,
‘charset‘ => ‘utf8‘,
‘tablePrefix‘ => ‘test_‘,
],
],
‘modules‘ => [
‘Examples‘ => [
‘class‘ => ‘modules\Examples\index‘,
‘db‘ => ‘db‘,
],
],
‘params‘ => $params,
];
其中配置components中的键FileCache,使用的时候这样Yii::$app->FileCache->get("test");
其中的db调用的时候这样\Yii::$app->db->createCommand()->insert($this->tablename, $data)->execute();
最下面的modules模块就是加载模块 在commands里面控制器use的时候使用 不然会报错:class "modules\Test\models\Test" not found
‘modules‘ => [
‘Examples‘ => [
‘class‘ => ‘modules\Examples\index‘,
‘db‘ => ‘db‘,
],
],
2、打开windows命令行 cmd
3、开始在commands目录下面写一个测试控制器 内容如下