侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

框架(架构)  /  管理员 发布于 7年前   190

本文实例讲述了YII Framework框架使用YIIC快速创建YII应用的方法。分享给大家供大家参考,具体如下:

yii提供了强大的命令行工具来快速的创建相关组件和应用。 

cd进入yii/framework框架的根目录。

执行

# php yiicYii command runner (based on Yii v1.1.8)Usage: yiic  [parameters...]The following commands are available: - message - migrate - shell - webappTo see individual command help, use the following:  yiic help 

可以看到

yiic的基本命令参数规则

yiic [parameters...]

command-name对应的有
 - message
 - migrate
 - shell
 - webapp

如果要查看具体的命令的功能可以用

yiic help

例如:

php yiic shell

1.php 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的完整结构的webapp。app-path是webapp的存放目录。如果目录不存在会被创建。

例如:

/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.php yiic messag

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. */ 

主要用于webapp的国际化。

例如将上面生产的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'=>'[email protected]',),'language'=>'zh_cn','sourceLanguage'=>'en_us',);

访问一一下

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

可以看到翻译成中文了。

可以有乱码的问题,注意编码问题。文件,浏览器要设置统一

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

您可能感兴趣的文章:

  • yii2框架中使用下拉菜单的自动搜索yii-widget-select2实例分析
  • Yii中创建自己的Widget实例
  • yii中widget的用法
  • PHP的Yii框架中创建视图和渲染视图的方法详解
  • YII Framework框架使用YIIC快速创建YII应用之migrate用法实例详解
  • Yii中Model(模型)的创建及使用方法
  • yii实现创建验证码实例解析
  • yii框架通过控制台命令创建定时任务示例
  • yii框架源码分析之创建controller代码
  • yii2.0实现创建简单widgets示例


  • 上一条:
    YII Framework框架使用YIIC快速创建YII应用之migrate用法实例详解
    下一条:
    YII Framework框架教程之国际化实现方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客