[PHP]yii framework 的 簡潔網址以及隱藏 index.php

  • 2935
  • 0
  • 2015-04-14

摘要:[PHP]yii framework 的 網址隱藏及 index.php 隱藏

嗯....

嗯...........

嗯...................

又是 [PHP]...........

而且又是 yii framework...... (#゚Д゚)

照舊 以上不是重點...


 

上一篇介紹了 yii framework 的啟用及建立專案,相信連入該專案網站的話,都會發現網址會變成像這樣.....
http://localhost/[專案名稱]/index.php?r=site/page&view=about

那麼能不能將網址簡化的像是 .NET MVC 那樣呢?????
如:http://localhost/[專案名稱]/site/page/view/about

其實是可以的哦!!! 
(疑似遠方傳來不明生物想要抱怨以及槌人的謎之聲:不然這篇留著紀念做什麼用的......(╯-_-)╯╧╧

所以上網GOOGLE了一下,整理出重點來留存,步驟如下:

步驟一:
專案建立完成後,開啟 /protected/config/main.php
找到程式碼中的被註解掉的 Array

 

// 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>',                   
    ),                                                                                                               
),                                                                                                                    
*/                                                                                                                   


把整個 Array 的註解取消後將 'showScriptName'=>false, 加入陣列之中,但是注意 false 不要上了單引號或雙引號。

 

最後會變成



// uncomment the following to enable URLs in path-format
// 簡潔網址
'urlManager'=>array(
    'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
),

步驟一結束啦!!


步驟二:
接著下來會發現,網址可以簡潔了,但是卻變得無法瀏覽 




所以我們需要在專案底下的第一層目錄中加入 “.htaccess” 檔案,
至於是哪個第一層目錄嘛...當然就是那個想要被我們隱藏起來的 index.php 同層級的目錄啦!!

.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


有興趣的大大們可以各別前往 GOOGLE 查看內容標籤的各項作用。

存檔後扔入剛剛所描述的那個神祕的目錄後,就可以發現我們的網址變得更親切囉  ლ(`∀´ლ)
成果就是~~~ http://localhost/[專案名稱]/site/page/view/about

打到這邊..其實真的有弱掉的FU...
因此,各位大大今天就先到這邊告別了
以上步驟同樣的以 apache 伺服器為主唷!!

 

後紀..
菜鳥上路,隨手編寫...如有錯誤也請指正

 

 

20150414 update 

Yii 2.0 改用


'urlManager' => [ 
	'enablePrettyUrl' => TRUE, 
	'rules' => [ 
		'//' => '/', 
		'/' => '/read', 
		's' => '/list' 
	], 
	'showScriptName' => FALSE,
	'suffix' => '.html' 
],