Yii-rules-of-url
提供:Dev Guides
Yii-URLのルール
*yii \ web \ UrlRule* の場合、URLルールはインスタンスです。 *urlManager* コンポーネントは、プリティURL形式が有効な場合、 *rules* プロパティで宣言されたURLルールを使用します。
要求を解析するために、URLマネージャーは宣言された順序でルールを取得し、最初のルールを探します。
ステップ1 *- *config/web.php ファイルの urlManager コンポーネントを変更します。
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'about' => 'site/about',
]
],
ステップ2 *-Webブラウザの http://localhost:8080/aboutにアクセスします。*ページが表示されます。
URLルールは、このパターンでクエリパラメータに関連付けることができます-
<ParamName:RegExp>、ここで-
- ParamName -パラメーター名
- RegExp -パラメーター値の一致に使用されるオプションの正規表現
、次のURLルールを宣言したとします-
[
'articles/<year:\d{4}>/<category>' => 'article/index',
'articles' => 'article/index',
'article/<id:\d+>' => 'article/view',
]
ルールが*解析*に使用される場合-
- /index.php/articlesはarticle/indexに解析されます */index.php/articles/2014/phpはarticle/indexに解析されます */index.php/article/100はarticle/viewに解析されます */index.php/articles/phpは、articles/phpに解析されます
ルールが* URLの作成*に使用される場合-
- Url :: to(['article/index'])は/index.php/articlesを作成します
- Url :: to(['article/index'、 'year' ⇒ 2014、 'category' ⇒ 'php'])は/index.php/articles/2014/phpを作成します
- Url :: to(['article/view'、 'id' ⇒ 100])は/index.php/article/100を作成します
- Url :: to(['article/view'、 'id' ⇒ 100、 'source' ⇒ 'ad'])は/index.php/article/100?source=adを作成します
- Url :: to(['article/index'、 'category' ⇒ 'php'])は/index.php/article/index?category=phpを作成します
- URLにサフィックスを追加するには、 *yii \ web \ UrlManager
- $ suffix* プロパティを構成する必要があります。
ステップ3 *- *config/web.php ファイルの urlComponent を変更します。
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'suffix' => 'l'
],
ステップ4 *-Webブラウザーのアドレスバーにアドレス *http://localhost:8080/site/contactl を入力すると、画面に次のように表示されます。 html サフィックスに注意してください。