`

YII配置

    博客分类:
  • Yii
阅读更多

用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.   
  5. // 这是 Web 应用配置的主体部分。任何可写的  
  6. // CWebApplication 属性可以在这里配置。  
  7. return 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.     //building on above for a controller in the external  
  28.     //framework you can use the controller map to map the  
  29.     //controller path  
  30.     'controllerMap'=>array('myController'=>'myExternalFramework.controllers.MyController'),  
  31.   
  32.     // 默认的 controller  
  33.     'defaultController'=>'site',  
  34.   
  35.     // 用户语言(for Locale)  
  36.     'language'=>'es',  
  37.   
  38.     //信息和视图的语言  
  39.     'sourceLanguage'=>'es',  
  40.     'timeZone'=>'Asia/Shanghai',  
  41.     'theme'=>'school',  
  42.     // 使用的字符集  
  43.     'charset'=>'utf-8',  
  44.   
  45.     // 预载入的应用组件  
  46.     'preload'=>array('log'),  
  47.   
  48.     // 自动载入的类  
  49.     'import'=>array(  
  50.         'application.models.*',  
  51.         'application.components.*',  
  52.     ),  
  53.   
  54.     // 可以使用 Yii::app()->params['paramName'] 访问的应用级别的参数  
  55.     'params'=>array(  
  56.         'adminEmail'=>'info@example.com',  
  57.     ),  
  58.     // 注意:你也可以将配置文件分为多个文件,  
  59.     // 例如: db.php, params.php 等等。  
  60.     // 你可以这样做:  
  61.     // 'params'=>require(dirname(__FILE__).'/params.php'),  
  62.     // 在 params.php 中你需要返回这个数组:  
  63.     // return array('adminEmail'=>'info@example.com');  
  64.   
  65.     // 应用组件的配置  
  66.     'components'=>array(  
  67.         // assets, 参考www.yiiframework.com/doc/api/CAssetManager  
  68.         'assetManager'=>array(  
  69.             // 改变磁盘上的路径  
  70.             'basePath'=>dirname(__FILE__).'/../../assets/',  
  71.             // 改变url  
  72.             'baseUrl'=>'/web/assets/'  
  73.         ),  
  74.         'errorHandler'=>array(// 用 'site/error' action 处理错误  
  75.             'errorAction'=>'site/error',  
  76.         ),  
  77.         // 记录  
  78.         'log'=>array(  
  79.             // 记录器的类  
  80.             'class'=>'CLogRouter',  
  81.             // 在哪里存储日志  
  82.             'routes'=>array(  
  83.                 array(  
  84.                     // 保存到文件中,其他选项是可用的  
  85.                     'class'=>'CFileLogRoute',  
  86.                     // 什么内容保存到文件中? error 和 warning, info 和 trace 可以增加到这里  
  87.                     'levels'=>'error, warning',  
  88.                 ),  
  89.             ),  
  90.         ),  
  91.   
  92.         // 用户  
  93.         'user'=>array(  
  94.             // 启用 cookie-based 验证  
  95.             'allowAutoLogin'=>true,  
  96.             // 设置需要验证时用户被转到的 url  
  97.             // 使用 null 出现 43 HTTP 错误  
  98.             'loginUrl'=>null,  
  99.             // 设置一个类的名字,  
  100.             // 这个类扩展自 CWebUser 并且保存在  
  101.             // protected/components/<classname> 中。  
  102.             'class' => 'WebUser',  
  103.         ),  
  104.   
  105.         // 数据库  
  106.         'db'=>require(dirname(__FILE__).DIRECTORY_SEPARATOR.'db.php'),  
  107.   
  108.         // 缓存  
  109.         'cache'=>array(  
  110.             'class'=>'A cache class, like: system.caching.CApcCache',  
  111.         ),  
  112.         'session' => array( //  memcache session cache  
  113.             'class' =>'CCacheHttpSession',  
  114.             'autoStart' => 1,  
  115.             'sessionName' => 'frontend',  
  116.             'cookieParams' => array('lifetime'=>'3600','path'=>'/','domain'=>'.test.com','httponly'=>'1'),  
  117.             'cookieMode' => 'only',  
  118.         ),  
  119.         // url  
  120.         'urlManager'=>array(  
  121.             // URL 格式。必须是 'path' 或 'get'。  
  122.             // path: index.php/controller/action/attribute/value  
  123.             // get: index.php?r=controller/action&attribute=value  
  124.             'urlFormat'=>'path',  
  125.             // 显示为www.example.com/index.php/controller/action  
  126.             // 或www.example.com/controller/action  
  127.             'showScriptName' => true,  
  128.             // 转向指定的 url 到你想要的 controller 的规则  
  129.             // 查阅www.yiiframework.com/doc/guide/topics.url  
  130.             'rules'=>array(  
  131.                 //www.example.com/home代替www.example.com/site/index  
  132.                 'home'=>'site/index',  
  133.                 'post/<id:\d+>'=>'post/show',  
  134.             ),  
  135.         ),  
  136.         // 你可以使用 scriptMap 来配置脚本来自哪里。  
  137.         //If you use the split configurations for development and production you can  
  138.         // have different maps in each and then just load the file and it'll  
  139.         // load the appropriate file depending on the configuration your running.  
  140.         // 对于一个生产环境的配置,如下  
  141.         'clientScript'=>array(  
  142.             'scriptMap'=>array(  
  143.                 'register.js'=>'site.min.js',  
  144.                 'login.js'=>'site.min.js',  
  145.             ),  
  146.         ),  
  147.         // 对于一个开发环境,可以这样做  
  148.         'clientScript'=>array(  
  149.             'scriptMap'=>array(  
  150.                 'register.js'=>'register.js',  
  151.                 'login.js'=>'login.js',  
  152.             ),  
  153.         ),  
  154.     ),  
  155. );  

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.   
  6. // change the following paths if necessary  
  7. if (APP_ENV == 'production') {  
  8.     error_reporting(0);  
  9.     $yii=dirname(__FILE__).'/../yii-svn/framework/yiilite.php';  
  10.     defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',1);  
  11. else {  
  12.     ini_set('error_reporting',E_ALL);  
  13.     ini_set('display_errors',1);      
  14.     $yii=dirname(__FILE__).'/../yii-svn/framework/yii.php';  
  15.     // remove the following lines when in production mode  
  16.     defined('YII_DEBUG') or define('YII_DEBUG',true);  
  17.     // specify how many levels of call stack should be shown in each log message  
  18.     defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);  
  19. }  
  20. $config=dirname(__FILE__).'/protected/config/'.APP_ENV.'.php';  
  21.   
  22. require_once($yii);  
  23. 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. );  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics