由於我完成了我的項目,只是無法理解url美化。假設這是我的網址:我試圖美化Yii中的URL
localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=1
其中產品是控制器,索引是該控制器的操作。 catId是我通過url傳遞的參數。我需要美化的網址
localhost/wowwaylabs/trunk/mpi_v1/this-is-india-1
其中1是我通過catId。
由於我完成了我的項目,只是無法理解url美化。假設這是我的網址:我試圖美化Yii中的URL
localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=1
其中產品是控制器,索引是該控制器的操作。 catId是我通過url傳遞的參數。我需要美化的網址
localhost/wowwaylabs/trunk/mpi_v1/this-is-india-1
其中1是我通過catId。
通過htaccess的:)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$/? index.php?r=$1 [PT,L]
</IfModule>
localhost/wowwaylabs/trunk/mpi_v1/products/index/&catId=1
有沒有辦法使用url管理器來做到這一點。否則你能否就此給我解釋一下? – user1532043
對不起,我沒有Yii的經驗。但'htaccess'文件是處理通過apache進入的請求的文件。前兩行開啓URL重寫。接下來的兩種說法是隻運行重寫,如果文件實際上不存在,第三種是正則表達式匹配'index.php?r =' – andy
爲了使URL漂亮,你需要添加下面的.htaccess文件的代碼行應該在你的項目的根文件夾:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
它會保持你的網址沒有?r。
現在取消以下,使路徑格式的URL(在保護/配置/ main.php)
/*
'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>',
),
),
*/
然後在同一個文件中的「urlManager」添加'showScriptName'=>false,
。它會從url中刪除index.php。
詳細檢查這些鏈接:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
http://www.sniptrichint.com/tip-of-the-day/beautiful-url-in-yii-without-index/
我認爲這將解決您的問題。
如果您已經使用Yii的URL Manager(如果不遵循Workonphp的指示將其打開)嘗試創建一個規則,在規則的頂部,看起來像這樣添加:
'<category_id:\w+>' => 'products/index',
是什麼這將做,如果只有一個參數(即類別名稱和ID)在URL中傳遞,它是一個字符串/字(:\ W +指定此)不是一個數字(:\ D +)它將默認爲產品控制器和索引行動。然後它會將$category_id
作爲變量傳遞給控制器。然後,將需要修改該操作以拉動id列的字符串,象這樣:
public function actionIndex($category_id) {
$pieces = explode('-',$category_id);
$cat_id = end($pieces); //actual category id seperated from name
//...rest of code for this function
}
假設this-is-india
是可變或任意長度(即可以變化的長度或語法wildy,如Pitchinnate建議在評論類別名稱),那麼你可以這樣做純粹使用URL經理無需編輯您的htaccess像這樣:
'urlManager'=>array(
...
'rules'=>array(
'<catName:[0-9a-zA-Z_\-]+>-<catId:\d+>'=>'products/index',
...
),
...
),
這將需要的任意組合擁有一批在結束字符,並使用數量在年底爲catId
,例如:
localhost/wowwaylabs/trunk/mpi_v1/this-is-india-1
將解析爲
localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=1&catName=this-is-india
類似;
localhost/wowwaylabs/trunk/mpi_v1/this-is-another-title-or-category-or-whatever-999
將解析:
localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=999&catName=this-is-another-title-or-category-or-whatever
是什麼'這個-是-india'是該類別的名稱? – Pitchinnate