关于Yii路由大小写敏感的解决方案

示例:默认情况下,以下路由

URI-1: http://www.w3c.com/StoreHouse/show

URI-2: http://www.w3c.com/storehouse/show

在windows开发环境下没有区别,而源码部署到linux/unix服务器环境下之时,倘若该控制器的路径是root/project/StoreHouseController.php中的actionShow(),则URI-1可正常访问,URI-2访问出现http-404异常。

究其原因:

在yii框架的framework/web/CUrlManager.php中定义了开放属性

1     /**
2      * @var boolean whether routes are case-sensitive. Defaults to true. By setting this to false,
3      * the route in the incoming request will be turned to lower case first before further processing.
4      * As a result, you should follow the convention that you use lower case when specifying
5      * controller mapping ({@link CWebApplication::controllerMap}) and action mapping
6      * ({@link CController::actions}). Also, the directory names for organizing controllers should
7      * be in lower case.
8      */
9     public $caseSensitive=true;

默认true值表示路由地址大小写敏感,false值表示路由不区分大小写。

据Yii官方参考描述:

Note: By default, routes are case-sensitive. It is possible to make routes case-insensitive by settingCUrlManager::caseSensitive to false in the application configuration. When in case-insensitive mode, make sure you follow the convention that directories containing controller class files are in lowercase, and both controller map and action map have lowercase keys.

当在大小写不敏感模式中时,要确保你遵循了相应的规则约定,即:包含控制器类文件的目录名小写,且控制器映射和动作映射 中使用的键为小写。

所以,除了配置main.php中的组件配置项urlManage属性caseSensitive=true以外,还应该确保以下几点:

  1. 控制器所在目录名称为小写;
  2. 控制器文件名和类名仅限于形如“StorehouseController.php”,杜绝“StoreHouseController.php” ;
  3. 如若控制器文件上层目录结构中有modules,则所属的模块目录名称也需要为小写,模块入口文件XyzModule.php遵循第2条约定。

出现以上的约束情况的原因如下:

在yii框架中framework/web/CWebApplication.php之方法createController()创建控制器方法中,

1 if(!$caseSensitive)
2     $id=strtolower($id); //若然等于false,则控制器ID转换成小写形式

首先会读取配置信息,其中包含检查了caseSensitive属性,若然等于false,则控制器ID即转换成全小写形式。

继而,每当创建控制器方法被调用的同时,控制器的首字母皆转换成大写形式。

1 //每当创建控制器实例,即时将控制器ID首字母转为大写形式
2 $className=ucfirst($id).'Controller';

如此,便造成了以上异常现象。

转载于:https://www.cnblogs.com/neilsenchan/archive/2012/09/21/2697292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值