路由定義檔案
route 定義下的所有的路由檔案都是有效的
定義路由必須使用
use think\facade\Route;
控制器定義
<?php
namespace app\admin\controller;
class Index
{
public function Index($number){
echo $number;
}
}
修改配置檔案,強制路由通路
此時已經開啟多應用配置
目錄檔案如下
修改配置檔案,啟用路由
<?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 <[email protected]>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 應用設定
// +----------------------------------------------------------------------
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 <[email protected]>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 應用設定
// +----------------------------------------------------------------------
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/34已經開始路由
正常通路
沒有路由
此時開啟強制路由以後,首頁需要開啟路由
由于預設的應用為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 <[email protected]>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 應用設定
// +----------------------------------------------------------------------
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 路由
MISS路由為全局最後一條執行的路由
跨域請求
通過allowCrossDomain 進行跨域請求
URL請求
用于生成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;
}
}