当前位置: 首页 > 编程文章 >

YII Framework框架教程之使用YIIC快速创建YII应用详解

YII Framework框架教程之使用YIIC快速创建YII应用详解

作者:陈言时间:2025-07-09 14:50:02浏览:655

本文介绍如何使用YII Framework提供的yiic命令行工具快速生成完整的Web应用程序结构,并演示如何通过message命令实现应用的国际化功能。通过配置config.php模板和修改视图代码中的Yii::t调用,可将页面文本如Username和Password翻译成中文,同时需注意文件编码一致性以避免乱码问题。

本文目录展开

YIIC命令行工具简介

本实例详细解析了利用YII Framework框架中的YIIC工具迅速构建YII应用的具体方法,供读者参考学习。

YII框架内置了功能强大的命令行接口,能够高效地生成各类组件及应用项目。

首先,需通过终端cd命令进入yii/framework框架所在的根目录。

在控制台中输入以下指令:


# php yiic
Yii command runner (based on Yii v1.1.8)
Usage: yiic  [parameters...]

The following commands are available:
 - message
 - migrate
 - shell
 - webapp

To see individual command help, use the following:
  yiic help 

运行后屏幕将显示如下输出内容:

关于yiic的基本命令参数规范说明。

其标准语法格式为:yiic [parameters...]

其中command-name代表具体的子命令,当前可用的选项包括: - message - migrate - shell - webapp

若需了解特定命令的详细功能说明,可执行以下操作:

运行命令:yiic help

具体示例如下:


php yiic shell

1. 使用 yiic webapp 命令

执行 php yiic help webapp 可查看该命令的帮助信息。


/* 
USAGE 
 yiic webapp  
DESCRIPTION 
 This command generates an Yii Web Application at the specified location. 
PARAMETERS 
 * app-path: required, the directory where the new application will be created. 
  If the directory does not exist, it will be created. After the application 
  is created, please make sure the directory can be accessed by Web users. 
*/ 

此命令用于在指定路径下生成一个具备完整结构的基于Yii的Web应用。参数app-path指代Web应用的存放目录;若该目录不存在,系统会自动创建。

例如:


/yii/framework# php yiic webapp ../../testwebap
 Create a Web application under '/home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap'? [Yes|No] Yes
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap
  generate index-test.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/layouts
  generate themes/classic/views/.htaccess
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/site
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/system
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/css
  generate css/ie.css
  generate css/bg.gif
  generate css/screen.css
  generate css/form.css
  generate css/print.css
  generate css/main.css
  generate index.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/assets
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models
  generate protected/models/LoginForm.php
  generate protected/models/ContactForm.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components
  generate protected/components/UserIdentity.php
  generate protected/components/Controller.php
  generate protected/yiic.bat
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/controllers
  generate protected/controllers/SiteController.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/migrations
  generate protected/yiic
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/data
  generate protected/data/testdrive.db
  generate protected/data/schema.mysql.sql
  generate protected/data/schema.sqlite.sql
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config
  generate protected/config/test.php
  generate protected/config/console.php
  generate protected/config/main.php
  generate protected/.htaccess
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts
  generate protected/views/layouts/column1.php
  generate protected/views/layouts/column2.php
  generate protected/views/layouts/main.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site
  generate protected/views/site/contact.php
  generate protected/views/site/login.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/pages
  generate protected/views/site/pages/about.php
  generate protected/views/site/index.php
  generate protected/views/site/error.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/messages
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests
  generate protected/tests/bootstrap.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/fixtures
  generate protected/tests/phpunit.xml
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/report
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/unit
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/functional
  generate protected/tests/functional/SiteTest.php
  generate protected/tests/WebTestCase.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/runtime
  generate protected/yiic.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/extensions
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/commands
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/commands/shell
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/images

Your application has been created successfully under /yii_dev/testwebap.

生成的目录层级结构如下所示:

testwebap │ ├── assets │ ├── css │ ├── images │ ├── index.php │ ├── index-test.php │ ├── protected │ └── themes 详细目录结构如下 ├── assets ├── css │ ├── bg.gif │ ├── form.css │ ├── ie.css │ ├── main.css │ ├── print.css │ └── screen.css ├── images ├── index.php ├── index-test.php ├── protected │ ├── commands │ │ └── shell │ ├── components │ │ ├── Controller.php │ │ └── UserIdentity.php │ ├── config │ │ ├── console.php │ │ ├── main.php │ │ └── test.php │ ├── controllers │ │ └── SiteController.php │ ├── data │ │ ├── schema.mysql.sql │ │ ├── schema.sqlite.sql │ │ └── testdrive.db │ ├── extensions │ ├── messages │ ├── migrations │ ├── models │ │ ├── ContactForm.php │ │ └── LoginForm.php │ ├── runtime │ ├── tests │ │ ├── bootstrap.php │ │ ├── fixtures │ │ ├── functional │ │ │ └── SiteTest.php │ │ ├── phpunit.xml │ │ ├── report │ │ ├── unit │ │ └── WebTestCase.php │ ├── views │ │ ├── layouts │ │ │ ├── column1.php │ │ │ ├── column2.php │ │ │ └── main.php │ │ └── site │ │ ├── contact.php │ │ ├── error.php │ │ ├── index.php │ │ ├── login.php │ │ └── pages │ │ └── about.php │ ├── yiic │ ├── yiic.bat │ └── yiic.php └── themes └── classic └── views ├── layouts ├── site └── system

接下来可以通过浏览器进行访问测试。

访问地址:http://www.localyii.com/testwebap/

关于应用的具体功能将在后续章节展开,此处仅讲解命令行的基本使用方法。

2. 使用 yiic message 命令

运行 php yiic help message 以打印message命令的相关用法说明。


/* 
USAGE 
 yiic message  
DESCRIPTION 
 This command searches for messages to be translated in the specified 
 source files and compiles them into PHP arrays as message source. 
PARAMETERS 
 * config-file: required, the path of the configuration file. You can find 
  an example in framework/messages/config.php. 
  The file can be placed anywhere and must be a valid PHP script which 
  returns an array of name-value pairs. Each name-value pair represents 
  a configuration option. 
  The following options are available: 
  - sourcePath: string, root directory of all source files. 
  - messagePath: string, root directory containing message translations. 
  - languages: array, list of language codes that the extracted messages 
   should be translated to. For example, array('zh_cn','en_au'). 
  - fileTypes: array, a list of file extensions (e.g. 'php', 'xml'). 
   Only the files whose extension name can be found in this list 
   will be processed. If empty, all files will be processed. 
  - exclude: array, a list of directory and file exclusions. Each 
   exclusion can be either a name or a path. If a file or directory name 
   or path matches the exclusion, it will not be copied. For example, 
   an exclusion of '.svn' will exclude all files and directories whose 
   name is '.svn'. And an exclusion of '/a/b' will exclude file or 
   directory 'sourcePath/a/b'. 
  - translator: the name of the function for translating messages. 
   Defaults to 'Yii::t'. This is used as a mark to find messages to be 
   translated. 
  - overwrite: if message file must be overwritten with the merged messages. 
*/ 

该命令主要应用于Web应用的国际化处理。

例如针对上述生成的testwebap项目中的页面: http://www.localyii.com/testwebap/index.php?r=site/login

需要将登录页面的Username和Password字段文本翻译成中文。

根据前述说明,此过程需要配置文件支持。YII框架已提供配置文件的模板。

模板文件位于:framework/messages/config.php

将 framework\messages\config.php 文件复制至 protected\messages\ 目录下。

对复制后的文件内容进行如下修改:


dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 
  'messagePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'messages', 
  'languages'=>array('zh_cn'), 
  'fileTypes'=>array('php'), 
  'overwrite'=>true, 
  'exclude'=>array( 
    '.svn', 
    'yiilite.php', 
    'yiit.php', 
    '/i18n/data', 
    '/messages', 
    '/vendors', 
    '/web/js', 
  ), 
); 

目标源文件路径:/www/yii_dev/testwebap/protected/views/site/login.php

对该文件做如下修改:


labelEx($model,Yii::t('login_message','username')); ?>
textField($model,'username'); ?>
error($model,'username'); ?>
labelEx($model,Yii::t('login_message','password')); ?>
passwordField($model,'password'); ?>
error($model,'password'); ?>

随后执行相应的命令:


testwebap/protected/messages
/www/yii_dev/yii/framework# php yiic message "../../testwebap/protected/messages/config.php"
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/Controller.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/UserIdentity.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/console.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/main.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/test.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/controllers/SiteController.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/ContactForm.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/LoginForm.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/WebTestCase.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/bootstrap.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/functional/SiteTest.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column1.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column2.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/main.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/contact.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/error.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/index.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/login.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/pages/about.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/yiic.php...
Saving messages to /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/messages/../messages/zh_cn/login_message.php...saved.

执行后目录下会生成如下文件:

新生成的文件路径:/www/yii_dev/testwebap/protected/messages/zh_cn/login_message.php


return array (
'password' => '',
'username' => '',
);

将其内容改为对应的中文翻译值。


return array (
'password' => '密码',
'username' => '用户名',
);

接着修改配置文件。

编辑以下配置文件:

/www/yii_dev/testwebap/protected/config/main.php

在其中添加如下配置项:


// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
'language'=>'zh_cn',
'sourceLanguage'=>'en_us',
);

再次访问页面。

地址:http://www.localyii.com/testwebap/index.php?r=site/login

此时可以看到页面文本已成功翻译成中文。

若出现乱码现象,请注意检查编码设置,确保文件保存编码与浏览器显示编码保持一致。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

上一篇
CodeIgniter框架整合Widget实现页面布局的方法

在Web开发中,视图文件通常包含头部、侧边栏和底部等公共元素。通过引入Widget机制并结合CodeIgniter的MY_Controller,可以有效提升视图套用的效率与代码的敏捷性。本文介绍了如何在CI框架中定义Widget类并在视图中调用,以实现模块化的页面布局管理。

下一篇
WordPress主题实现单双侧边栏动态切换的PHP方案

本文介绍如何在WordPress Classic主题中通过PHP脚本实现单侧边栏与双侧边栏的动态切换。核心思路是在后台添加管理选项以控制侧边栏数量,在前台根据该数值动态注册不同的Widget区域(如Sidebar_single或Sidebar_top/bottom),并通过条件判断合并或分离HTML结构,从而在不修改大量代码的情况下灵活调整布局。

相关文章

更多
Yii框架CGridView底部功能菜单实现方法

本文介绍如何在Yii框架中通过修改基类文件framework/zii/widgets/grid/CGridView.php来扩展CGridView组件,从而实现自定义底部功能菜单。具体包括重写renderFooterCell方法以支持底部单元格渲染,并在视图配置中使用CCheckBoxColumn结合footerHtmlOptions属性定义包含按钮的底部区域,为开发者提供灵活的列表页操作界面定制方案。

2025.09.24 935
解决yiic命令提示php.exe不是内部或外部命令的方法

在CMD中运行yiic webapp时若提示“php.exe”不是内部或外部命令,通常因系统未配置PHP路径或yiic.bat脚本引用错误。可通过修改系统环境变量PATH或在yii/framework/yiic.bat中指定php.exe绝对路径来解决该问题。

2026.07.15 837
PHP strncasecmp函数:高效进行不区分大小写的字符串前缀比较

在PHP开发中,处理字符集标识(如UTF-8)或特定前缀匹配时,直接使用strtolower转换往往效率较低。strncasecmp函数允许指定比较长度并忽略大小写,返回0表示相等,负数表示前者小,正数表示后者大。该函数二进制安全且常用于框架底层逻辑,如Yii Framework中的事件判断。

2025.03.20 748
PHPUnit安装配置与基础测试用例编写指南

PHPUnit是Zend官方支持的PHP测试框架,通过高质量单元测试可有效减少Bug并改善程序质量。本文介绍在PHP目录下使用Pear命令安装PHPUnit的方法,以及Windows环境下配置PATH变量的步骤。同时解析了基于PHPUnit_Framework_TestCase基类、test*命名规范及assertEquals断言方法的基础测试用例编写规律。

2025.07.11 650
Entity Framework 6 连接 MySQL 的配置与常见错误排查

本文介绍在 .NET 项目中通过 Entity Framework 6 连接 MySQL 数据库的完整流程。主要内容包括使用 NuGet 安装 MySql.Data.Entity 包、定义实体类并添加长度限制以解决 key length 错误、配置 MyContext 继承 MySqlEFConfiguration,以及在 Web.config 中添加必要的 provider 和 connectionStrings 配置。同时针对 Model compatibility 检查和关联映射中的 WithMany/WithOptional 报错提供具体修复方案。

2026.02.09 610
微软agent-framework PyPI后门与ClawHub投毒

奇安信威胁情报中心指出,OpenClaw的ClawHub技能市场遭遇批量恶意Skill上架,同时微软agent-framework在PyPI发布的1.26.1和1.26.2版本被植入后门。这两个版本通过.pth文件在Python启动时自动执行恶意代码,无需业务导入即可触发。受影响的系统需立即回退至1.26.0版本,并全面轮换包括环境变量、CI令牌及托管身份在内的所有可达凭据,以消除潜在风险。

2026.11.27 551

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。