thinkphp 路由

路由定義文件

route 定義下的所有的路由文件都是有效的

定義路由必須使用

use think\\facade\\Route;
thinkphp 路由

控制器定義

namespace app\\admin\\controller;
class Index
{
public function Index($number){
echo $number;
}
}

修改配置文件,強制路由訪問

此時已經開啟多應用配置

目錄文件如下

thinkphp 路由

修改配置文件,啟用路由

// +----------------------------------------------------------------------
// | 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>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 應用設置
// +----------------------------------------------------------------------
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,
];
/<liu21st>

再次修改配置文件,強制路由

// +----------------------------------------------------------------------

// | 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>

// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

// | 應用設置

// +----------------------------------------------------------------------

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下的路由

use think\\facade\\Route;
// 當訪問ming/34 的時候 路由到index控制器下的index方法,並傳入參數numer=34
Route::rule('ming/:number', 'index/index');

此時訪問 http://localhost:8082/admin/ming/34

已經開始路由

正常訪問

thinkphp 路由

沒有路由

此時開啟強制路由以後,首頁需要開啟路由

由於默認的應用為index 所以需要在route定義index

目錄如下

thinkphp 路由

定義首頁目錄

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');

可以做到把一個變量傳入另外一個路由中

路由地址

路由到控制器的操作

添加一個控制器

thinkphp 路由

此控制器使用app\\admin\\controller 命名空間 其文件內容如下

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內容

其結果如下

thinkphp 路由

路由地址

路由到控制器操作

路由到控制器和操作

上面的例子就是

路由到類的方法

這種方式可以執行任何方法

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重定向一個新的地址

路由到模板

使用路由到模板直接渲染

use think\\facade\\Route;
Route::view('blog', 'hello');

訪問 http://localhost:8082/admin/blog/ 此時會渲染出

thinkphp 路由

閉包支持

使用閉包可以使用一些特殊需求的路由,不需要再次執行控制器的操作了

use think\\facade\\Route;
Route::get('blog/:name', function ($name){
return $name;
});

http://localhost:8082/admin/blog/34

thinkphp 路由

閉包中可以實現依賴注入

use think\\facade\\Route;
Route::rule('blog/:name', function (\\think\\Request $request, $name){
$method = $request->method();
return $method . $name;
});

此時由於依賴request會自動注入request

路由參數

對當前的路由進行匹配。。

use think\\facade\\Route;
Route::rule('blog/:id', 'blog/read')
->ext('html') // url 後綴檢測
->https(); // https 檢測

只有全部符合要求才能匹配到

額外追加參數

使用append額外追加參數

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秒

路由中間件

可以在路由中,數據直接傳給中間件

路由分組

可以對公有的路由進行分組操作

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+'
]);

此時,可以根據正則匹配路由

資源路由

namespace app\\admin\\controller;
class Blog
{
public function index(){
}
public function read($id){
return $id . "read";
}
public function edit($id){
return $id . "edit";
}
}
use think\\facade\\Route;
Route::resource('blog', 'Blog');

此時訪問

http://localhost:8082/admin/blog/34/edit 會調用edit方法

http://localhost:8082/admin/blog/34/read 會調用read方法

資源嵌套

路由支持資源嵌套

註解路由

修改配置文件,實現註解路由

// +----------------------------------------------------------------------

// | 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>

// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

// | 應用設置

// +----------------------------------------------------------------------

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',

];

添加註解,實現路由

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請求

路由規則

use think\\facade\\Route;
Route::rule('blog/:id', 'blog/read');
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;
}
}


分享到:


相關文章: