程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> YII使用url組件美化管理的方法

YII使用url組件美化管理的方法

編輯:PHP綜合

本文實例講述了YII使用url組件美化管理的方法。分享給大家供大家參考,具體如下:

urlManager組件

yii的官方文檔對此的解釋如下:

urlSuffix  此規則使用的url後綴,默認使用CurlManger::urlSuffix,值為null。例如可以將此設置為.html,讓url看起來“像”是一個靜態頁面。
caseSensitive  是否大小寫敏感,默認使用CUrlManager::caseSensitive,值為null。
defaultParams  該規則使用的默認get參數。當使用該規則來解析一個請求時,這個參數的值會被注入到$_GET參數中。
matchValue  當創建一個URL時,GET參數是否匹配相應的子模式。默認使用CurlManager::matchValue,值為null。

如果該屬性為 false,那麼意味著當路由和參數名匹配給定的規則時,將以此來創建一個URL。

如果該屬性為true,那麼給定的參數值夜必須匹配相應的參數子模式。

注意:將此屬性設置為true會降低性能。

我們使用一些例子來解釋網址工作規則。我們假設我們的規則包括如下三個:

array(
  'posts'=>'post/list',
  'post/<id:\d+>'=>'post/read',
  'post/<year:\d{4}>/<title>'=>'post/read',
)

調用$this->createUrl('post/list')生成/index.php/posts。第一個規則適用。

調用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二個規則適用。

調用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三個規則適用。

調用$this->createUrl('post/read')產生/index.php/post/read。請注意,沒有規則適用。

總之,當使用createUrl生成網址,路線和傳遞給該方法的GET參數被用來決定哪些網址規則適用。如果關聯規則中的每個參數可以在GET參數找到的,將被傳遞給createUrl ,如果路線的規則也匹配路線參數,規則將用來生成網址。

如果GET參數傳遞到createUrl是以上所要求的一項規則,其他參數將出現在查詢字符串。例如,如果我們調用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我們將獲得/index.php/post/100?year=2008。為了使這些額外參數出現在路徑信息的一部分,我們應該給規則附加/* 。 因此,該規則post/<id:\d+>/* ,我們可以獲取網址/index.php/post/100/year/2008 。

正如我們提到的,URL規則的其他用途是解析請求網址。當然,這是URL生成的一個逆過程。例如, 當用戶請求/index.php/post/100 ,上面例子的第二個規則將適用來解析路線post/read和GET參數array('id'=>100) (可通過$_GET獲得) 。

提示:此網址通過createurl方法所產生的是一個相對地址。為了得到一個絕對的url ,我們可以用前綴yii: :app()->hostInfo ,或調用createAbsoluteUrl 。

注:使用的URL規則將降低應用的性能。這是因為當解析請求的URL ,[ CUrlManager ]嘗試使用每個規則來匹配它,直到某個規則可以適用。因此,高流量網站應用應盡量減少其使用的URL規則。

test.com/vthot 想生成 test.com/vthot/
復制代碼 代碼如下:'urlSuffix'=>'/',
要更改URL格式,我們應該配置urlManager應用元件,以便createUrl可以自動切換到新格式和應用程序可以正確理解新的網址:

'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代碼
復制代碼 代碼如下:'posts'=>'post/list',
Action代碼
復制代碼 代碼如下:echo $this->createAbsoluteUrl('post/list');

輸出

http://localhost/test/index.php/post

示例二

Rule代碼
復制代碼 代碼如下:'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
Action代碼
復制代碼 代碼如下:echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));

輸出

http://localhost/test/index.php/post/998.html?name=123

示例三

Rule代碼:
復制代碼 代碼如下:'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),

Action代碼
復制代碼 代碼如下:echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
輸出

http://localhost/test/index.php/post/998/tody.xml

示例四

Rule代碼
復制代碼 代碼如下:'http://<user:\w+>.vt.com/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.me'),

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') ; 經過處理的$_GET['id']
}
//$id = Yii::app()->request->getPost('id'); 經過處理的$_POST['id']
//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多

2)public function actionUpdate($id)  這種不支持多主鍵,會檢查一下到底GET裡面有沒有id,沒有id就直接不允許訪問

'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的參數
'post/<alias:[-a-z]+>' => 'post/view',  domain/post/e文小寫 其中:前面的alias是PostController actionView($alias)的參數
'(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

還有一點,我們可以做進一步清理我們的網址,即在URL中藏匿index.php  入口腳本。這就要求我們配置Web服務器,以及urlManager應用程序元件。

1.add showScriptName=>false

2.add project/.htaccess

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

3.開啟rewrite

簡單的說,在main.php中簡單設置urlManager,然後講了3條規則,基本都覆蓋到了。最後是隱藏index.php,請記住.htaccess位於index.php同級目錄 ,而不是protected/目錄。其他就簡單了。

希望本文所述對大家基於Yii框架的PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved