<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # **路由:** ThinkPHP5.0的路由規則定義是從根目錄開始,而不是基于模塊名的。 application/route.php注冊? ?然后訪問變成`http://serverName/new/5` ~~~ use think\Route; // 注冊路由到index模塊的News控制器的read操作 Route::rule('new/:id','index/News/read'); ~~~ ## **參數配置:** ``` // 是否開啟路由 'url_route_on' => true, // 路由配置文件(支持配置多個) 'route_config_file' => ['route'], // 路由使用完整匹配 'route_complete_match' => false, // 是否強制使用路由? ? 為true的話必須要為每個url配置路由才能訪問哦 所以才開發階段可以false 這樣原url和路由url都可以訪問到 'url_route_must' => false, ``` 路由模式:**普通**、**強制**和**混合**模式 **1、普通模式:** ~~~ //配置文件 關閉路由,完全使用默認的PATH_INFO方式URL 'url_route_on' => false, ~~~ 路由關閉后,不會解析任何路由規則,采用默認的`PATH_INFO`模式訪問URL: ~~~ http://serverName/index.php/module/controller/action/param/value/... ~~~ 關閉路由后的普通模式任然可以通過操作方法的 **[參數綁定](https://www.cnblogs.com/lichihua/p/10405130.html)**、控制器和空操作等特性實現url地址的簡化 可以設置`url_param_type`配置參數來改變pathinfo模式下面的參數獲取方式,默認是按名稱成對解析,支持按照順序解析變量,只需要更改為: ~~~ // 按照順序解析變量 'url_param_type' => 1, ~~~ **2、強制模式:** ~~~ //配置文件如下配置 'url_route_on' => true, 'url_route_must' => true, //這種方式下面必須嚴格給每一個訪問地址定義路由規則(包括首頁),否則將拋出異常。 //首頁的路由規則采用/定義即可,例如下面把網站首頁路由輸出Hello,world! Route::get('/',function(){ return 'Hello,world!'; }); ~~~ **3、混合模式:** ~~~ //開啟路由,并使用路由定義+默認PATH_INFO方式的混合 //配置如下 'url_route_on' => true, 'url_route_must'=> false, //該方式下面,只需要對需要定義路由規則的訪問地址定義路由規則,其它的仍然按照第一種普通模式的PATH_INFO模式訪問URL ~~~ 注冊方式:**動態注冊**和**路由配置**兩種方式 >[danger]由于檢測機制問題,**動態注冊的性能比路由配置要高一些**,尤其是多種請求類型混合定義的時候。 **1、動態注冊:** 路由定義采用`\think\Route`類的`rule`方法注冊,通常是在應用的路由配置文件`application/route.php`進行注冊,格式是 >[info]### **Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)');** 請求類型不指定的話默認為任何請求類型包含(GET、POST、PUT、DELETE、*表示任何其他請求) ~~~ 單個注冊: Route::rule('路由規則表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)'); 例子: Route::rule( 'theme/:id/[:name]', 'home/Goods/theme', 'GET', //路由參數 [ 'method'=>'POST|GET|PUT',//請求類型檢測 'ext'=>'shtml|html|php',//檢測是shtml|html|php這三個后綴的請求就通過 'deny_ext'=>'jpg|png|gif',//URL禁止后綴檢測,這里禁止檢測jpg|png|gif者三個后綴 'domain'=>'news.thinkphp.cn',//子域名檢測則直接news就行'domain'=>'news'] 'https'=>true,//檢測是否https請求 'before_behavior'=>'\app\index\behavior\UserCheck',//前置行為(檢測) 'after_behavior'=>'\app\index\behavior\ReadInfo',//后置行為(執行) 'callback'=>'my_check_fun',//自定義檢測方法 'merge_extra_vars'=>true,//合并額外參數 'cache'=>3600,//請求緩存V5.0.1+ 'bind_model'=>['User','name'],//綁定模型V5.0.1+ 'param_depr'=>'',//路由參數分隔符V5.0.2+ 'ajax'=>true,//V5.0.2+ 'pjax'=>true,//V5.0.2+ ], ['name'=>'\w+','id'=>'\d+'] ); 批量注冊: Route::rule( [ '路由規則表達式1'=>'路由地址和參數', '路由規則表達式2'=>['路由地址和參數','路由參數(數組)','變量規則(數組)'] ], '', '請求類型', '路由參數(數組)', '變量規則' ); //批量注冊例子 Route::rule( [ 'new/:id' => 'News/read', 'blog/:id' => ['Blog/update',['ext'=>'shtml'],['id'=>'\d{4}'] ], ], '', 'GET', ['ext'=>'html'], ['id'=>'\d+'] ); ~~~ ### **路由規則表達式** 通常包含靜態地址和動態地址,或者兩種地址的結合,例如下面都屬于有效的規則表達式: ~~~ Route::rule('/', 'index'); // 首頁訪問路由 Route::rule('my', 'Member/myinfo'); // 靜態地址路由 Route::rule('blog/:id', 'Blog/read'); // 靜態地址和動態地址結合 Route::rule('new/:year/:month/:day', 'News/read'); // 靜態地址和動態地址結合 Route::rule(':user/:blog_id', 'Blog/read'); // 全動態地址 //當控制器文件不再controler文件夾下而是在controller子文件夾下時我們定義路由地址格式為\[子文件名.? 控制器名\] //controller/v1/Banner.php 訪問為:http://www.localhost/api/v1/banner/1 Route::get("api/v1/banner/:id","api/v1.Banner/getBanner"); //下面是動態調用(如不同版本) Route::get("api/:version/banner/:id","api/:version.Banner/getBanner"); //當前新品 Route::get("api/:version/product/recent","api/:version.Product/getRecent"); ~~~ ### **路由地址:** ~~~ 1、路由到模塊/控制器/操作:'[模塊/控制器/]操作?額外參數1=值1&額外參數2=值2...' // 路由到默認或者用define('BIND_MODULE','index');綁定的模塊 'blog/:id'=>'blog/read', // 路由到admin模塊 'blog/:id'=>'admin/blog/read', //支持多級控制器,路由到index/controller/group/Blog 'blog/:id'=>'index/group.blog/read', //支持路由到動態的模塊、控制器或者操作 // action變量的值作為操作方法傳入 ':action/blog/:id' => 'index/blog/:action' // 變量傳入index模塊的控制器和操作方法 ':c/:a'=> 'index/:c/:a' //額外參數`status`和`app_id`參數都是URL里面不存在的,屬于隱式傳值,當然并不一定需要用到,只是在需要的時候可以使用。 'blog/:id'=>'blog/read?status=1&app_id=5', 2、路由到操作方法: @[模塊/控制器/]操作 //區別是直接執行某個控制器類的方法,而不需要去解析**模塊/控制器/操作**這些,同時也不會去初始化模塊 //前者走模塊初始化的 這個是直接調用控制器類 性能好點 //直接執行Loader::action('index/blog/read');相當于直接調用 \app\\index\controller\blog類的read方法。 'blog/:id'=>'@index/blog/read', 3、路由到類的方法 動態:\完整的類命名空間\類名@方法名 靜態:\完整的類命名空間\類名::方法名 // \app\index\service\Blog類的read方法 'blog/:id'=>'\app\index\service\Blog@read', 'blog/:id'=>'\app\index\service\Blog::read',//這個read是靜態方法 'blog/:id'=>'\app\index\service\Blog::read?status=1',//這個read是靜態方法 4、路由到重定向地址:'外部地址'(默認301重定向) 或者 \['外部地址','重定向代碼'\] 必須以“/”或者http開頭 'blog/:id'=>'/blog/read/id/:id' // 表示當前網站(可能是http://thinkphp.cn )的 blog/123地址 // 會直接重定向到http://blog.thinkphp.cn/read/123。 'blog/:id'=>'http://blog.thinkphp.cn/read/:id' ~~~ **路由地址參數可選定義** 變量用 [ ] 包含起來后就表示該變量是路由匹配的可選變量 ~~~ Route::get('blog/:year/[:month]','Blog/archive'); 下面的URL訪問地址都可以被正確的路由匹配: http://serverName/index.php/blog/2015 http://serverName/index.php/blog/2015/12 ~~~ ### **路由參數** 路由參數以數組的形式出現,他們都是可選的,根據實際情況組合 ``` 'method'=>'POST|GET|PUT',//請求類型檢測 'ext'=>'shtml|html|php',//檢測是shtml|html|php這三個后綴的請求就通過 'deny_ext'=>'jpg|png|gif',//URL禁止后綴檢測,這里禁止檢測jpg|png|gif者三個后綴 'domain'=>'news.thinkphp.cn',//子域名檢測則直接news就行'domain'=>'news'] 'https'=>true,//檢測是否https請求 'before_behavior'=>'\app\index\behavior\UserCheck',//前置行為(檢測) 'after_behavior'=>'\app\index\behavior\ReadInfo',//后置行為(執行) 'callback'=>'my_check_fun',//自定義檢測方法 'merge_extra_vars'=>true,//合并額外參數 'cache'=>3600,//請求緩存V5.0.1+ 'bind_model'=>['User','name'],//綁定模型V5.0.1+ 'param_depr'=>'',//路由參數分隔符V5.0.2+ 'ajax'=>true,//V5.0.2+ 'pjax'=>true,//V5.0.2+ ``` ### **變量規則** ### 設置全局變量規則,全部路由有效: ~~~ 全局變量規則,作用于全部路由 // 設置name變量規則(采用正則定義) Route::pattern('name','\w+'); // 支持批量添加 Route::pattern([ 'name' => '\w+', 'id' => '\d+', ]); 局部變量規則,僅在當前路由有效 // 定義GET請求路由規則 并設置name變量規則 Route::get('new/:name','News/read',[],['name'=>'\w+']); ~~~ ### **請求類型** 默認支持所有的請求類型,可單獨設置,多個用|分隔 ~~~ //如果要定義get和post請求支持的路由規則,也可以用: Route::rule('new/:id','News/read','GET|POST'); ~~~ 不同的請求類型定義路由規則的簡化方法:請求類型參數必須大寫 ~~~ Route::get('new/:id','News/read'); // 定義GET請求路由規則 Route::post('new/:id','News/update'); // 定義POST請求路由規則 Route::put('new/:id','News/update'); // 定義PUT請求路由規則 Route::delete('new/:id','News/delete'); // 定義DELETE請求路由規則 Route::any('new/:id','News/read'); // 所有請求都支持的路由規則 ~~~ ## **路由標志:設置一個唯一的用于url生成的字符** ~~~ // 注冊路由到index模塊的News控制器的read操作 Route::name('new_read')->rule('new/:id','index/News/read'); //生成路由地址的時候就可以使用 url('new_read',['id'=>10]); //如果不定義路由標識的話,使用下面的方式生成 url('index/News/read',['id'=>10]); V5.1.6+ 版本開始,路由標識的用法調整,原來的用法: // 注冊路由到index模塊的News控制器的read操作 Route::name('new_read')->rule('new/:id','index/News/read'); 需要改為: // 注冊路由到index模塊的News控制器的read操作 Route::rule('new/:id','index/News/read')->name('new_read'); 因為后者更符合語義。 ~~~ ## **路由別名:Route::alias('別名','模型/控制器');** ~~~ use think\Route; Route::alias('home','index/index'); Route::alias('admin','admin/index'); 或者: return [ '__alias__' => [ 'home' => 'index/index', 'admin'=> 'admin/index' ], ]; ~~~ ## **資源路由** ~~~ //注冊資源路由(資源路由會自動注冊7個路由規則分別對應下面新建的這幾個方法) //動態注冊 Route::resource('user','index/user'); //或者路由配置 return [ // 定義資源路由 '__rest__'=>[ // 指向index模塊的User控制器 'user'=>'index/user', ], // 定義普通路由 'hello/:id'=>'index/hello', ]; //在指向index模塊的User控制器新建index()、create($id)、save、read($id)、eidit($id)、update($id)、delete($id)方法 URL訪問: index:訪問用戶列表【GET】index方法可省略 http://serverName/user/ create:創建用戶頁面【GET】 http://serverName/user/create save:創建用戶【POST】 http://serverName/user/ read:獲取單個用戶信息【GET】 http://serverName/user/128 edit:獲取編輯數據(更新頁面)【GET】 http://serverName/user/128/edit update:更新單個用戶(實際執行編輯更新)【PUT】 http://serverName/user/128 delete:刪除單個用戶【DELETE】 http://serverName/user/128 ~~~ 將參數$id定義為$blog_id ~~~ Route::resource('user','index/user',['var'=>['user'=>'user_id']]); 對應方法變為 index()、create($user_id)、save、read($user_id)、eidit($user_id)、update($user_id)、delete($user_id)方法 ~~~ 也可以在定義資源路由的時候限定執行的方法(標識),例如: ~~~ // 只允許index read edit update 四個操作 Route::resource('user','index/user',['only'=>['index','read','edit','update']]); // 排除index和delete操作 Route::resource('user','index/user',['except'=>['index','delete']]); ~~~ 資源路由的標識不可更改,但生成的路由規則和對應操作方法可以修改。 數組第一個參數:請求類型 數組第二個參數:請求規則 數組第三個參數:路由標識對應的控制器方法請求 ~~~ // 修改之前看下默認的REST路由操作方法定義 private static $rest = [ 'index' => ['get', '', 'index'], 'create' => ['get', '/create', 'create'], 'edit' => ['get', '/:id/edit', 'edit'], 'read' => ['get', '/:id', 'read'], 'save' => ['post', '', 'save'], 'update' => ['put', '/:id', 'update'], 'delete' => ['delete', '/:id', 'delete'], ]; Route::rest('create',['GET', '/add','add']); //支持批量更改 Route::rest([ 'index'=>['GET','','front'], 'create'=>['GET', '/add','add'] 'save' => ['POST', '', 'store'], 'update' => ['PUT', '/:id', 'save'], 'delete' => ['DELETE', '/:id', 'destory'], ]); URL訪問: index:訪問用戶列表【GET】訪問的是front方法而不是index方法 http://serverName/user create:創建用戶頁面【GET】 http://serverName/user/add save:創建用戶【POST】訪問的是store方法了而不是默認的save方法 http://serverName/user/ read:獲取單個用戶信息【GET】 http://serverName/user/128 edit:獲取編輯數據(更新頁面)【GET】 http://serverName/user/128/edit update:更新單個用戶(實際執行編輯更新)【PUT】訪問的是save方法而不是update方法 http://serverName/user/128 delete:刪除單個用戶【DELETE】訪問的是destory方法而不是delete方法 http://serverName/user/128 ~~~ ### 資源嵌套 支持資源路由的嵌套,例如: ~~~ Route::resource('blog.comment','index/comment'); ~~~ 就可以訪問如下地址: ~~~ http://serverName/blog/128/comment/32 http://serverName/blog/128/comment/32/edit ~~~ 生成的路由規則分別是: ~~~ blog/:blog_id/comment/:id blog/:blog_id/comment/:id/edit ~~~ Comment控制器對應的操作方法如下: ~~~ namespace app\index\controller; class Comment{ public function edit($id,$blog_id){ } } ~~~ edit方法中的參數順序可以隨意,但參數名稱必須滿足定義要求。 如果需要改變其中的變量名,可以使用: ~~~ // 更改嵌套資源路由的blog資源的資源變量名為blogId Route::resource('blog.comment','index/comment',['var'=>['blog'=>'blogId']]); ~~~ Comment控制器對應的操作方法改變為: ~~~ namespace app\index\controller; class Comment{ public function edit($id,$blogId) { } } ~~~ `Route::resource('blog','index/blog');`路由規則: | 標識 | 請求類型 | 生成路由規則 | 對應操作方法(默認) | url地址| | --- | --- | --- | --- | --- | | index | GET | `blog` | index |http://serverName/blog| | create | GET | `blog/create` | create($id) | http://serverName/blog/create| | save | POST | `blog` | save | http://serverName/blog/| | read | GET | `blog/:id` | read($id) | http://serverName/blog/128| | edit | GET | `blog/:id/edit` | edit($id) | http://serverName/blog/128/edit| | update | PUT | `blog/:id` | update($id) |http://serverName/blog/128| | delete | DELETE | `blog/:id` | delete($id) |http://serverName/blog/128| 資源路由的嵌套`Route::resource('blog.comment','index/comment'` 會對應index模塊的comment控制器 路由規則 | 標識 | 請求類型 | 生成路由規則 | 對應操作方法(默認) |url地址| | --- | --- | --- | --- | --- | | index | GET | `blog` | index |http://serverName/blog| | create | GET | `blog/create` | create($id) | http://serverName/blog/create| | save | POST | `blog` | save | http://serverName/blog/| | read | GET | blog/:blog_id/comment/:id | read($id) | http://serverName/blog/128| | edit | GET | blog/:blog_id/comment/:id/edit | edit($id,$blog_id) | http://serverName/blog/128/comment/32/edit| | update | PUT | blog/:blog_id/comment/:id | update($id) |http://serverName/blog/128| | delete | DELETE | blog/:blog_id/comment/:id | delete($id) |http://serverName/blog/128| 參數的順序隨便 同理也可以更改參數變量名 ~~~ // 更改嵌套資源路由的blog資源的資源變量名為blogId Route::resource('blog.comment','index/comment',['var'=>['blog'=>'blogId']]); 修改后方法變為:edit($id,$blogId)或edit($blogId, $id) ~~~ ## **快捷路由** ``` //快捷路由 Route::controller('user','模塊/控制器'); // 給User控制器設置快捷路由 控制器方法為 請求類型+方法名 如getInfo()與postInfo() // get http://localhost/user/phone post http://localhost/user/info Route::controller('user','index/User'); ``` ## **路由別名** 不支持變量類型和路由條件判斷,單純只是為了縮短URL地址 不推薦 ~~~ //我們希望使用user可以訪問Home模塊的User控制器的所有操作 那么我么定義別名為 Route::alias('user','home/User'); //或者 return [ '__alias__' => [ 'user' => 'home/User', //'user' => ['home/User',['ext'=>'html']], ], ]; //路由別名可以指向任意一個有效的路由地址,例如下面指向一個類 // user 路由別名指向 User控制器類 Route::alias('user','\app\index\controller\User'); // user 別名路由到 index/user 控制器 Route::alias('user','index/user',[ 'ext'=>'html',//設置路由條件 'allow'=>'index,read,edit,delete',//白名單 'except'=>'save,delete',//黑名單 'method'=>['index'=>'GET','save'=>'POST','delete'=>'DELETE'],//設置操作方法的請求類型 ]); ~~~ ## **路由分組** ~~~ Route::rule( [ 'blog/:id' => ['Blog/read', ['method' => 'get'], ['id' => '\d+']], 'blog/:name' => ['Blog/read', ['method' => 'post']], ], ); //可以合并到一個blog分組 Route::rule( [ '[blog]' => [ ':id' => ['Blog/read', ['method' => 'get'], ['id' => '\d+']], ':name' => ['Blog/read', ['method' => 'post']], ], ], ); ~~~ 也可以使用group方法進行分組 ``` //路由分組 允許把相同前綴的路由定義合并分組 Route::group('blog',[ ':id' => ['Blog/read', ['method' => 'get'], ['id' => '\d+']], ':name' => ['Blog/read', ['method' => 'post']], ],['method'=>'get','ext'=>'html']); ``` 支持使用閉包方式注冊路由分組,例如: ~~~ Route::group('blog',function(){ Route::any(':id','blog/read',[],['id'=>'\d+']); Route::any(':name','blog/read',[],['name'=>'\w+']); },['method'=>'get','ext'=>'html']); ~~~ 如果僅僅是用于對一些路由規則設置一些公共的路由參數,也可以使用: ~~~ Route::group(['method'=>'get','ext'=>'html'],function(){ Route::any('blog/:id','blog/read',[],['id'=>'\d+']); Route::any('blog/:name','blog/read',[],['name'=>'\w+']); }); ~~~ 路由分組支持嵌套,例如: ~~~ Route::group(['method'=>'get','ext'=>'html'],function(){ Route::group('blog',function(){ Route::any('blog/:id','blog/read',[],['id'=>'\d+']); Route::any('blog/:name','blog/read',[],['name'=>'\w+']); } }); ~~~ ## **MISS路由** 如果希望在沒有匹配到所有的路由規則后執行一條設定的路由,可以使用`MISS`路由功能,只需要在路由配置文件中定義: ~~~ return [ 'new/:id' => 'News/read', 'blog/:id' => ['Blog/update',['method' => 'post|put'], ['id' => '\d+']], '__miss__' => 'public/miss', ]; ~~~ 或者使用`miss`方法注冊路由 ~~~ Route::miss('public/miss'); ~~~ 當沒有匹配到所有的路由規則后,會路由到`public/miss`路由地址。 ## 分組MISS路由 分組支持獨立的`MISS`路由,例如如下定義: ~~~ return [ '[blog]' => [ 'edit/:id' => ['Blog/edit',['method' => 'get'], ['id' => '\d+']], ':id' => ['Blog/read',['method' => 'get'], ['id' => '\d+']], '__miss__' => 'blog/miss', ], 'new/:id' => 'News/read', '__miss__' => 'public/miss', ]; ~~~ 如果使用`group`方法注冊路由的話,可以使用下面的方式: ~~~ Route::group('blog',function(){ Route::rule(':id','blog/read',[],['id'=>'\d+']); Route::rule(':name','blog/read',[],['name'=>'\w+']); Route::miss('blog/miss'); },['method'=>'get','ext'=>'html']); ~~~ ## **閉包定義** 我們可以使用閉包的方式定義一些特殊需求的路由,而不需要執行控制器的操作方法了,例如: ~~~ Route::get('hello',function(){ return 'hello,world!'; }); //參數傳遞,規則路由中定義的動態變量的名稱 就是閉包函數中的參數名稱,不分次序。 Route::get('hello/:name',function($name){ return 'Hello,'.$name; }); 因此,如果我們訪問的URL地址是:http://serverName/hello/thinkphp 則瀏覽器輸出的結果是:Hello,thinkphp ~~~ ## **路由綁定** ### **綁定到模塊/控制器/操作** 把當前的URL綁定到模塊/控制器/操作,最多支持綁定到操作級別,例如在路由配置文件中添加: ~~~ // 綁定當前的URL到 index模塊 Route::bind('index'); // 綁定當前的URL到 index模塊的blog控制器 Route::bind('index/blog'); // 綁定當前的URL到 index模塊的blog控制器的read操作 Route::bind('index/blog/read'); ~~~ 例子: ``` http://serverName/index/blog/read/id/5 如果定義了路由 Route::get('index/blog/:id','index/blog/read'); 訪問路徑變為 http://serverName/index/blog/5 假如我們綁定到了index模塊的blog控制器 Route::bind('index/blog'); 那么訪問URL就變成了 http://serverName/5 ``` ~~~ // 綁定命名空間 Route::bind('\app\index\controller','namespace');//通過http://serverName/blog/read/id/5就可以直接訪問 \app\index\controller\Blog類的read方法。 // 綁定到類 Route::bind('\app\index\controller\Blog','class');//通過http://serverName/read/id/5就可以直接訪問 \app\index\controller\Blog類的read方法。 //入口文件綁定 // 復制一份入口文件添加define('BIND_MODULE','admin'); 命名為demo.php 我們訪問這個demo.php就訪問的是admin模塊 // 自動入口綁定 // 配置文件開啟入口文件自動綁定模塊 'auto_bind_module' => true, //復制一份index.php 改名為demo.php 訪問index.php是訪問index模塊 訪問的demo.php則訪問得是demo模塊 ~~~ ## 模型綁定(`V5.0.1`) 路由規則和分組支持綁定模型數據,例如: ~~~ Route::rule('hello/:id','index/index/hello','GET',[ 'ext' => 'html', 'bind_model' => [ 'user' => '\app\index\model\User', ], ]); ~~~ 會自動給當前路由綁定`id`為 當前路由變量值的`User`模型數據。 可以定義模型數據的查詢條件,例如: ~~~ Route::rule('hello/:name/:id','index/index/hello','GET',[ 'ext' => 'html', 'bind_model' => [ 'user' => ['\app\index\model\User','id&name'] ], ]); ~~~ 表示查詢`id`和`name`的值等于當前路由變量的模型數據。 也可以使用閉包來返回模型對象數據 ~~~ Route::rule('hello/:id','index/index/hello','GET',[ 'ext' => 'html', 'bind_model' => [ 'user' => function($param){ $model = new \app\index\model\User; return $model->where($param)->find(); } ], ]); ~~~ 閉包函數的參數就是當前請求的URL變量信息。 在控制器中可以通過下面的代碼或者使用依賴注入獲取: ~~~ request()->user; ~~~ > 綁定的模型可以直接在控制器的架構方法或者操作方法中自動注入,具體可以參考請求章節的依賴注入。 ## **域名路由** 全一點的例子 ~~~ use think\Route; // 注冊路由到index模塊的News控制器的read操作 Route::rule('show/:id','test/Index/index');//id必須 Route::rule('show/[:id]','test/Index/index');//id可選 Route::rule('new/:id','News/update','POST');//第三個參數為空則表示接受所有的請求類型的參數 //完全匹配 Route::rule('show/:id$','test/Index/index');//只有http://www.admindemo2.com/show/2才能訪問而去掉$的話http://www.admindemo2.com/show/2/3/tom/...也能通過 //全局完全匹配(開啟全局完全匹配后所有的都不用再加$) //配置文件添加 //'route_complete_match' => true,//開啟路由定義的全局完全匹配 //當開啟全局完全匹配的時候,如果個別路由不需要使用完整匹配,可以添加路由參數覆蓋定義: //Route::rule('new/:id','News/read','GET|POST',['complete_match' => false]); Route::get('routeget','test/Index/routeGet'); // 定義GET請求路由規則,同Route::rule('routeget','test/Index/routeGet','GET');//只接受get的請求 Route::get('routeget/:id','test/Index/routeGet'); // 同//Route::rule('routeget/:id','test/Index/routeGet','GET');//只接受get的請求 //注意: Route::get('routeget','test/Index/routeGet');與Route::get('routeget/:id','test/Index/routeGet');同時定義相當于Route::get('routeget/[:id]]','test/Index/routeGet');文檔說明:注冊多個路由規則后,系統會依次遍歷注冊過的滿足請求類型的路由規則,一旦匹配到正確的路由規則后則開始調用控制器的操作方法,后續規則就不再檢測 所以為了避免沖突最好將優先級高的放在最前面(比如單個設置的變量規則和路由規則放前面,全局的放在最后面) Route::rule('routepost','test/Index/routePost','POST');//post的請求才能接受 Route::rule('routepostorget','test/Index/routePostOrGet','POST|GET');//只接受get和post請求,其他一律不接受 // 定義new路由命名標識 Route::rule(['name','routenameflag/:id'],'test/Index/routeNameFlag'); //http://www.admindemo2.com/routenmeflag/1 //{:url('name',['id'=>1])} Route::rule(['name2','routenameflag2/'],'test/Index/routeNameFlag2');//第二個參數有沒有/都無所謂 //http://www.admindemo2.com/routenmeflag2 or //http://www.admindemo2.com/routenmeflag2/ //{:url('name2')} Route::rule(['name3','routenameflag3/[:id]'],'test/Index/routeNameFlag3'); //http://www.admindemo2.com/routenmeflag3 or //http://www.admindemo2.com/routenmeflag3/1 //{:url('name3')} or //{:url('name',['id'=>1])} //批量注冊: Route::rule(['new/:id'=>'News/read','blog/:name'=>'Blog/detail']); Route::get(['new/:id'=>'News/read','blog/:name'=>'Blog/detail']); /* Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)'); Route::rule( [ '路由規則1'=>'路由地址和參數', '路由規則2'=>['路由地址和參數','匹配參數(數組)','變量規則(數組)'] ], '', '請求類型', '匹配參數(數組)', '變量規則' ); */ Route::rule( [ 'new/:id' => 'News/read', 'blog/:id' => ['Blog/update',['ext'=>'shtml'],['id'=>'\d{4}']], ':action/blog/:id' => 'index/blog/:action',// action變量的值作為操作方法傳入 ':c/:a'=> 'index/:c/:a',// 變量傳入index模塊的控制器和操作方法 ], '', 'GET', ['ext'=>'html'], ['id'=>'\d+'] ); //變量規則 //1、設置單個路由全局的變量規則 Route::rule('new/:tittle','News/update'); // 設置tittle變量規則(采用正則定義) Route::pattern('name','\w+'); //2、批量設置路由全局規則 Route::rule('new/:tittle','News/update'); Route::rule('banner/:id','home/Goods/banner'); Route::pattern([ 'tittle' => '\w+', 'id' => '\d+', ]); //3、設置局部變量規則,僅在當前路由有效 Route::get('new/:name','News/read',[],['name'=>'\w+']); //4、完整URL規則 Route::get('allurl/:catergory','test/Index/allUrl',[],['__url__'=>'allurl\/[A-Za-z0-9]{1,}$']);//id必須 //組合變量 //http://www.admindemo2.com/item-sb or http://www.admindemo2.com/item-sb555 or http://www.admindemo2.com/item-sb555a /*Route::get('item-<name><id?>','test/Index/product',[],['name'=>'\w+','id'=>'\d+']);*/ // http://www.admindemo2.com/item-sb-555 Route::get('item-<name>-<id>','test/Index/product',[],['name'=>'\w+','id'=>'\d+']); Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)'); 例子: Route::rule( 'theme/:id/[:name]', 'home/Goods/theme',//路由地址 'GET', //路由參數 [ 'method'=>'POST|GET|PUT',//請求類型檢測 'ext'=>'shtml|html|php',//檢測是shtml|html|php這三個后綴的請求就通過 'deny_ext'=>'jpg|png|gif',//URL禁止后綴檢測,這里禁止檢測jpg|png|gif者三個后綴 'domain'=>'news.thinkphp.cn',//子域名檢測則直接news就行'domain'=>'news'] 'https'=>true,//檢測是否https請求 'before_behavior'=>'\app\index\behavior\UserCheck',//前置行為(檢測) 'after_behavior'=>'\app\index\behavior\ReadInfo',//后置行為(執行) 'callback'=>'my_check_fun',//自定義檢測方法 'merge_extra_vars'=>true,//合并額外參數 'cache'=>3600,//請求緩存V5.0.1+ 'bind_model'=>['User','name'],//綁定模型V5.0.1+ 'param_depr'=>'',//路由參數分隔符V5.0.2+ 'ajax'=>true,//V5.0.2+ 'pjax'=>true,//V5.0.2+ ], ['name'=>'\w+','id'=>'\d+'] ); //路由地址 //1、額外參數: Route::get('extraparams/:id','test/Index/extraParam?status=1&app_id=5'); // http://www.admindemo2.com/extraparams/2/3 訪問 // Request::instance()->route()能獲取到id、status、app_id //2 路由到模塊/控制器 [模塊/控制器/]操作?參數1=值1&參數2=值2... //2.1 路由到默認或者綁定模塊 Route::rule('blog/:id','index/read'); //2.2 路由到index模塊 Route::rule('blog/:id','test/index/read'); //2.3、路由到動態的模塊、控制器或者操作 //http://www.admindemo2.com/moviemodel/index/1 訪問到的是test/Index/movieModel() Route::rule(':action/index/:id','test/Index/:action'); //http://www.admindemo2.com/moviemodel/lichihua/1 訪問到的是test/Index/movieModel() Route::rule(':action/lichihua/:id','test/Index/:action'); //4 路由到操作方法 @[模塊/控制器/]操作 //'blog/:id'=>'@index/blog/read', 系統會直接執行 Loader::action('index/blog/read'); //這種方式看起來似乎和第一種是一樣的,本質的區別是直接執行某個控制器類的方法,而不需要去解析 模塊/控制器/操作這些,同時也不會去初始化模塊,視圖的默認模板規則失效,所以這種情況下面,如果使用了視圖模板渲染,則必須傳入明確的參數 Route::get('test/:id','@test/Index/dongTest?status=1'); //?? //路由到類的方法 路由地址的格式為(動態方法):\類的命名空間\類名@方法名 或者(靜態方法):\類的命名空間\類名::方法名 //可以支持執行任何類的方法,而不僅僅是執行控制器的操作方法 Route::rule('blog/:id','\app\index\service\Blog@read');//執行的是 \app\index\service\Blog類的read方法 Route::rule('blog/:id','\app\index\service\Blog::read'); //V5.0.4+版本開始,支持傳入額外的參數作為方法的參數調用(用于參數綁定),例如: Route::rule('blog/:id','\app\index\service\Blog::read?status=1'); //路由到重定向地址 以/或者http 開頭 Route::rule('wolichihua/:id','/test/:id'); //http://www.admindemo2.com/wolichihua/1 訪問到了est/Index/dongTest即上面定義的test/:id' //資源路由 //在指向index模塊的blog控制器新建index()、create($id)、save、read($id)、eidit($id)、update($id)、delete($id)方法然后動態注冊(資源路由會自動注冊7個路由規則分別對應這幾個方法) 詳情參考資源路由 Route::resource('blog','index/blog'); //或者路由配置 return [ // 定義資源路由 '__rest__'=>[ // 指向index模塊的blog控制器 'blog'=>'index/blog', ], // 定義普通路由 'hello/:id'=>'index/hello', ]; //快捷路由 Route::controller('user','模塊/控制器'); // 給User控制器設置快捷路由 控制器方法為 請求類型+方法名 如getInfo()與postInfo() // get http://localhost/user/phone post http://localhost/user/info Route::controller('user','index/User'); //路由別名 //我們希望使用user可以訪問Home模塊的User控制器的所有操作 那么我么定義別名為 Route::alias('user','home/User'); //或者 return [ '__alias__' => [ 'user' => 'home/User', ], ]; //路由別名可以指向任意一個有效的路由地址,例如下面指向一個類 // user 路由別名指向 User控制器類 Route::alias('user','\app\index\controller\User'); //路由分組 允許把相同前綴的路由定義合并分組 Route::group('blog',[ ':id' => ['Blog/read', ['method' => 'get'], ['id' => '\d+']], ':name' => ['Blog/read', ['method' => 'post']], ],['ext'=>'html']); //MISS路由:沒有匹配到所有的路由規則后執行一條設定的路由,可以使用MISS路由功能, MISS路由配置后相當于開啟了強制路由 //全局MISS路由 //Route::miss(); 放到路由的第一行才能生效,放到最后一行或者return里面不生效????? Route::miss('public/miss');//當沒有匹配到所有的路由規則后,會路由到 public/miss路由地址。 //分組MISS路由 分組支持獨立的MISS路由 // // //閉包支持: Route::get('hello/:name',function($name){ return 'Hello,'.$name; }); //閉包還支持模板師視圖 Route::get('/', function () { return view('admin@home/index'); }); //路由綁定 // 綁定當前的URL到 index模塊 Route::bind('index'); // 綁定當前的URL到 index模塊的blog控制器 Route::bind('index/blog');//http://serverName/index/blog/read/id/5可以簡化成http://serverName/read/id/5 ;如果定義了路由Route::get('index/blog/:id','index/blog/read');那么訪問URL就變成了http://serverName/5 // 綁定當前的URL到 index模塊的blog控制器的read操作 Route::bind('index/blog/read'); // 綁定命名空間 Route::bind('\app\index\controller','namespace');//通過http://serverName/blog/read/id/5就可以直接訪問 \app\index\controller\Blog類的read方法。 // 綁定到類 Route::bind('\app\index\controller\Blog','class');//通過http://serverName/read/id/5就可以直接訪問 \app\index\controller\Blog類的read方法。 //入口文件綁定 // 復制一份入口文件添加define('BIND_MODULE','admin'); 命名為demo.php 我們訪問這個demo.php就訪問的是admin模塊 // 自動入口綁定 // 配置文件開啟入口文件自動綁定模塊 'auto_bind_module' => true, //復制一份index.php 改名為demo.php 訪問index.php是訪問index模塊 訪問的demo.php則訪問得是demo模塊 //域名路由 省略 ~~~ 定義路由后參數的獲取問題: ~~~ Route::get('hello/:id','index/test/hello'); 以下三種是獲取不到值的: dump(request()->get()); dump(request()->get('id')); dump(input('get.id')); 我們只有通過:下面這幾個才能獲取到 dump(request()->param()); dump(request()->param('id')); dump(request()->route()); dump(request()->route('id')); 怎么才能讓get獲取到? 參數綁定和?后面的如: public function hello($id){ dump(request()->get()); dump(input('get.')); } 瀏覽器輸入:域名/hello/10?name=123 array (size=2) 'name' => string '123' (length=3) 'id' => string '10' (length=2) ~~~ 路由之后這些方法的controller和action的的值與實際的不同了 ~~~ 11.// 調用Request對象的path方法 12.{$Request.path} 13.// 調用Request對象的module方法 14.{$Request.module} 15.// 調用Request對象的controller方法 16.{$Request.controller} 17.// 調用Request對象的action方法 18.{$Request.action} ~~~
                  <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>

                              哎呀哎呀视频在线观看