转自http://blog.lixiphp.com/phpstorm-yii-command-line/
PHPStorm 虽然在框架整合(Framework Integration)上支持Yii 框架,但是PhpStorm 的命令行工具(Command Line Tools)并不支持Yii 框架。
为了充分利用命令行工具来快速操作,可以自行添加。
PhpStorm 安装 Yii 命令行工具
你可以通过 IDE Settings | Command Line Tool Support 来添加Yii 命令,请通过以下步骤来实现:
1)第一步,在Command Line Tool Support 设置页面,点击 + 按钮,弹出Command Line Tool 窗口。
你可以选择这个命令行工具是针对当前项目还是全部的项目,这里设置推荐设置为Global,因为以后随时都可以使用,不用每次再单独设置。
2)第二步,根据上图点击“OK”按钮后,出现“Tool Settings”设置窗口。
- Tool path 设置为你Yii 框架的命令行路径,Windows下设置yiic.bat 的绝对路径,Linux下设置为yiic 的绝对路径;
- Alias 设置为yii,以后在命令控制台输入yii 即可调用命令;
- Description 设置为Yii,可选设置,你可以不用设置;
设置好,点击“OK”按钮,即可看到Command Line Tool Support 窗口中显示出刚刚添加的yii 命令行工具。
上图中,在有阴影的那一行,就是我们刚刚添加的。
3)第三步,点击当前窗口的“OK”或者 “Apply”按钮,即完成Yii 命令行工具的安装。
通过打开“open definition in Editor”,我们可以得到这个命令行的定义文件。
<?xml version="1.0" encoding="UTF-8"?>
<framework xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schemas/frameworkDescriptionVersion1.1.3.xsd" name="Custom_yii" invoke="E:\USB\wnmp\www\framework\yii\framework\yiic.bat" alias="yii" enabled="true" version="2">
<help><![CDATA[Yii]]></help>
</framework>
在这个文件中再继续定义。
PhpStorm 使用 Yii 命令行工具
这里我只列出几个简单的使用作为演示。
在PHPStorm 使用命令行工具可以通过Tools | Run Command… 菜单或者使用快捷键 Ctrl+Shift+X (Cmd+Shift+X on Mac)。
在命令控制台输入:
yii help
该条命令是用于测试yii 命令行工具是否在工作,得到下面的结果说明你的安装是正确的,
> E:\USB\wnmp\www\framework\yii\framework\yiic.bat help
Yii command runner (based on Yii v1.1.13)
Usage: E:\USB\wnmp\www\framework\yii\framework\yiic <command-name> [parameters...]The following commands are available:
- message
- migrate
- shell
- webappTo see individual command help, use the following:
E:\USB\wnmp\www\framework\yii\framework\yiic help <command-name>Process finished with exit code 0 at 20:39:55.
Execution time: 4,855 ms.
每次运行命令后,会再最后一行显示执行时间。
根据上一条命令,我们继续在命令控制台输入:
yii help shell
该条命令是用于列出yii shell命令的帮助信息,结果如下:
> E:\USB\wnmp\www\framework\yii\framework\yiic.bat help shell
USAGE
yiic shell [entry-script | config-file]DESCRIPTION
This command allows you to interact with a Web application
on the command line. It also provides tools to automatically
generate new controllers, views and data models.It is recommended that you execute this command under
the directory that contains the entry script file of
the Web application.
PARAMETERS
* entry-script | config-file: optional, the path to
the entry script file or the configuration file for
the Web application. If not given, it is assumed to be
the ‘index.php’ file under the current directory.
Process finished with exit code 0 at 20:40:24.
Execution time: 3,409 ms.
根据以上输出,命令yii shell 允许您与一个Web应用程序交互。
紧接着,我们用yii 创建一个网页应用程序(Web application)。
> cd demos
Current working directory is ‘E:\USB\wnmp\www\framework\yii\demos’.
使用cd 命令进入到某个目录,命令控制台会输出当前的工作目录。如果不使用cd 某个目录的话,会在项目的根目录直接创建应用。
继续在命令控制台输入:
yii webapp test
命令行开始执行创建一个网页应用程序(Web application) test,结果如下:
> E:\USB\wnmp\www\framework\yii\framework\yiic.bat webapp test
Create a Web application under ‘E:\USB\wnmp\www\framework\yii\demos\test’? (yes|no) [no]:yes
mkdir E:/USB/wnmp/www/framework/yii/demos/test
mkdir E:/USB/wnmp/www/framework/yii/demos/test/assets
mkdir E:/USB/wnmp/www/framework/yii/demos/test/css
generate css/bg.gif
generate css/form.css
generate css/ie.css
generate css/main.css
generate css/print.css
generate css/screen.css
mkdir E:/USB/wnmp/www/framework/yii/demos/test/images
generate index-test.php
generate index.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected
generate protected/.htaccess
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/commands
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/commands/shell
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/components
generate protected/components/Controller.php
generate protected/components/UserIdentity.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/config
generate protected/config/console.php
generate protected/config/main.php
generate protected/config/test.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/controllers
generate protected/controllers/SiteController.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/data
generate protected/data/schema.mysql.sql
generate protected/data/schema.sqlite.sql
generate protected/data/testdrive.db
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/extensions
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/messages
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/migrations
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/models
generate protected/models/ContactForm.php
generate protected/models/LoginForm.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/runtime
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/tests
generate protected/tests/bootstrap.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/tests/fixtures
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/tests/functional
generate protected/tests/functional/SiteTest.php
generate protected/tests/phpunit.xml
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/tests/report
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/tests/unit
generate protected/tests/WebTestCase.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/views
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/views/layouts
generate protected/views/layouts/column1.php
generate protected/views/layouts/column2.php
generate protected/views/layouts/main.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/views/site
generate protected/views/site/contact.php
generate protected/views/site/error.php
generate protected/views/site/index.php
generate protected/views/site/login.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/protected/views/site/pages
generate protected/views/site/pages/about.php
generate protected/yiic
generate protected/yiic.bat
generate protected/yiic.php
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes/classic
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes/classic/views
generate themes/classic/views/.htaccess
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes/classic/views/layouts
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes/classic/views/site
mkdir E:/USB/wnmp/www/framework/yii/demos/test/themes/classic/views/systemYour application has been created successfully under E:\USB\wnmp\www\framework\yii\demos\test.
Process finished with exit code 0 at 20:43:53.
Execution time: 7,323 ms.
中间会提示你是否真正安装,输入“Yes”。
在浏览器中输入网址:http://localhost/demos/test,即可得到一个初始化的网页应用程序(Web application)。
这样,通过本文你实现了在PhpStorm IDE中使用Yii 命令行工具创建一个网页应用程序。
关于Yii其他命令使用方法,请查看Yii框架帮助文档。