關于Yii路由大小寫敏感的解決方案
2012-09-21 17:54
Neilsen-Chan
閱讀(1175)
評論(0)
編輯
收藏
舉報
示例:預設情況下,以下路由URI-1: http://www.w3c.com/StoreHouse/showURI-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 * @...
示例:預設情況下,以下路由
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以外,還應該確定以下幾點:
- 控制器所在目錄名稱為小寫;
- 控制器檔案名和類名僅限于形如“StorehouseController.php”,杜絕“StoreHouseController.php” ;
- 如若控制器檔案上層目錄結構中有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\';
如此,便造成了以上異常現象。
-
标簽 PHP
, Yii
