《PHP編程:YII使用url組件美化管理的方法》要點:
本文介紹了PHP編程:YII使用url組件美化管理的方法,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
本文實例講述了YII使用url組件美化管理的辦法.分享給大家供大家參考,具體如下:PHP實戰
urlManager組件PHP實戰
yii的官方文檔對此的解釋如下:PHP實戰
urlSuffix? 此規則使用的url后綴,默認使用CurlManger::urlSuffix,值為null.例如可以將此設置為.html,讓url看起來“像”是一個靜態頁面.
caseSensitive? 是否大小寫敏感,默認使用CUrlManager::caseSensitive,值為null.
defaultParams? 該規則使用的默認get參數.當使用該規則來解析一個哀求時,這個參數的值會被注入到$_GET參數中.
matchValue? 當創建一個URL時,GET參數是否匹配相應的子模式.默認使用CurlManager::matchValue,值為null.PHP實戰
如果該屬性為 false,那么意味著當路由和參數名匹配給定的規則時,將以此來創建一個URL.PHP實戰
如果該屬性為true,那么給定的參數值夜必須匹配相應的參數子模式.PHP實戰
注意:將此屬性設置為true會降低性能.PHP實戰
我們使用一些例子來解釋網址工作規則.我們假設我們的規則包括如下三個:PHP實戰
array( 'posts'=>'post/list', 'post/<id:\d+>'=>'post/read', 'post/<year:\d{4}>/<title>'=>'post/read', )
調用$this->createUrl('post/list')生成/index.php/posts.第一個規則適用.PHP實戰
調用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100.第二個規則適用.PHP實戰
調用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post.第三個規則適用.PHP實戰
調用$this->createUrl('post/read')產生/index.php/post/read.請注意,沒有規則適用.PHP實戰
總之,當使用createUrl生成網址,路線和傳遞給該辦法的GET參數被用來決定哪些網址規則適用.如果關聯規則中的每個參數可以在GET參數找到的,將被傳遞給createUrl ,如果路線的規則也匹配路線參數,規則將用來生成網址.PHP實戰
如果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 .PHP實戰
正如我們提到的,URL規則的其他用途是解析哀求網址.當然,這是URL生成的一個逆過程.例如, 當用戶哀求/index.php/post/100 ,上面例子的第二個規則將適用來解析路線post/read和GET參數array('id'=>100) (可通過$_GET獲得) .PHP實戰
提示:此網址通過createurl辦法所產生的是一個相對地址.為了得到一個絕對的url ,我們可以用前綴yii: :app()->hostInfo ,或調用createAbsoluteUrl .PHP實戰
注:使用的URL規則將降低應用的性能.這是因為當解析哀求的URL ,[ CUrlManager ]嘗試使用每個規則來匹配它,直到某個規則可以適用.因此,高流量網站應用應盡量減少其使用的URL規則.PHP實戰
test.com/vthot 想生成 test.com/vthot/
PHP實戰
'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'), ), ),
示例一PHP實戰
Rule代碼
PHP實戰
輸出PHP實戰
http://localhost/test/index.php/postPHP實戰
示例二PHP實戰
Rule代碼
PHP實戰
輸出PHP實戰
http://localhost/test/index.php/post/998.html?name=123PHP實戰
示例三PHP實戰
Rule代碼:
PHP實戰
Action代碼
PHP實戰
http://localhost/test/index.php/post/998/tody.xmlPHP實戰
示例四PHP實戰
Rule代碼
PHP實戰
Action代碼:PHP實戰
echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01')); echo ''; echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));
輸出PHP實戰
http://boy.vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01PHP實戰
1)controller/Update/id/23PHP實戰
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就直接不允許拜訪PHP實戰
'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.PHP實戰
暗藏 index.phpPHP實戰
還有一點,我們可以做進一步清理我們的網址,即在URL中藏匿index.php? 入口腳本.這就要求我們配置Web服務器,以及urlManager應用程序元件.PHP實戰
1.add showScriptName=>falsePHP實戰
2.add project/.htaccessPHP實戰
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.開啟rewritePHP實戰
簡單的說,在main.php中簡單設置urlManager,然后講了3條規則,基本都覆蓋到了.最后是暗藏index.php,請記住.htaccess位于index.php同級目錄 ,而不是protected/目錄.其他就簡單了.PHP實戰
希望本文所述對大家基于Yii框架的PHP程序設計有所贊助.PHP實戰
維易PHP培訓學院每天發布《PHP編程:YII使用url組件美化管理的方法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7974.html