<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 2018 年 12 月 9 日 發布 `5.1`版本的路由其實是完全重構了,只是保留了和`5.0`幾乎一致的使用方法。詳細的用法官方手冊已經說的非常詳細了,本文專門整理了`5.1`路由使用的一些注意事項和技巧,可以作為你學習的指引。 ## 開啟路由 `5.1`版本的路由是默認開啟的,而且不能關閉,對于不使用路由的模塊你不定義路由即可。 所有的路由規則是不支持普通URL參數的,必須是PATHINFO地址。 路由定義文件不一定是`route.php`,事實上可以是任何文件名,如果有必要,你可以使用不同的路由定義文件管理不同模塊的路由。 如果你開啟了路由強制模式,那么未定義的路由訪問將會拋出異常。 ## 使用方法注冊路由規則 盡量使用路由類的方法注冊路由規則,數組配置定義路由規則的方式下一個版本已經取消,不再建議使用,方法定義對于路由緩存的支持更好。 推薦使用: ``` Route::get('hello/:name', 'index/hello'); ``` 而不建議使用: ``` return [ 'hello/:name' => 'index/hello', ]; ``` ## 明確定義請求類型 盡量明確定義路由的請求類型,提高路由解析的效率。 推薦使用: ``` Route::get('hello/:name', 'index/hello'); ``` 不建議使用: ``` Route::rule('hello/:name', 'index/hello'); ``` ## 不需要添加開頭斜線 除了首頁外,其它路由規則定義不需要添加開頭的斜線。 推薦使用: ``` Route::get('hello/:name', 'index/hello'); ``` 不建議使用: ``` Route::get('/hello/:name', 'index/hello'); ``` 除非是首頁路由 ``` Route::get('/', 'index/index'); ``` 或者直接訪問分組名的情況 ``` Route::group('blog', function() { Route::get('/', 'index/blog'); Route::get(':id$', 'blog/read'); Route::get(':id/edit$', 'blog/edit'); }); ``` ## 多使用路由分組 可能的情況下,盡可能多使用路由分組。可以充分利用分組的匹配機制提高路由解析性能。 推薦使用 ``` Route::group('blog', function() { Route::get(':id$', 'blog/read'); Route::get(':id/edit$', 'blog/edit'); }); ``` 不建議使用 ``` Route::get('blog/:id$', 'blog/read'); Route::get('blog/:id/edit$', 'blog/edit'); ``` ## 路由分組傳入額外參數 可以統一給分組路由傳入額外的參數 ``` Route::group('blog', [ ':id' => 'Blog/read', ':name' => 'Blog/read', ])->ext('html') ->pattern(['id' => '\d+']) ->append(['group_id'=>1]); ``` 上面的分組路由統一傳入了`group_id`參數,該參數的值可以通過`Request`類的`param`方法獲取。 ## 路由變量定義規范 對于新版的路由變量定義來說,不再區分普通變量和組合變量,哪怕你使用`:name`方式定義,內部也會統一轉換成`<name>` 這種方式,因此為了提高路由解析性能,建議統一使用: ``` Route::group('blog', function() { Route::get('<id>$', 'blog/read'); Route::get('<id>/edit$', 'blog/edit'); }); ``` 盡量明確定義路由變量的規則 ``` Route::group('blog', function() { Route::get('<id>$', 'blog/read'); Route::get('<id>/edit$', 'blog/edit'); })->pattern(['id' => '\d+']); ``` 路由變量規則采用正則表達式方式定義,但無需在開頭添加^或者在最后添加$,也無需使用模式修飾符,系統會自動添加。 不能使用`$_GET`方法或者Request類的`get`方法獲取路由變量,而應該使用`param`方法或者參數綁定。 不同于`5.0`版本的變量匹配解析,`5.1`版本的路由變量是整體匹配解析的,因此性能不同。 如果開啟了路由合并解析的話,分組下面的多個路由規則是通過一次解析匹配完成的,如果路由規則較多性能會有大幅提升,具體可以參考這篇:[5.1.7新特性](https://blog.thinkphp.cn/784292)中的「路由改進和提速」部分。 ## 變量分隔符 你可以很隨意的使用路由變量分隔符,只要注意不要和你的變量規則沖突。 ``` Route::get('item/<date><name>-<id>', 'product/item') ->pattern(['date'=>'\d{8}','name'=>'\w+','id'=>'\d+']); ``` ## 默認路由變量規則 默認情況下,如果你沒有定義變量規則,則使用`\w+`作為變量規則 ``` Route::get('hello/<name>', 'index/hello'); ``` 其實等同于 ``` Route::get('hello/<name>', 'index/hello')->pattern([ 'name' => '\w+', ]); ``` 如果你希望改變默認的路由變量規則,可以在應用配置文件中設置 ``` // 默認的路由變量規則 'default_route_pattern' => '[a-z0-9\-\_\.]+', ``` 路由變量規則的定義推薦使用`pattern`方法傳入數組的方式,下面的方式將來的版本將不再支持。 ``` Route::get('hello/<name>', 'index/hello')->pattern('name','\w+'); ``` ## 路由完全匹配 默認的路由規則只是匹配URL地址的開頭是否和定義的路由匹配 所以 ``` Route::get('hello/:name', 'index/hello'); ``` 可以匹配下面的URL地址 ``` hello/think hello/think/thinkphp hello/think/thinkphp/demo ``` 但如果添加了路由完全匹配后 ``` Route::get('hello/:name$', 'index/hello'); ``` 上面的三個URL地址中就只會匹配 ``` hello/think ``` 建議你開啟全局路由完全匹配 ``` // 路由完全匹配 'route_complete_match' => true, ``` 如果有個別路由仍然希望不要完全匹配,你可以使用 ``` Route::get('hello/:name', 'index/hello') ->completeMatch(false); ``` 關閉當前路由規則(或者路由分組)的完整匹配。 ## 定義路由標識 路由標識的作用只是用于URL地址生成,而且默認會使用當前的路由地址作為路由標識。 ``` Route::get('hello/:name', 'index/hello'); echo url('index/hello', ['name' => 'think']); ``` 如果指定了路由標識的話,url方法的用法就需要調整為: ``` Route::get('hello/:name', 'index/hello')->name('hello'); echo url('hello', ['name' => 'think']); ``` 如果你希望簡化URL地址的生成調用,可以在項目規范中強制統一規范,而不要使用URL地址這種默認標識,使用路由標識的優勢是即使路由地址發生了變化,也無需改變url方法的代碼。 ## 路由地址盡量不使用閉包定義 路由地址使用閉包的方式雖然有時候很方便,例如: ``` Route::get('hello/:name', function ($name) { return 'Hello,' . $name; }); ``` 但由于無法支持路由緩存,所以請謹慎使用。 ## 用方法定義路由參數 出于語義化考慮,5.1版本的路由參數建議使用方法而不是參數。 推薦使用(支持IDE) ``` Route::get('new/:id', 'News/read') ->ext('html') ->https(); ``` 不建議使用: ``` Route::get('new/:id','News/read',['ext'=>'html','https'=>true]); ``` ## 路由管理 路由定義可以按模塊分開多個文件管理,但最終都是會加載和進行匹配,不要以為只會加載當前模塊的,在路由解析完成之前,根本不知道當前的模塊是什么。多個文件定義路由僅僅是為了讓你更方便管理而已。 如果你試圖用這種方式給不同模塊定義MISS路由,顯然是錯誤的做法。 正確的姿勢是通過設置分組的MISS路由來給模塊定義。 ## MISS路由 一旦你設置了全局的`MISS`路由,相當于開啟了強制路由模式。 ``` Route::miss('public/miss'); ``` MISS路由可以針對不同路由分組(或者域名)設置,也可以針對不同的請求類型設置, ``` Route::group('blog', function() { Route::get(':name$', 'blog/read'); ... Route::miss('blog/error','get'); Route::miss('blog/noAuth','post'); }); Route::group('user', function() { Route::get(':id', 'user/info'); ... Route::miss('user/error'); }); ``` ## 路由綁定到模塊 你可以把某個域名(或者分組)綁定到模塊 ``` Route::domain('blog', 'blog'); ``` 如果想在綁定模塊的同時定義一些特殊的路由規則,可以使用 ``` Route::domain('blog', function () { Route::rule(':id', 'index/blog/read'); Route::rule(user/':user', 'index/user/info'); })->bind('blog'); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看