ITeye博客

YII配置

keshion · 2012-07-31 15:24 · Yii · 编程语言 · Markdown

用YIIFramework的库开发

Java代码  收藏代码

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

Yii::import(class1,true),在将class1类文件路径存储时,同时include该文件

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

Java代码  收藏代码

  1. <?php
  2. // 取消下行的注释,来定义一个路径别名
  3. // Yii::setPathOfAlias('local','path/to/local-folder');
  4. // 这是 Web 应用配置的主体部分。任何可写的
  5. // CWebApplication 属性可以在这里配置。
  6. return array(
  7. // protected 目录的基础路径
  8. // 使用 Yii::app()->basePath 来访问
  9. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
  10. // 应用的名字
  11. // 使用 Yii::app()->name 来访问
  12. 'name'=>'My website',
  13. //路径别名
  14. // 可以是应用内部的路径,也可以是外部资源
  15. 'aliases'=>array(
  16. 'myExternalFramework'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'myexternalframework'
  17. ),
  18. //维护程序时,这样子所有的请求转发到一个地方
  19. 'catchAllRequest'=>array('site/all'),
  20. //如何在应用程序处理请求之前执行一段操作?当然这个function方法要存在index.php
  21. 'onBeginRequest' => 'function',
  22. //building on above for a controller in the external
  23. //framework you can use the controller map to map the
  24. //controller path
  25. 'controllerMap'=>array('myController'=>'myExternalFramework.controllers.MyController'),
  26. // 默认的 controller
  27. 'defaultController'=>'site',
  28. // 用户语言(for Locale)
  29. 'language'=>'es',
  30. //信息和视图的语言
  31. 'sourceLanguage'=>'es',
  32. 'timeZone'=>'Asia/Shanghai',
  33. 'theme'=>'school',
  34. // 使用的字符集
  35. 'charset'=>'utf-8',
  36. // 预载入的应用组件
  37. 'preload'=>array('log'),
  38. // 自动载入的类
  39. 'import'=>array(
  40. 'application.models.*',
  41. 'application.components.*',
  42. ),
  43. // 可以使用 Yii::app()->params['paramName'] 访问的应用级别的参数
  44. 'params'=>array(
  45. 'adminEmail'=>'info@example.com',
  46. ),
  47. // 注意:你也可以将配置文件分为多个文件,
  48. // 例如: db.php, params.php 等等。
  49. // 你可以这样做:
  50. // 'params'=>require(dirname(__FILE__).'/params.php'),
  51. // 在 params.php 中你需要返回这个数组:
  52. // return array('adminEmail'=>'info@example.com');
  53. // 应用组件的配置
  54. 'components'=>array(
  55. // assets, 参考www.yiiframework.com/doc/api/CAssetManager
  56. 'assetManager'=>array(
  57. // 改变磁盘上的路径
  58. 'basePath'=>dirname(__FILE__).'/../../assets/',
  59. // 改变url
  60. 'baseUrl'=>'/web/assets/'
  61. ),
  62. 'errorHandler'=>array(// 用 'site/error' action 处理错误
  63. 'errorAction'=>'site/error',
  64. ),
  65. // 记录
  66. 'log'=>array(
  67. // 记录器的类
  68. 'class'=>'CLogRouter',
  69. // 在哪里存储日志
  70. 'routes'=>array(
  71. array(
  72. // 保存到文件中,其他选项是可用的
  73. 'class'=>'CFileLogRoute',
  74. // 什么内容保存到文件中? error 和 warning, info 和 trace 可以增加到这里
  75. 'levels'=>'error, warning',
  76. ),
  77. ),
  78. ),
  79. // 用户
  80. 'user'=>array(
  81. // 启用 cookie-based 验证
  82. 'allowAutoLogin'=>true,
  83. // 设置需要验证时用户被转到的 url
  84. // 使用 null 出现 43 HTTP 错误
  85. 'loginUrl'=>null,
  86. // 设置一个类的名字,
  87. // 这个类扩展自 CWebUser 并且保存在
  88. // protected/components/<classname> 中。
  89. 'class' => 'WebUser',
  90. ),
  91. // 数据库
  92. 'db'=>require(dirname(__FILE__).DIRECTORY_SEPARATOR.'db.php'),
  93. // 缓存
  94. 'cache'=>array(
  95. 'class'=>'A cache class, like: system.caching.CApcCache',
  96. ),
  97. 'session' => array( //  memcache session cache
  98. 'class' =>'CCacheHttpSession',
  99. 'autoStart' => 1,
  100. 'sessionName' => 'frontend',
  101. 'cookieParams' => array('lifetime'=>'3600','path'=>'/','domain'=>'.test.com','httponly'=>'1'),
  102. 'cookieMode' => 'only',
  103. ),
  104. // url
  105. 'urlManager'=>array(
  106. // URL 格式。必须是 'path' 或 'get'。
  107. // path: index.php/controller/action/attribute/value
  108. // get: index.php?r=controller/action&attribute=value
  109. 'urlFormat'=>'path',
  110. // 显示为www.example.com/index.php/controller/action
  111. // 或www.example.com/controller/action
  112. 'showScriptName' => true,
  113. // 转向指定的 url 到你想要的 controller 的规则
  114. // 查阅www.yiiframework.com/doc/guide/topics.url
  115. 'rules'=>array(
  116. //www.example.com/home代替www.example.com/site/index
  117. 'home'=>'site/index',
  118. 'post/<id:\d+>'=>'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'=>array(
  127. 'scriptMap'=>array(
  128. 'register.js'=>'site.min.js',
  129. 'login.js'=>'site.min.js',
  130. ),
  131. ),
  132. // 对于一个开发环境,可以这样做
  133. 'clientScript'=>array(
  134. 'scriptMap'=>array(
  135. 'register.js'=>'register.js',
  136. 'login.js'=>'login.js',
  137. ),
  138. ),
  139. ),
  140. );

params.php

Java代码  收藏代码

  1. <?php
  2. return array(
  3. 'adminEmail'=>'info@example.com',
  4. 'pagesize'=>'100',
  5. 'pager'=>array(
  6. 'class'=>'PagerWidget',
  7. 'maxButtonCount'=>8,
  8. 'firstPageLabel'=>'首页',
  9. 'lastPageLabel'=>'末页',
  10. 'nextPageLabel'=>'下一页',
  11. 'prevPageLabel'=>'上一页',
  12. 'header'=>'',
  13. 'cssFile'=>false,
  14. ),
  15. );

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

Java代码  收藏代码

  1. /**
  2. * 应用程序环境,可选:development,test,production,
  3. */
  4. defined('APP_ENV') or define('APP_ENV','development');
  5. // change the following paths if necessary
  6. if (APP_ENV == 'production') {
  7. error_reporting(0);
  8. $yii=dirname(__FILE__).'/../yii-svn/framework/yiilite.php';
  9. 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)->run();

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

Java代码  收藏代码

  1. <?php
  2. return CMap::mergeArray(
  3. require(dirname(__FILE__).'/main.php'),
  4. array(
  5. 'components'=>array(
  6. // uncomment the following to use a MySQL database
  7. 'db'=>require(dirname(__FILE__).'/db.php'),
  8. 'log'=>array(
  9. 'class'=>'CLogRouter',
  10. 'routes'=>array(
  11. array(
  12. 'class'=>'CFileLogRoute',
  13. 'levels'=>'error, warning',
  14. ),
  15. array(
  16. 'class'=>'CProfileLogRoute',
  17. ),
  18. array(
  19. 'class'=>'CWebLogRoute',
  20. ),
  21. ),
  22. ),
  23. ),
  24. 'modules'=>array(
  25. // uncomment the following to enable the Gii tool
  26. 'gii'=>array(
  27. 'class'=>'system.gii.GiiModule',
  28. 'password'=>'sa',
  29. // If removed, Gii defaults to localhost only. Edit carefully to taste.
  30. 'ipFilters'=>array('127.0.0.1','::1'),
  31. ),
  32. ),
  33. )
  34. );

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

Java代码  收藏代码

  1. <?php
  2. return CMap::mergeArray(
  3. require(dirname(__FILE__).'/main.php'),
  4. array(
  5. 'components'=>array(
  6. // uncomment the following to use a MySQL database
  7. 'db'=>require(dirname(__FILE__).'/db.php'),
  8. 'log'=>array(
  9. 'class'=>'CLogRouter',
  10. 'routes'=>array(
  11. array(
  12. 'class'=>'CFileLogRoute',
  13. 'levels'=>'error, warning',
  14. )
  15. ),
  16. ),
  17. 'cache'=>array(
  18. 'class'=>'system.caching.CEAcceleratorCache',
  19. ),
  20. ),
  21. )
  22. );

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。