首頁  >  文章  >  php框架  >  yii框架怎麼配置pathinfo的模式

yii框架怎麼配置pathinfo的模式

angryTom
angryTom原創
2020-02-18 11:18:502108瀏覽

yii框架怎麼配置pathinfo的模式

yii框架怎麼配置pathinfo的模式   

第一次部署Yii框架搭建的應用程式後,框架預設使用的不是PathInfo形式的URL,而是類似http://yourdomain.com/index.php?r=account/login 這樣的形式,這種URL不僅不美觀,而且不利於SEO,所以下面介紹在Yii中如何使用PathInfo形式的URL(註:開發環境基於wampserver2.4)。

1)打開protected/config/main.php設定文件,將下面這段urlManager程式碼的註解去掉:

'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
        &#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;,
        &#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;,
        &#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;,
    ),
),

2)去掉以後,我們就可以使用類似http:// yourdomain.com/index.php/controller/action這種形式的URL去訪問應用,但是接下來我們還要隱藏掉中間的那個index.php;

相關文章教程推薦:yii教學

3)在應用程式的根目錄下新增一個名為.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

4)開啟apache的rewrite模組,在httpd .conf中找到#LoadModule rewrite_module modules/mod_rewrite.so,把前面的「#」去掉;

5)重啟apache;

6)繼續編輯main.php文件,在剛才那個urlManager的陣列中新增一個元素:

&#39;urlManager&#39; => array(
    &#39;urlFormat&#39; => &#39;path&#39;,
    &#39;showScriptName&#39; => false, // 添加这一行
    &#39;rules&#39; => array(
        &#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;,
        &#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;,
        &#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;,
    ),
),

7)完成!

更多yii程式設計入門技術,請持續關注PHP中文網! !    

#

以上是yii框架怎麼配置pathinfo的模式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn