route 定義下的全部的路由文件都是有效的php
定義路由必須使用html
use think\facade\Route;
控制器定義ajax
<?php namespace app\admin\controller; class Index { public function Index($number){ echo $number; } }
修改配置文件,強制路由訪問 thinkphp
此時已經開啓多應用配置apache
目錄文件以下json
修改配置文件,啓用路由跨域
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | 應用設置 // +---------------------------------------------------------------------- use think\facade\Env; return [ // 應用地址 'app_host' => Env::get('app.host', ''), // 應用Trace(環境變量優先讀取) 'app_trace' => false, // 應用的命名空間 'app_namespace' => '', // 是否啓用路由 'with_route' => true, // 是否啓用事件 'with_event' => true, // 自動多應用模式 'auto_multi_app' => true, // 應用映射(自動多應用模式有效) 'app_map' => [], // 域名綁定(自動多應用模式有效) 'domain_bind' => [], // 禁止URL訪問的應用列表(自動多應用模式有效) 'deny_app_list' => [], // 默認應用 'default_app' => 'index', // 默認時區 'default_timezone' => 'Asia/Shanghai', // 默認驗證器 'default_validate' => '', // 異常頁面的模板文件 'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl', // 錯誤顯示信息,非調試模式有效 'error_message' => '頁面錯誤!請稍後再試~', // 顯示錯誤信息 'show_error_msg' => true, ];
再次修改配置文件,強制路由緩存
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | 應用設置 // +---------------------------------------------------------------------- return [ // PATHINFO變量名 用於兼容模式 'var_pathinfo' => 's', // 兼容PATH_INFO獲取 'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'], // pathinfo分隔符 'pathinfo_depr' => '/', // HTTPS代理標識 'https_agent_name' => '', // URL僞靜態後綴 'url_html_suffix' => 'html', // URL普通方式參數 用於自動生成 'url_common_param' => true, // 是否開啓路由延遲解析 'url_lazy_route' => false, // 是否強制使用路由 'url_route_must' => true, // 合併路由規則 'route_rule_merge' => false, // 路由是否徹底匹配 'route_complete_match' => false, // 使用註解路由 'route_annotation' => false, // 是否開啓路由緩存 'route_check_cache' => false, // 路由緩存鏈接參數 'route_cache_option' => [], // 路由緩存Key 'route_check_cache_key' => '', // 訪問控制器層名稱 'controller_layer' => 'controller', // 空控制器名 'empty_controller' => 'Error', // 是否使用控制器後綴 'controller_suffix' => false, // 默認的路由變量規則 'default_route_pattern' => '[\w\.]+', // 域名根,如thinkphp.cn 'url_domain_root' => '', // 是否自動轉換URL中的控制器和操做名 'url_convert' => true, // 表單請求類型假裝變量 'var_method' => '_method', // 表單ajax假裝變量 'var_ajax' => '_ajax', // 表單pjax假裝變量 'var_pjax' => '_pjax', // 是否開啓請求緩存 true自動緩存 支持設置請求緩存規則 'request_cache' => false, // 請求緩存有效期 'request_cache_expire' => null, // 全局請求緩存排除規則 'request_cache_except' => [], // 默認控制器名 'default_controller' => 'Index', // 默認操做名 'default_action' => 'index', // 操做方法後綴 'action_suffix' => '', // 默認JSONP格式返回的處理方法 'default_jsonp_handler' => 'jsonpReturn', // 默認JSONP處理方法 'var_jsonp_handler' => 'callback', ];
再次定義admin下的路由閉包
<?php use think\facade\Route; // 當訪問ming/34 的時候 路由到index控制器下的index方法,並傳入參數numer=34 Route::rule('ming/:number', 'index/index');
此時訪問 http://localhost:8082/admin/ming/34app
已經開始路由
正常訪問
沒有路由
此時開啓強制路由之後,首頁須要開啓路由
因爲默認的應用爲index 因此須要在route定義index
目錄以下
定義首頁目錄
<?php use think\facade\Route; Route::rule('/', 'index/index');
此時訪問首頁
http://localhost:8082/
會被重定向到 index控制器下的index方法
變量規則,這裏定義的是
Route::get('new/:name', 'News/read') ->pattern(['name' => '[\w|\-]+']);
此時匹配的是name變量的匹配的規則,匹配的規則是雙斜槓
// 定義動態路由 Route::get('hello/:name', 'index/:name/hello');
能夠作到把一個變量傳入另一個路由中
路由到控制器的操做
添加一個控制器
此控制器使用appadmincontroller 命名空間 其文件內容以下
<?php namespace app\admin\controller; class Blog { public function read($id){ return $id; } }
傳入$id做爲參數
再次定義路由規則以下
Route::get('blog/:id', 'Blog/read');
此時訪問admin模塊下的blog內容,會匹配:id的內容,
http://localhost:8082/admin/blog/23/ 此時會匹配23內容
其結果以下
路由到控制器和操做
上面的例子就是
這種方式能夠執行任何方法
Route::get('blog/:id','\app\index\service\Blog@read');
Route::get('blog/:id','\app\index\service\Blog::read');
上方執行的是Blog的read方法或者read的靜態方法
Route::redirect('blog/:id', 'http://blog.thinkphp.cn/read/:id', 302);
使用302重定向一個新的地址
使用路由到模板直接渲染
<?php use think\facade\Route; Route::view('blog', 'hello');
訪問 http://localhost:8082/admin/blog/ 此時會渲染出
使用閉包能夠使用一些特殊需求的路由,不須要再次執行控制器的操做了
<?php use think\facade\Route; Route::get('blog/:name', function ($name){ return $name; });
http://localhost:8082/admin/blog/34
閉包中能夠實現依賴注入
<?php use think\facade\Route; Route::rule('blog/:name', function (\think\Request $request, $name){ $method = $request->method(); return $method . $name; });
此時因爲依賴request會自動注入request
對當前的路由進行匹配。。
<?php use think\facade\Route; Route::rule('blog/:id', 'blog/read') ->ext('html') // url 後綴檢測 ->https(); // https 檢測
只有所有符合要求才能匹配到
使用append額外追加參數
<?php use think\facade\Route; Route::rule('blog/:id', 'blog/read') ->append( ['app_id' => 1, 'status' => 1] );
此時會傳入兩個參數 app_id 和 status 兩個參數
支持綁定模型
Route::get('hello/:id', 'index/hello') ->model('\app\index\model\User');
支持從模型層中直接獲取數據
同時能夠使用閉包,獲取數據
Route::rule('hello/:id', 'index/hello') ->model(function ($id) { $model = new \app\index\model\User; return $model->where('id', $id)->find(); });
Route::get('new/:name$', 'News/read') ->cache(3600);
表示直接請求3600秒
能夠在路由中,數據直接傳給中間件
能夠對公有的路由進行分組操做
<?php use think\facade\Route; Route::group('blog', function (){ Route::rule(':id', 'blog/read'); Route::rule(':name', 'blog/read'); })->ext('html')->pattern([ 'id' => '\d+', 'name' => '\w+' ]);
此時,能夠根據正則匹配路由
<?php namespace app\admin\controller; class Blog { public function index(){ } public function read($id){ return $id . "read"; } public function edit($id){ return $id . "edit"; } }
<?php use think\facade\Route; Route::resource('blog', 'Blog');
此時訪問
http://localhost:8082/admin/blog/34/edit 會調用edit方法
http://localhost:8082/admin/blog/34/read 會調用read方法
路由支持資源嵌套
修改配置文件,實現註解路由
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | 應用設置 // +---------------------------------------------------------------------- return [ // PATHINFO變量名 用於兼容模式 'var_pathinfo' => 's', // 兼容PATH_INFO獲取 'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'], // pathinfo分隔符 'pathinfo_depr' => '/', // HTTPS代理標識 'https_agent_name' => '', // URL僞靜態後綴 'url_html_suffix' => 'html', // URL普通方式參數 用於自動生成 'url_common_param' => true, // 是否開啓路由延遲解析 'url_lazy_route' => false, // 是否強制使用路由 'url_route_must' => true, // 合併路由規則 'route_rule_merge' => false, // 路由是否徹底匹配 'route_complete_match' => false, // 使用註解路由 'route_annotation' => true, // 是否開啓路由緩存 'route_check_cache' => false, // 路由緩存鏈接參數 'route_cache_option' => [], // 路由緩存Key 'route_check_cache_key' => '', // 訪問控制器層名稱 'controller_layer' => 'controller', // 空控制器名 'empty_controller' => 'Error', // 是否使用控制器後綴 'controller_suffix' => false, // 默認的路由變量規則 'default_route_pattern' => '[\w\.]+', // 域名根,如thinkphp.cn 'url_domain_root' => '', // 是否自動轉換URL中的控制器和操做名 'url_convert' => true, // 表單請求類型假裝變量 'var_method' => '_method', // 表單ajax假裝變量 'var_ajax' => '_ajax', // 表單pjax假裝變量 'var_pjax' => '_pjax', // 是否開啓請求緩存 true自動緩存 支持設置請求緩存規則 'request_cache' => false, // 請求緩存有效期 'request_cache_expire' => null, // 全局請求緩存排除規則 'request_cache_except' => [], // 默認控制器名 'default_controller' => 'Index', // 默認操做名 'default_action' => 'index', // 操做方法後綴 'action_suffix' => '', // 默認JSONP格式返回的處理方法 'default_jsonp_handler' => 'jsonpReturn', // 默認JSONP處理方法 'var_jsonp_handler' => 'callback', ];
添加註解,實現路由
<?php namespace app\controller; /** * @route('blog') */ class Blog { public function index() { } public function read($id) { } public function edit($id) { } }
支持綁定到控制器操做,命名空間,和類
// 綁定當前的URL到 Blog控制器 Route::bind('blog'); // 綁定當前的URL到 Blog控制器的read操做 Route::bind('blog/read');
原先訪問 http://serverName/blog/read/id/5
須要訪問 http://serverName/read/id/5 能夠訪問到
剩下的還能夠綁定到命名空間 類
使用 Route::domain 綁定子域
過
MISS路由爲全局最後一條執行的路由
經過allowCrossDomain 進行跨域請求
用於生成url請求
路由規則
<?php use think\facade\Route; Route::rule('blog/:id', 'blog/read');
<?php namespace app\admin\controller; class Blog { public function index(){ } public function read($id){ var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming'])); return $id; } }