---
title: "YII配置"
author: "keshion"
date: "2012-07-31T15:24:25Z"
tags: []
categories: ["编程语言"]
slug: "yii"
draft: false
lastmod: "2012-07-31T15:24:25Z"
id: 1613664
url: "https://keshion.iteye.com/blog/1613664"
---

用YIIFramework的库开发

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

1. ....
2. Yii::createWebApplication($config); //没有run

Yii::import（class1，true）,在将class1类文件路径存储时，同时include该文件

注意：你也可以将配置文件分为多个文件， // 例如： db.php, params.php等等 。Yii::import('db',true); main.php

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

001. &lt;?php
002. // 取消下行的注释，来定义一个路径别名
003. // Yii::setPathOfAlias('local','path/to/local-folder');
004. // 这是 Web 应用配置的主体部分。任何可写的
005. // CWebApplication 属性可以在这里配置。
006. return array(
007. // protected 目录的基础路径
008. // 使用 Yii::app()-&gt;basePath 来访问
009. 'basePath'=&gt;dirname(\_\_FILE\_\_).DIRECTORY\_SEPARATOR.'..',
010. // 应用的名字
011. // 使用 Yii::app()-&gt;name 来访问
012. 'name'=&gt;'My website',
013. //路径别名
014. // 可以是应用内部的路径，也可以是外部资源
015. 'aliases'=&gt;array(
016. 'myExternalFramework'=&gt;dirname(\_\_FILE\_\_).DIRECTORY\_SEPARATOR.'..'.DIRECTORY\_SEPARATOR.'..'.DIRECTORY\_SEPARATOR.'myexternalframework'
017. ),
018. //维护程序时，这样子所有的请求转发到一个地方
019. 'catchAllRequest'=&gt;array('site/all'),
020. //如何在应用程序处理请求之前执行一段操作？当然这个function方法要存在index.php
021. 'onBeginRequest' =&gt; 'function',
022. //building on above for a controller in the external
023. //framework you can use the controller map to map the
024. //controller path
025. 'controllerMap'=&gt;array('myController'=&gt;'myExternalFramework.controllers.MyController'),
026. // 默认的 controller
027. 'defaultController'=&gt;'site',
028. // 用户语言(for Locale)
029. 'language'=&gt;'es',
030. //信息和视图的语言
031. 'sourceLanguage'=&gt;'es',
032. 'timeZone'=&gt;'Asia/Shanghai',
033. 'theme'=&gt;'school',
034. // 使用的字符集
035. 'charset'=&gt;'utf-8',
036. // 预载入的应用组件
037. 'preload'=&gt;array('log'),
038. // 自动载入的类
039. 'import'=&gt;array(
040. 'application.models.\*',
041. 'application.components.\*',
042. ),
043. // 可以使用 Yii::app()-&gt;params\['paramName'] 访问的应用级别的参数
044. 'params'=&gt;array(
045. 'adminEmail'=&gt;'info@example.com',
046. ),
047. // 注意：你也可以将配置文件分为多个文件，
048. // 例如： db.php, params.php 等等。
049. // 你可以这样做：
050. // 'params'=&gt;require(dirname(\_\_FILE\_\_).'/params.php'),
051. // 在 params.php 中你需要返回这个数组：
052. // return array('adminEmail'=&gt;'info@example.com');
053. // 应用组件的配置
054. 'components'=&gt;array(
055. // assets, 参考www.yiiframework.com/doc/api/CAssetManager
056. 'assetManager'=&gt;array(
057. // 改变磁盘上的路径
058. 'basePath'=&gt;dirname(\_\_FILE\_\_).'/../../assets/',
059. // 改变url
060. 'baseUrl'=&gt;'/web/assets/'
061. ),
062. 'errorHandler'=&gt;array(// 用 'site/error' action 处理错误
063. 'errorAction'=&gt;'site/error',
064. ),
065. // 记录
066. 'log'=&gt;array(
067. // 记录器的类
068. 'class'=&gt;'CLogRouter',
069. // 在哪里存储日志
070. 'routes'=&gt;array(
071. array(
072. // 保存到文件中，其他选项是可用的
073. 'class'=&gt;'CFileLogRoute',
074. // 什么内容保存到文件中? error 和 warning, info 和 trace 可以增加到这里
075. 'levels'=&gt;'error, warning',
076. ),
077. ),
078. ),
079. // 用户
080. 'user'=&gt;array(
081. // 启用 cookie-based 验证
082. 'allowAutoLogin'=&gt;true,
083. // 设置需要验证时用户被转到的 url
084. // 使用 null 出现 43 HTTP 错误
085. 'loginUrl'=&gt;null,
086. // 设置一个类的名字，
087. // 这个类扩展自 CWebUser 并且保存在
088. // protected/components/&lt;classname&gt; 中。
089. 'class' =&gt; 'WebUser',
090. ),
091. // 数据库
092. 'db'=&gt;require(dirname(\_\_FILE\_\_).DIRECTORY\_SEPARATOR.'db.php'),
093. // 缓存
094. 'cache'=&gt;array(
095. 'class'=&gt;'A cache class, like: system.caching.CApcCache',
096. ),
097. 'session' =&gt; array( //  memcache session cache
098. 'class' =&gt;'CCacheHttpSession',
099. 'autoStart' =&gt; 1,
100. 'sessionName' =&gt; 'frontend',
101. 'cookieParams' =&gt; array('lifetime'=&gt;'3600','path'=&gt;'/','domain'=&gt;'.test.com','httponly'=&gt;'1'),
102. 'cookieMode' =&gt; 'only',
103. ),
104. // url
105. 'urlManager'=&gt;array(
106. // URL 格式。必须是 'path' 或 'get'。
107. // path: index.php/controller/action/attribute/value
108. // get: index.php?r=controller/action&attribute=value
109. 'urlFormat'=&gt;'path',
110. // 显示为www.example.com/index.php/controller/action
111. // 或www.example.com/controller/action
112. 'showScriptName' =&gt; true,
113. // 转向指定的 url 到你想要的 controller 的规则
114. // 查阅www.yiiframework.com/doc/guide/topics.url
115. 'rules'=&gt;array(
116. //www.example.com/home代替www.example.com/site/index
117. 'home'=&gt;'site/index',
118. 'post/&lt;id:\\d+&gt;'=&gt;'post/show',
119. ),
120. ),
121. // 你可以使用 scriptMap 来配置脚本来自哪里。
122. //If you use the split configurations for development and production you can
123. // have different maps in each and then just load the file and it'll
124. // load the appropriate file depending on the configuration your running.
125. // 对于一个生产环境的配置，如下
126. 'clientScript'=&gt;array(
127. 'scriptMap'=&gt;array(
128. 'register.js'=&gt;'site.min.js',
129. 'login.js'=&gt;'site.min.js',
130. ),
131. ),
132. // 对于一个开发环境，可以这样做
133. 'clientScript'=&gt;array(
134. 'scriptMap'=&gt;array(
135. 'register.js'=&gt;'register.js',
136. 'login.js'=&gt;'login.js',
137. ),
138. ),
139. ),
140. );

params.php

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

01. &lt;?php
02. return array(
03. 'adminEmail'=&gt;'info@example.com',
04. 'pagesize'=&gt;'100',
05. 'pager'=&gt;array(
06. 'class'=&gt;'PagerWidget',
07. 'maxButtonCount'=&gt;8,
08. 'firstPageLabel'=&gt;'首页',
09. 'lastPageLabel'=&gt;'末页',
10. 'nextPageLabel'=&gt;'下一页',
11. 'prevPageLabel'=&gt;'上一页',
12. 'header'=&gt;'',
13. 'cssFile'=&gt;false,
14. ),
15. );

**index.php**   
配置环境常量，不同环境调用不同配置文件和调试级别。

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

01. /\**
02. * 应用程序环境，可选：development,test,production,
03. \*/
04. defined('APP\_ENV') or define('APP\_ENV','development');
05. // change the following paths if necessary
06. if (APP\_ENV == 'production') {
07. error\_reporting(0);
08. $yii=dirname(\_\_FILE\_\_).'/../yii-svn/framework/yiilite.php';
09. defined('YII\_TRACE\_LEVEL') or define('YII\_TRACE\_LEVEL',1);
10. } else {
11. ini\_set('error\_reporting',E\_ALL);
12. ini\_set('display\_errors',1);
13. $yii=dirname(\_\_FILE\_\_).'/../yii-svn/framework/yii.php';
14. // remove the following lines when in production mode
15. defined('YII\_DEBUG') or define('YII\_DEBUG',true);
16. // specify how many levels of call stack should be shown in each log message
17. defined('YII\_TRACE\_LEVEL') or define('YII\_TRACE\_LEVEL',3);
18. }
19. $config=dirname(\_\_FILE\_\_).'/protected/config/'.APP\_ENV.'.php';
20. require\_once($yii);
21. Yii::createWebApplication($config)-&gt;run();

**development.php**   
开启weblog,profile，数据库性能显示，数据库查询参数记录，GII

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

01. &lt;?php
02. return CMap::mergeArray(
03. require(dirname(\_\_FILE\_\_).'/main.php'),
04. array(
05. 'components'=&gt;array(
06. // uncomment the following to use a MySQL database
07. 'db'=&gt;require(dirname(\_\_FILE\_\_).'/db.php'),
08. 'log'=&gt;array(
09. 'class'=&gt;'CLogRouter',
10. 'routes'=&gt;array(
11. array(
12. 'class'=&gt;'CFileLogRoute',
13. 'levels'=&gt;'error, warning',
14. ),
15. array(
16. 'class'=&gt;'CProfileLogRoute',
17. ),
18. array(
19. 'class'=&gt;'CWebLogRoute',
20. ),
21. ),
22. ),
23. ),
24. 'modules'=&gt;array(
25. // uncomment the following to enable the Gii tool
26. 'gii'=&gt;array(
27. 'class'=&gt;'system.gii.GiiModule',
28. 'password'=&gt;'sa',
29. // If removed, Gii defaults to localhost only. Edit carefully to taste.
30. 'ipFilters'=&gt;array('127.0.0.1','::1'),
31. ),
32. ),
33. )
34. );

**production.php**   
开启数据库结构缓存，开启CEAcceleratorCache，关闭错误显示

Java代码  [![收藏代码](http://hudeyong926.iteye.com/images/icon_star.png)](http://hudeyong926.iteye.com/blog/1484367 "收藏这段代码")

01. &lt;?php
02. return CMap::mergeArray(
03. require(dirname(\_\_FILE\_\_).'/main.php'),
04. array(
05. 'components'=&gt;array(
06. // uncomment the following to use a MySQL database
07. 'db'=&gt;require(dirname(\_\_FILE\_\_).'/db.php'),
08. 'log'=&gt;array(
09. 'class'=&gt;'CLogRouter',
10. 'routes'=&gt;array(
11. array(
12. 'class'=&gt;'CFileLogRoute',
13. 'levels'=&gt;'error, warning',
14. )
15. ),
16. ),
17. 'cache'=&gt;array(
18. 'class'=&gt;'system.caching.CEAcceleratorCache',
19. ),
20. ),
21. )
22. );