Yii 配置文件

主配置文件是main.php:

 <?php

 // uncomment the following to define a path alias
 // Yii::setPathOfAlias(‘local‘,‘path/to/local-folder‘);

 // This is the main Web application configuration. Any writable
 // CWebApplication properties can be configured here.
 return array(
     ‘basePath‘=>dirname(__FILE__).DIRECTORY_SEPARATOR.‘..‘,
     ‘name‘=>‘My Web Application‘,

     // preloading ‘log‘ component
     ‘preload‘=>array(‘log‘),

     // autoloading model and component classes
     ‘import‘=>array(
         ‘application.models.*‘,
         ‘application.components.*‘,
     ),

     //default controller
     ‘defaultController‘=>‘test‘,

     //controller map
     /* ‘controllerMap‘=>array(
         ‘index‘=>‘application.controllers.TestController‘,
     ), */    

     ‘modules‘=>array(
         // uncomment the following to enable the Gii tool

         ‘gii‘=>array(
             ‘class‘=>‘system.gii.GiiModule‘,
             ‘password‘=>‘mr.coke‘,
              // If removed, Gii defaults to localhost only. Edit carefully to taste.
             ‘ipFilters‘=>array(‘127.0.0.1‘,‘::1‘),
         ),

     ),

     // application components
     ‘components‘=>array(
         ‘user‘=>array(
             // enable cookie-based authentication
             ‘allowAutoLogin‘=>true,
         ),
         // uncomment the following to enable URLs in path-format

         ‘urlManager‘=>array(
             ‘urlFormat‘=>‘path‘,
             ‘rules‘=>array(
                 ‘<controller:\w+>/<id:\d+>‘=>‘<controller>/view‘,
                 ‘<controller:\w+>/<action:\w+>/<id:\d+>‘=>‘<controller>/<action>‘,
                 ‘<controller:\w+>/<action:\w+>‘=>‘<controller>/<action>‘,
                 ‘index‘=>‘test/index‘,
                 ‘test‘=>‘test/test‘,
             ),
         ),

         /* ‘db‘=>array(
             ‘connectionString‘ => ‘sqlite:‘.dirname(__FILE__).‘/../data/testdrive.db‘,
         ), */
         // uncomment the following to use a MySQL database

         ‘db‘=>array(
             ‘connectionString‘ => ‘mysql:host=localhost;dbname=user.login.yii‘,
             ‘emulatePrepare‘ => true,
             ‘username‘ => ‘root‘,
             ‘password‘ => ‘root‘,
             ‘charset‘ => ‘utf8‘,
             ‘tablePrefix‘ =>‘tbl_‘,
             ‘enableProfiling‘=>YII_DEBUG,
             ‘enableParamLogging‘=>YII_DEBUG,
         ),

         ‘errorHandler‘=>array(
             // use ‘site/error‘ action to display errors
             ‘errorAction‘=>‘site/error‘,
         ),
         ‘log‘=>array(
             ‘class‘=>‘CLogRouter‘,
             ‘routes‘=>array(
                 array(
                     ‘class‘=>‘CFileLogRoute‘,
                     ‘levels‘=>‘error, warning‘,
                 ),
                 // uncomment the following to show log messages on web pages

                 array(
                     ‘class‘=>‘CWebLogRoute‘,
                     ‘levels‘=>‘error, warning,trace‘,
                     ‘categories‘=>‘system.db.*‘,
                 ),

             ),
         ),
     ),

     // application-level parameters that can be accessed
     // using Yii::app()->params[‘paramName‘]
     ‘params‘=>array(
         // this is used in contact page
         ‘adminEmail‘=>‘[email protected]‘,
     ),
 );

关于urlmanager:

User-friendly URLs 

When path is used as the URL format, we can specify some URL rules to make our URLs even more user-friendly. For example, we can generate a URL as short as /post/100, instead of the lengthy/index.php/post/read/id/100. URL rules are used by CUrlManager for both URL creation and parsing purposes.

    ......
    ‘components‘=>array(
        ......
        ‘urlManager‘=>array(
            ‘urlFormat‘=>‘path‘,
            ‘rules‘=>array(
                ‘pattern1‘=>‘route1‘,
                ‘pattern2‘=>‘route2‘,
                ‘pattern3‘=>‘route3‘,
            ),
        ),
    ),
);

The rules are specified as an array of pattern-route pairs, each corresponding to a single rule. The pattern of a rule is a string used to match the path info part of URLs. And the route of a rule should refer to a valid controller route.

Besides the above pattern-route format, a rule may also be specified with customized options, like the following:

‘pattern1‘=>array(‘route1‘, ‘urlSuffix‘=>‘.xml‘, ‘caseSensitive‘=>false)

Starting from version 1.1.7, the following format may also be used (that is, the pattern is specified as an array element), which allows specifying several rules with the same pattern:

array(‘route1‘, ‘pattern‘=>‘pattern1‘, ‘urlSuffix‘=>‘.xml‘, ‘caseSensitive‘=>false)

3. Using Named Parameters 使用命名的参数

A rule can be associated with a few GET parameters. These GET parameters appear in the rule‘s pattern as special tokens in the following format:

<ParamName:ParamPattern>

where ParamName specifies the name of a GET parameter, and the optional ParamPattern specifies the regular expression that should be used to match the value of the GET parameter. In case when ParamPatternis omitted, it means the parameter should match any characters except the slash /. When creating a URL, these parameter tokens will be replaced with the corresponding parameter values; when parsing a URL, the corresponding GET parameters will be populated with the parsed results.

Let‘s use some examples to explain how URL rules work. We assume that our rule set consists of three rules:

array(
    ‘posts‘=>‘post/list‘,
    ‘post/<id:\d+>‘=>‘post/read‘,
    ‘post/<year:\d{4}>/<title>‘=>‘post/read‘,
)
  • Calling $this->createUrl(‘post/list‘) generates /index.php/posts. The first rule is applied.
  • Calling $this->createUrl(‘post/read‘,array(‘id‘=>100)) generates /index.php/post/100. The second rule is applied.
  • Calling $this->createUrl(‘post/read‘,array(‘year‘=>2008,‘title‘=>‘a sample post‘))generates /index.php/post/2008/a%20sample%20post. The third rule is applied.
  • Calling $this->createUrl(‘post/read‘) generates /index.php/post/read. None of the rules is applied.
更多:http://www.yiiframework.com/doc/guide/1.1/en/topics.urlhttp://www.cnblogs.com/mrcoke/articles/2407759.html
时间: 2024-10-12 21:31:14

Yii 配置文件的相关文章

yii第三方插件snoopy配置

首先.把snoopy类放到protected\extensions\snoopy\目录下. 其次.在yii配置文件main.php里配置import扩展进来. 'import'=>array( 'application.extensions.*', ), 然后在一个controller类文件的開始,增加下面行:require_once('snoopy/Snoopy.class.php'); 最后直接实例化对象就ok了 $snoopy = new Snoopy; $snoopy->fetch($

yii phpexcel &lt;转&gt;

原文详情参见 这里 1.下载phpexcel,将压缩包中的classes复制到protected/extensions下并修改为PHPExcel. 2.修改YII配置文件config/main.php [php] view plaincopy 'import'=>array( 'application.extensions.PHPExcel.PHPExcel', ), (以下处理PHPExcel autoload和YII autoload相冲突的方法任选其一,推荐第4种,最符合YII标准)3.1

YII使用PHPExcel导入Excel文件的方法

1.下载phpexcel,将压缩包中的classes复制到protected/extensions下并修改为PHPExcel. 2.修改YII配置文件config/main.php [php] view plaincopy 'import'=>array( 'application.extensions.PHPExcel.PHPExcel', ), (以下处理PHPExcel autoload和YII autoload相冲突的方法任选其一,推荐第4种,最符合YII标准) 3.1.修改PHPExc

php yii 学习笔记

yii 归档安装 1,下载 yii  Yii2的高级应用程序模板 2,解压模板到目录,进入控制台进入目录 运行 php init 安装YII 3,进入 http://localhost/phpmyadmin/ 创建数据库 排序规则使用  utf8_unicode_ci 4,打开yii配置文件 yii\commo\main-local.php 设置数据库配置信息 5,进入控制台运行 php yii migrate 命令 (会在数据库中建立两个表 ,一个是migration用来数据迁移的表,一个是u

命令行方式运行yii2程序

测试环境,yii 2.0.3版本 web访问方式,控制器放在controllers目录下 ,浏览器访问如下地址 http://127.0.0.1/index.php?r=[controller-name]/[method-name] console访问方式,控制器放在commands目录下 ./yii [command-name]/[method-name] 示例: <?php namespace app\commands; use Yii; use yii\console\Controller

yii2微博第三方登录

微博登录是最常用的第三方账号登录之一.由于其网站用户量大,可操作接口功能多,所以受到很多开发者的青睐. 既然是第三方,如果想使用它们的账号进行登录,那么第一步就应该申请一个开发账号. 前面啰嗦两句,这里有两个条件是硬性的,否则将影响你的开发. 微博账号,这个应该都有. 域名和服务器,也就是说你要有你自己的网站.不过为公司开发就方便多了. 申请开发账号 首先去微博开放平台:http://open.weibo.com/connect,点击立即接入,填写一个表单,验证一下网站就OK了 之后你在我的应用

yii 1.0配置文件解析

yii中配置文件主要是一个入口文件,然后 main.php <?php // 取消下行的注释,来定义一个路径别名 // Yii::setPathOfAlias('local','path/to/local-folder'); // 这是 Web 应用配置的主体部分.任何可写的 // CWebApplication 属性可以在这里配置. $config = array( // protected 目录的基础路径 // 使用 Yii::app()->basePath 来访问 'basePath'

yii的常用配置文件

<?php return array( 'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..', //当前应用根目录路径 'name' => '大侠商城', //应用名称 'preload' => array('log'), //预加载 'import' => array( 'application.models.*', //加载application/models所有模型类 'application.componen

yii2_配置文件详解1_定义yii\web\Response组件并且监听

配置就是定义一个个类的属性嘛,第一层是定义app的属性,components里就是定义每一个组件的属性,要监听事件,我们不是定义属性了,写法是这样的: return [ Application的配置 'components' => [ 其它组件的配置 'response' => [ //重点在这里,先写on加空格,然后就写事件的标记值 'on beforeSend' => function($event){ if(is_string($event->sender->data)