定义路由必须利用
use think\facade\Route;
掌握器定义

<?phpnamespace 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 <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下的路由
<?phpuse think\facade\Route;// 当访问ming/34 的时候 路由到index掌握器下的index方法,并传入参数numer=34Route::rule('ming/:number', 'index/index');
此时访问 http://localhost:8082/admin/ming/34
已经开始路由
正常访问
没有路由
此时开启逼迫路由往后,首页须要开启路由
由于默认的运用为index 以是须要在route定义index
目录如下
定义首页目录
<?phpuse 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');
可以做到把一个变量传入其余一个路由中
路由地址路由到掌握器的操作
添加一个掌握器
此掌握器利用app\admin\controller 命名空间 其文件内容如下
<?phpnamespace 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重定向一个新的地址
路由到模板
利用路由到模板直接渲染
<?phpuse think\facade\Route;Route::view('blog', 'hello');
访问 http://localhost:8082/admin/blog/ 此时会渲染出
闭包支持
利用闭包可以利用一些分外需求的路由,不须要再次实行掌握器的操作了
<?phpuse think\facade\Route;Route::get('blog/:name', function ($name){ return $name;});
http://localhost:8082/admin/blog/34
闭包中可以实现依赖注入
<?phpuse think\facade\Route;Route::rule('blog/:name', function (\think\Request $request, $name){ $method = $request->method(); return $method . $name;});
此时由于依赖request会自动注入request
路由参数对当前的路由进行匹配。。
<?phpuse think\facade\Route;Route::rule('blog/:id', 'blog/read') ->ext('html') // url 后缀检测 ->https(); // https 检测
只有全部符合哀求才能匹配到
额外追加参数
利用append额外追加参数
<?phpuse 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秒
路由中间件
可以在路由中,数据直接传给中间件
路由分组可以对公有的路由进行分组操作
<?phpuse think\facade\Route;Route::group('blog', function (){ Route::rule(':id', 'blog/read'); Route::rule(':name', 'blog/read');})->ext('html')->pattern([ 'id' => '\d+', 'name' => '\w+']);
此时,可以根据正则匹配路由
资源路由<?phpnamespace app\admin\controller;class Blog{ public function index(){ } public function read($id){ return $id . \"大众read\"大众; } public function edit($id){ return $id . \公众edit\"大众; }}<?phpuse 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',
];
添加表明,实现路由
<?phpnamespace 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要求
路由规则
<?phpuse think\facade\Route;Route::rule('blog/:id', 'blog/read');<?phpnamespace app\admin\controller;class Blog{ public function index(){ } public function read($id){ var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming'])); return $id; }}