編程學習網 > PHP技術 > Yii2 > yii2.0 UrlManage鏈接url規則解析
2016
07-07

yii2.0 UrlManage鏈接url規則解析

Url規則:


urlmanager配置為應用程序組件在[[yiiaseApplication]]。你可以通過Yii訪問實例:Yii::$app->urlManager

您可以通過添加一個數組到您的應用程序配置下的“components”中,示例如下:

 'urlManager' => [
      'enablePrettyUrl' => true,
      'rules' => [
          // your rules go here
      ],
      // ...
  ]

rules規則舉例:

    [
          'dashboard' => 'site/index',
     
          'POST <controller:w+>s' => '<controller>/create',
          '<controller:w+>s' => '<controller>/index',
     
          'PUT <controller:w+>/<id:d+>'    => '<controller>/update',
          'DELETE <controller:w+>/<id:d+>' => '<controller>/delete',
          '<controller:w+>/<id:d+>'        => '<controller>/view',
    ];


rules' => [
    // 為路由指定了一個別名,以 post 的復數形式來表示 post/index 路由
    'posts' => 'post/index',

    // id 是命名參數,post/100 形式的URL,其實是 post/view&id=100
    'post/<id:d+>' => 'post/view',

    // controller action 和 id 以命名參數形式出現
    '<controller:(post|comment)>/<id:d+>/<action:(create|update|delete)>'=> '<controller>/<action>',

    // 包含了 HTTP 方法限定,僅限于DELETE方法
    'DELETE <controller:w+>/<id:d+>' => '<controller>/delete',

    // 需要將 Web Server 配置成可以接收 *.digpage.com 域名的請求
    'http://<user:w+>.digpage.com/<lang:w+>/profile' => 'user/profile',]


掃碼二維碼 獲取免費視頻學習資料

Python編程學習

查 看2022高級編程視頻教程免費獲取