页面错误!请稍后再试~ thinkphp 5_thinkphp 路由

路由定义文件

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

定义路由必须使用

use thinkfacadeRoute;
02dfd421078dffbd72fcca8605bed692.png

控制器定义

<?phpnamespace appadmincontroller;class Index{ public function Index($number){ echo $number; }}

修改配置文件,强制路由访问

此时已经开启多应用配置

目录文件如下

0be083189fe769824c9c7351c02162ae.png

修改配置文件,启用路由

<?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 // +----------------------------------------------------------------------// +----------------------------------------------------------------------// | 应用设置// +----------------------------------------------------------------------use thinkfacadeEnv;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,];

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

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

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

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

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

// | 应用设置

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

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 thinkfacadeRoute;// 当访问ming/34 的时候 路由到index控制器下的index方法,并传入参数numer=34Route::rule('ming/:number', 'index/index');

此时访问 http://localhost:8082/admin/ming/34

已经开始路由

正常访问

ed15cd2b31f2b6e7bcda2b3674b4844b.png

没有路由

此时开启强制路由以后,首页需要开启路由

由于默认的应用为index 所以需要在route定义index

目录如下

a86787783c1bd735fcdce390b5010c2e.png

定义首页目录

<?phpuse thinkfacadeRoute;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');

可以做到把一个变量传入另外一个路由中

路由地址

路由到控制器的操作

添加一个控制器

b9d9d0b23a545a1dd5d6c88cf2ee1a6b.png

此控制器使用appadmincontroller 命名空间 其文件内容如下

<?phpnamespace appadmincontroller;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内容

其结果如下

def341cb462c94079fe120abb148243b.png

路由地址

路由到控制器操作

路由到控制器和操作

上面的例子就是

路由到类的方法

这种方式可以执行任何方法

Route::get('blog/:id','appindexserviceBlog@read');Route::get('blog/:id','appindexserviceBlog::read');

上方执行的是Blog的read方法或者read的静态方法

重定向路由

Route::redirect('blog/:id', 'http://blog.thinkphp.cn/read/:id', 302);

使用302重定向一个新的地址

路由到模板

使用路由到模板直接渲染

<?phpuse thinkfacadeRoute;Route::view('blog', 'hello');

访问 http://localhost:8082/admin/blog/ 此时会渲染出

0db780be9a40b88b802053e48764d2a4.png

闭包支持

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

<?phpuse thinkfacadeRoute;Route::get('blog/:name', function ($name){ return $name;});

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

cb753730f14def58ff5f446759416f1f.png

闭包中可以实现依赖注入

<?phpuse thinkfacadeRoute;Route::rule('blog/:name', function (hinkRequest $request, $name){ $method = $request->method(); return $method . $name;});

此时由于依赖request会自动注入request

路由参数

对当前的路由进行匹配。。

<?phpuse thinkfacadeRoute;Route::rule('blog/:id', 'blog/read') ->ext('html') // url 后缀检测 ->https(); // https 检测

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

额外追加参数

使用append额外追加参数

<?phpuse thinkfacadeRoute;Route::rule('blog/:id', 'blog/read')->append( ['app_id' => 1, 'status' => 1]);

此时会传入两个参数 app_id 和 status 两个参数

绑定模型

支持绑定模型

Route::get('hello/:id', 'index/hello') ->model('appindexmodelUser');

支持从模型层中直接获取数据

同时可以使用闭包,获取数据

Route::rule('hello/:id', 'index/hello') ->model(function ($id) { $model = new appindexmodelUser; return $model->where('id', $id)->find(); });

请求缓存

Route::get('new/:name$', 'News/read') ->cache(3600);

表示直接请求3600秒

路由中间件

可以在路由中,数据直接传给中间件

路由分组

可以对公有的路由进行分组操作

<?phpuse thinkfacadeRoute;Route::group('blog', function (){ Route::rule(':id', 'blog/read'); Route::rule(':name', 'blog/read');})->ext('html')->pattern([ 'id' => 'd+', 'name' => 'w+']);

此时,可以根据正则匹配路由

资源路由

<?phpnamespace appadmincontroller;class Blog{ public function index(){ } public function read($id){ return $id . "read"; } public function edit($id){ return $id . "edit"; }}<?phpuse thinkfacadeRoute;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

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

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

// | 应用设置

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

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 appcontroller;/** * @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 thinkfacadeRoute;Route::rule('blog/:id', 'blog/read');<?phpnamespace appadmincontroller;class Blog{ public function index(){ } public function read($id){ var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming'])); return $id; }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值