YII 配置文件

简介:

用YIIFramework的库开发

Java代码   收藏代码
  1. ....  
  2. Yii::createWebApplication($config); //没有run  

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

注意:你也可以将配置文件分为多个文件, // 例如: db.php, params.php等等

main.php

Java代码   收藏代码
  1. <?php  
  2. // 取消下行的注释,来定义一个路径别名  
  3. // Yii::setPathOfAlias('local','path/to/local-folder');  
  4.   
  5. // 这是 Web 应用配置的主体部分。任何可写的  
  6. // CWebApplication 属性可以在这里配置。  
  7. $config = array(  
  8.     // protected 目录的基础路径  
  9.     // 使用 Yii::app()->basePath 来访问  
  10.     'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',  
  11.   
  12.     // 应用的名字  
  13.     // 使用 Yii::app()->name 来访问  
  14.     'name' => 'My website',  
  15.   
  16.     //路径别名  
  17.     // 可以是应用内部的路径,也可以是外部资源  
  18.     'aliases' => array(  
  19.         'myExternalFramework' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'myexternalframework'  
  20.     ),  
  21.     //维护程序时,这样子所有的请求转发到一个地方  
  22.     'catchAllRequest' => array('site/all'),  
  23.   
  24.     //如何在应用程序处理请求之前执行一段操作?当然这个function方法要存在index.php  
  25.     'onBeginRequest' => 'function',  
  26.   
  27.     //controller path  
  28.     'controllerMap' => array('myController' => 'myExternalFramework.controllers.MyController'),  
  29.   
  30.     // 默认的 controller  
  31.     'defaultController' => 'site',  
  32.   
  33.     // 用户语言(for Locale)  
  34.     'language' => 'es',  
  35.   
  36.     //信息和视图的语言  
  37.     'sourceLanguage' => 'es',  
  38.     'timeZone' => 'Asia/Shanghai',  
  39.     'theme' => 'default',  
  40.     // 使用的字符集  
  41.     'charset' => 'utf-8',  
  42.   
  43.     // 预载入的应用组件  
  44.     'preload' => array('log'),  
  45.   
  46.     // 自动载入的类  
  47.     'import' => array(  
  48.         'application.models.*',  
  49.         'application.components.*',  
  50.     ),  
  51.   
  52.     // 可以使用 Yii::app()->params['paramName'] 访问的应用级别的参数  
  53.     'params' => require(dirname(__FILE__) . '/params.php'),  
  54.     // 在 params.php 中你需要返回这个数组:Yii::app()->setParams设置的只能用Yii::app()->params['xxx']这种数组的方式访问  
  55.     // return array('adminEmail'=>'info@example.com');  
  56.   
  57.     // 应用组件的配置  
  58.     'components' => array(  
  59.         // assets, 参考www.yiiframework.com/doc/api/CAssetManager  
  60.         'assetManager' => array(  
  61.             // 改变磁盘上的路径  
  62.             'basePath' => dirname(__FILE__) . '/../../assets/',  
  63.             // 改变url  
  64.             'baseUrl' => '/web/assets/'  
  65.         ),  
  66.         'request' => array(  
  67.             'enableCsrfValidation' => true//如果防止post跨站攻击  
  68.             'enableCookieValidation' => true//防止Cookie攻击  
  69.         ),  
  70.         // 缓存  
  71.         'cache' => array(  
  72.             'class' => 'A cache class, like: system.caching.CApcCache',  
  73.         ),  
  74.         'session' => array( //  memcache session cache  
  75.             'class' => 'CCacheHttpSession',  
  76.             'autoStart' => 1,  
  77.             'sessionName' => 'frontend',  
  78.             'cookieParams' => array('lifetime' => '3600''path' => '/''domain' => '.test.com''httponly' => '1'),  
  79.             'cookieMode' => 'only',  
  80.         ),  
  81.         // 你可以使用 scriptMap 来配置脚本来自哪里。  
  82.         // 对于一个生产环境的配置,如下  
  83.         'clientScript' => array(  
  84.             'scriptMap' => array(  
  85.                 'register.js' => 'site.min.js',  
  86.                 'login.js' => 'site.min.js',  
  87.             ),  
  88.         ),  
  89.         // 对于一个开发环境,可以这样做  
  90.         'clientScript' => array(  
  91.             'scriptMap' => array(  
  92.                 'register.js' => 'register.js',  
  93.                 'login.js' => 'login.js',  
  94.             ),  
  95.         ),  
  96.     ),  
  97. );  
  98. $database =  require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'db.php');  
  99. if (!empty($database)) {  
  100.     $config['components'] = CMap::mergeArray($config['components'],$database);  
  101. //    Yii::app()->setComponents($database);  
  102. }  
  103. return $config;  

db.php

Java代码   收藏代码
  1. <?php  
  2. return array(  
  3.     'db' => array(  
  4.         'connectionString' => 'mysql:host=192.168.1.240;dbname=tttt',  
  5.         'emulatePrepare' => true,  
  6.         'username' => 'root',  
  7.         'password' => '****',  
  8.         'charset' => 'utf8',  
  9.     ),  
  10.     'card' => array(  
  11.         'class' => 'CDbConnection',//  
  12.         'connectionString' => 'mysql:host=192.168.1.240;dbname=card',  
  13.         'emulatePrepare' => true,  
  14.         'username' => 'root',  
  15.         'password' => '**',  
  16.         'charset' => 'utf8',  
  17.     ),  
  18. );  

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,production, 
  3.  */  
  4. defined('APP_ENV') or define('APP_ENV','development');  
  5.   
  6. // change the following paths if necessary  
  7. if (APP_ENV == 'production') {  
  8.     error_reporting(0);  
  9.     $yii=dirname(__FILE__).'/framework/yiilite.php';  
  10.     defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',1);  
  11. else {  
  12.     $yii=dirname(__FILE__).'/framework/yii.php';  
  13.     // remove the following lines when in production mode  
  14.     defined('YII_DEBUG') or define('YII_DEBUG',true);  
  15.     // specify how many levels of call stack should be shown in each log message  
  16.     defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);  
  17. }  
  18. $config=dirname(__FILE__).'/protected/config/'.APP_ENV.'.php';  
  19. require('path/to/globals.php'); //见附件  
  20. require_once($yii);  
  21. Yii::createWebApplication($config)->run();  

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

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

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.             'log'=>array(  
  8.                 'class'=>'CLogRouter',  
  9.                 'routes'=>array(  
  10.                     array(  
  11.                         'class'=>'CFileLogRoute',  
  12.                         'levels'=>'error, warning',  
  13.                     )  
  14.                 ),  
  15.             ),  
  16.         ),  
  17.     )  
  18. );  
相关文章
|
缓存 数据库连接 测试技术
Yii2的配置文件有哪些?
Yii2的配置文件有哪些?
236 0
|
缓存 数据库连接 PHP
Yii2.0的配置文件是什么?底层原理是什么?
Yii2.0的配置文件是什么?底层原理是什么?
|
测试技术 PHP
Yii2的目录结构是怎样的?
Yii2的目录结构是怎样的?
160 0
|
缓存 安全 数据处理
Yii2相对于Yii1有哪些改进?
Yii2相对于Yii1有哪些改进?
|
Oracle 关系型数据库 MySQL
Yii2的基本要求是什么?
Yii2的基本要求是什么?
119 0
|
缓存 开发框架 安全
Yii2是什么?
Yii2是什么?
243 0
|
安全 应用服务中间件 Apache
Yii2框架(六)Yii2.0框架部署nginx服务器
一直在apache服务器下开发yii2.0的项目,apache下的部署可能没有什么。服务器环境是nginx
263 0
Yii2框架(六)Yii2.0框架部署nginx服务器
Yii2中的环境配置
默认的Debug配置  在入口文件中 defined ( 'YII_DEBUG' ) or define ( 'YII_DEBUG', true ); defined ( 'YII_ENV' ) or define ( 'YII_ENV', 'dev' ); 以上配置后,所有的异常会...
1234 0
Yii2中的入口文件环境配置
默认的Debug配置  在入口文件中 defined ( 'YII_DEBUG' ) or define ( 'YII_DEBUG', true ); defined ( 'YII_ENV' ) or define ( 'YII_ENV', 'dev' ); 以上配置后,所有的异常会...
985 0