本文實(shí)例講述了YII使用url組件美化管理的方法。分享給大家供大家參考,具體如下:
urlManager組件
yii的官方文檔對(duì)此的解釋如下:
urlSuffix 此規(guī)則使用的url后綴,默認(rèn)使用CurlManger::urlSuffix,值為null。例如可以將此設(shè)置為.html,讓url看起來“像”是一個(gè)靜態(tài)頁面。
caseSensitive 是否大小寫敏感,默認(rèn)使用CUrlManager::caseSensitive,值為null。
defaultParams 該規(guī)則使用的默認(rèn)get參數(shù)。當(dāng)使用該規(guī)則來解析一個(gè)請(qǐng)求時(shí),這個(gè)參數(shù)的值會(huì)被注入到$_GET參數(shù)中。
matchValue 當(dāng)創(chuàng)建一個(gè)URL時(shí),GET參數(shù)是否匹配相應(yīng)的子模式。默認(rèn)使用CurlManager::matchValue,值為null。
如果該屬性為 false,那么意味著當(dāng)路由和參數(shù)名匹配給定的規(guī)則時(shí),將以此來創(chuàng)建一個(gè)URL。
如果該屬性為true,那么給定的參數(shù)值夜必須匹配相應(yīng)的參數(shù)子模式。
注意:將此屬性設(shè)置為true會(huì)降低性能。
我們使用一些例子來解釋網(wǎng)址工作規(guī)則。我們假設(shè)我們的規(guī)則包括如下三個(gè):
array( 'posts'=>'post/list', 'post/<id:/d+>'=>'post/read', 'post/<year:/d{4}>/<title>'=>'post/read',)
調(diào)用$this->createUrl('post/list')生成/index.php/posts。第一個(gè)規(guī)則適用。
調(diào)用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二個(gè)規(guī)則適用。
調(diào)用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三個(gè)規(guī)則適用。
調(diào)用$this->createUrl('post/read')產(chǎn)生/index.php/post/read。請(qǐng)注意,沒有規(guī)則適用。
總之,當(dāng)使用createUrl生成網(wǎng)址,路線和傳遞給該方法的GET參數(shù)被用來決定哪些網(wǎng)址規(guī)則適用。如果關(guān)聯(lián)規(guī)則中的每個(gè)參數(shù)可以在GET參數(shù)找到的,將被傳遞給createUrl ,如果路線的規(guī)則也匹配路線參數(shù),規(guī)則將用來生成網(wǎng)址。
如果GET參數(shù)傳遞到createUrl是以上所要求的一項(xiàng)規(guī)則,其他參數(shù)將出現(xiàn)在查詢字符串。例如,如果我們調(diào)用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我們將獲得/index.php/post/100?year=2008。為了使這些額外參數(shù)出現(xiàn)在路徑信息的一部分,我們應(yīng)該給規(guī)則附加/* 。 因此,該規(guī)則post/<id:/d+>/* ,我們可以獲取網(wǎng)址/index.php/post/100/year/2008 。
正如我們提到的,URL規(guī)則的其他用途是解析請(qǐng)求網(wǎng)址。當(dāng)然,這是URL生成的一個(gè)逆過程。例如, 當(dāng)用戶請(qǐng)求/index.php/post/100 ,上面例子的第二個(gè)規(guī)則將適用來解析路線post/read和GET參數(shù)array('id'=>100) (可通過$_GET獲得) 。
提示:此網(wǎng)址通過createurl方法所產(chǎn)生的是一個(gè)相對(duì)地址。為了得到一個(gè)絕對(duì)的url ,我們可以用前綴yii: :app()->hostInfo ,或調(diào)用createAbsoluteUrl 。
注:使用的URL規(guī)則將降低應(yīng)用的性能。這是因?yàn)楫?dāng)解析請(qǐng)求的URL ,[ CUrlManager ]嘗試使用每個(gè)規(guī)則來匹配它,直到某個(gè)規(guī)則可以適用。因此,高流量網(wǎng)站應(yīng)用應(yīng)盡量減少其使用的URL規(guī)則。
test.com/vthot 想生成 test.com/vthot/
要更改URL格式,我們應(yīng)該配置urlManager應(yīng)用元件,以便createUrl可以自動(dòng)切換到新格式和應(yīng)用程序可以正確理解新的網(wǎng)址:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'urlSuffix'=>'.html', 'rules'=>array( 'posts'=>'post/list', 'post/<id:/d+>'=>array('post/show','urlSuffix'=>'.html'), 'post/<id:/d+>/<mid:/w+>'=>array('post/view','urlSuffix'=>'.xml'), ),),
示例一
Rule代碼
Action代碼
輸出
http://localhost/test/index.php/post
示例二
Rule代碼
Action代碼
輸出
http://localhost/test/index.php/post/998.html?name=123
示例三
Rule代碼:
Action代碼
輸出
http://localhost/test/index.php/post/998/tody.xml
示例四
Rule代碼
Action代碼:
echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01'));echo '';echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));
輸出
http://boy.vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01
1)controller/Update/id/23
public function actionUpdate(){ $id = Yii::app()->request->getQuery('id') ; 經(jīng)過處理的$_GET['id']}//$id = Yii::app()->request->getPost('id'); 經(jīng)過處理的$_POST['id']//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多
2)public function actionUpdate($id) 這種不支持多主鍵,會(huì)檢查一下到底GET里面有沒有id,沒有id就直接不允許訪問
'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的參數(shù)'post/<alias:[-a-z]+>' => 'post/view', domain/post/e文小寫 其中:前面的alias是PostController actionView($alias)的參數(shù)'(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC'(posts|archive)' => 'post/index', domain/posts或domain/archive'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),
When the URL is /tos, pass terms_of_service as the alias parameter value.
隱藏 index.php
還有一點(diǎn),我們可以做進(jìn)一步清理我們的網(wǎng)址,即在URL中藏匿index.php 入口腳本。這就要求我們配置Web服務(wù)器,以及urlManager應(yīng)用程序元件。
1.add showScriptName=>false
2.add project/.htaccess
RewriteEngine on# if a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# otherwise forward it to index.phpRewriteRule . index.php
3.開啟rewrite
簡單的說,在main.php中簡單設(shè)置urlManager,然后講了3條規(guī)則,基本都覆蓋到了。最后是隱藏index.php,請(qǐng)記住.htaccess位于index.php同級(jí)目錄 ,而不是protected/目錄。其他就簡單了。
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。