欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-12 20:21:52
本文实例讲述了yii framework框架使用yiic快速创建yii应用的方法。分享给大家供大家参考,具体如下: yii提供了强大的命令行工具来快速的创建相关组件和应用...

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

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

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

执行

# php yiic
yii command runner (based on yii v1.1.8)
usage: yiic <command-name> [parameters...]

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

to see individual command help, use the following:
  yiic help <command-name>

可以看到

yiic的基本命令参数规则

yiic <command-name> [parameters...]

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

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

yiic help <command-name>

例如:

php yiic shell

1.php yiic webapp

执行 php yiic help webapp

/* 
usage 
 yiic webapp <app-path> 
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 <config-file> 
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'=>'webmaster@example.com',
),
'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程序设计有所帮助。

上一篇:

下一篇: