一:建立Urlphp
Yii提供了一個助手方法yiihelpersUrl::to(),用來根據提供的路由和參數建立各類各樣的URL,Url::to()方法調用的是UrlManager::createUrl()方法來建立Urlhtml
1:建立一個普通的路由Urlweb
Url::to(['post/index']); //等同於 Yii::$app->urlManager->createUrl(['post/index']);
2:建立一個帶路由參數的路由Url正則表達式
Url::to(['post/index', 'id' => 1]); //等同於 Yii::$app->urlManager->createUrl(['post/index', 'id' => 1]);
3:建立一個帶錨點的Url數組
Url::to(['post/index', '#' => 'content']); //等同於 Yii::$app->urlManager->createUrl(['post/index', '#' => 'content']);
4:建立一個絕對路徑Url:http://local.yii.com/index.ph...app
Url::to(['post/index'], true); //等同於 Yii::$app->urlManager->createAbsoluteUrl(['post/index'], true);
5:建立一個帶https協議的絕對路徑URL:https://local.yii.com/index.p...yii
Url::to(['post/index'], 'https'); //等同於 Yii::$app->urlManager->createAbsoluteUrl(['post/index'], 'https');
6:在Yii2.0.2版本開始,咱們能夠使用別名來定義別名路由post
建立當前請求Urlurl
Url::to(['']) //等同於 Yii::$app->urlManager->createUrl(['']);
建立設置別名的路由code
Yii::setAlias('@post', 'post/index'); Url::to(['@post']); //等同於 Yii::$app->urlManager->createUrl(['@post']);
7:在Yii中除了Url::to()方法,yiihelpersUrl 助手類同時提供了多個其它建立 URL 的方法
//建立主頁URL Url::home(); //建立根Url,若是程序部署到一個Web目錄下的子目錄時很是有用 Url::base() //當前請求的權威規範URL Url::canonical() //記住當前請求的URL並在之後獲取 //記住當前Url Url::remember(); //獲取記住的Url Url::previous()
8:配置默認路由
在Yii中yiiwebApplication::$defaultRoute 屬性所指定的缺省路由就是網頁的首頁,默認值爲site/index,若是你想要修改默認路由地址,只須要修改配置中的defaultRoute 值
return [ // ... 'defaultRoute' => 'main/index', ];
9:全攔截路由
有時候你的系統須要偉華沒這時候你可能想要全部的請求都顯示相同的信息頁,Yii中設置 yiiwebApplication::$catchAll 屬性便可實現訪問任何請求都顯示相同的信息頁
return [ // ... 'catchAll' => ['site/offline'], ];
二:美化Url
1:在Yii中若是你須要美化Url,這時候你須要在配置中配置urlManager組件
[ 'components' => [ 'urlManager' => [ 'class' => \yii\web\UrlManager::className(), 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableStrictParsing' => false, 'suffix' => '', 'rules' => [ // ... ], ], ], ]
urlManager組件中經常使用到的個參數說明
class:urlManager組件使用到的class類
enablePrettyUrl:是否開啓美化路由,默認爲false,true爲開啓美化路由,false爲不開啓美化路由
showScriptName:是否顯示腳本名稱,默認爲true,true爲顯示腳本名稱,false爲不顯示腳本名稱,此屬性決定建立的URL中是否包含入口腳本名稱,例如默認的/index.php/post/index.若是設置showScriptName爲false,則建立成/post/index,這裏注意,若是設置爲false後訪問不成功,在web目錄下建立一個.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
enableStrictParsing:是否開啓嚴格解析,默認爲false,true爲開啓嚴格解析路由,false圍毆不開啓嚴格解析路由,若是設置爲true開啓嚴格解析是,請求的url必須嚴格匹配rules(規則)中設定的一條規則,不然系統將拋出 yiiwebNotFoundHttpException 異常,若是設置爲false關閉嚴格解析,當rules(規則)中沒有任何一條匹配時, 請求URL中的路徑信息將被做爲請求路由使用。
suffix:設置路由後綴,即路由結尾,默認爲空(沒有後綴),例如,若是此蠶食設置.html,這時候/post/index會設置爲/post/index.html
rules:規則,此屬性包含一個規則列表,用來規定如何解析和建立URL。 這是一個主要屬性,你應該根據特定的應用環境配置此屬性用來生成特定格式的URL,詳細說明下面介紹
2:rules(規則)
一個Url規則是類yiiwebUrlRule 或子類的一個實例
(1):咱們能夠配置rules中鍵爲匹配規則,值爲路由,例:
'rules' => [ 'test' => 'post/test', 'test/<id:\d+>' => 'post/test' ]
上面第一條規則匹配test映射到路由post/test,第二條規則匹配符合正則表達式 test/(d+) 的URL並映射到路由 post/test
根據上面,若是咱們想要配置站點下全部的路由爲/post/index/1這種形式
'rules' => [ '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', '<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<module>/<controller>/<action>', '<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>', ]
(2):除了定義Url規則以外,咱們還能夠將規則定義爲配置數組。 每一個配置數組用來配置一個單獨的 URL 規則對象
'rules' => [ [ 'class' => \yii\web\UrlRule::className(), 'pattern' => 'wj/<id:\d+>/<tag>', 'route' => 'post/index', 'suffix' => '', 'defaults' => ['id' => 1, 'tag' => ''], ], ]
url規則常見個參數說明
class:url規則使用class類,默認爲yiiwebUrlRule
pattern:路由規則
route:映射的路由
suffix:路由後綴,默認爲空
defaults:路由規則中的參數默認值