一:創建Url
Yii提供了一個助手方法yii\helpers\Url::to(),用來根據提供的路由和參數創建各種各樣的URL,Url::to()方法調用的是UrlManager::createUrl()方法來創建Url
1:創建一個普通的路由Url
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.php?r=post%2Findex
Url::to(['post/index'], true);
//等同於
Yii::$app->urlManager->createAbsoluteUrl(['post/index'], true);
5:創建一個帶https協議的絕對路徑URL:https://local.yii.com/index.php?r=post%2Findex
Url::to(['post/index'], 'https');
//等同於
Yii::$app->urlManager->createAbsoluteUrl(['post/index'], 'https');
6:在Yii2.0.2版本開始,我們可以使用別名來定義別名路由
創建當前請求Url
Url::to([''])
//等同於
Yii::$app->urlManager->createUrl(['']);
創建設置別名的路由
Yii::setAlias('@post', 'post/index');
Url::to(['@post']);
//等同於
Yii::$app->urlManager->createUrl(['@post']);
7:在Yii中除了Url::to()方法,yii\helpers\Url 助手類同時提供了多個其它創建 URL 的方法
//創建主頁URL
Url::home();
//創建根Url,如果程序部署到一個Web目錄下的子目錄時非常有用
Url::base()
//當前請求的權威規範URL
Url::canonical()
//記住當前請求的URL並在以後獲取
//記住當前Url
Url::remember();
//獲取記住的Url
Url::previous()
8:配置默認路由
在Yii中yii\web\Application::$defaultRoute 屬性所指定的缺省路由就是網頁的首頁,默認值爲site/index,如果你想要修改默認路由地址,只需要修改配置中的defaultRoute 值
return [
// ...
'defaultRoute' => 'main/index',
];
9:全攔截路由
有時候你的系統需要偉華沒這時候你可能想要所有的請求都顯示相同的信息頁,Yii中設置 yii\web\Application::$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(規則)中設定的一條規則,否則系統將拋出 yii\web\NotFoundHttpException 異常,如果設置爲false關閉嚴格解析,當rules(規則)中沒有任何一條匹配時, 請求URL中的路徑信息將被作爲請求路由使用。
suffix:設置路由後綴,即路由結尾,默認爲空(沒有後綴),例如,如果此蠶食設置.html,這時候/post/index會設置爲/post/index.html
rules:規則,此屬性包含一個規則列表,用來規定如何解析和創建URL。 這是一個主要屬性,你應該根據特定的應用環境配置此屬性用來生成特定格式的URL,詳細說明下面介紹
2:rules(規則)
一個Url規則是類yii\web\UrlRule 或子類的一個實例
(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類,默認爲yii\web\UrlRule
pattern:路由規則
route:映射的路由
suffix:路由後綴,默認爲空
defaults:路由規則中的參數默認值