前言
有三种模式可以访问操作:PATH_INFO 、混合模式和强制模式 混合模式:PATH_INFO 和路由 强制模式:仅路由
代码中设置了自定义配置目录config,相关介绍可以看另一篇文章 thinkphp 5还在学习中,文章内容不能保证全都正确
Thinkphp 5.0完全开发手册
PHP-配置(Thinkphp 5)
一、环境
phpstudy
phpstorm
thinkphp 5.0.24
php 7.3.4nts
二、PATH_INFO
PATH_INFO 的访问格式有标准格式和兼容格式两种 需要访问的操作如下

[1]. 标准格式
不传参时 http或https://域名/入口文件/模块/控制器/操作

传参时,如果传入的变量不为操作所需要的变量名,或者是个数不对,将会报错 http或https://域名/入口文件/模块/控制器/操作/变量1/值1/变量2/值2
 
[2]. 兼容格式
先看一下thinkphp/convention.php 惯例配置文件,var_pathinfo 的默认参数是s 使用兼容格式访问操作时,需要传入这个参数s 这个参数可以直接在惯例配置文件修改,也可以在自定义的配置里修改

在application/config.php 修改设置 如果在application/config.php 中添加和惯例配置文件相同的参数选项时,将会覆盖掉原先惯例配置文件的参数选项值(相当于var_pathinfo 值由s 变成了pth )
<?php
return [
'var_pathinfo' => 'pth'
];
无传参,通过PATH_INFO 兼容格式访问一个操作时,就需要传入刚刚添加的pth http或https://域名/入口文件?pth=模块/控制器/操作/

有传参时,可以有两个传参的方式,前一个和标准格式一样 http或https://域名/入口文件?pth=模块/控制器/操作/变量1/值1/变量2/值2 或 http或https://域名/入口文件?pth=模块/控制器/操作&变量1=值1&变量2=值2
 
三、路由
需要访问的操作如下

[1]. 开启路由
兼容模式其实就是既可以通过PATH_INFO 也可以通过自定义的路由访问操作 PATH_INFO 访问操作的方式是默认的,如果需要使用自定义的路由进行访问时需要在config.php 添加如下配置
<?php
return [
"url_route_on" => true
];

[2]. 动态注册路由
创建一个route.php 文件,并在其中注册路由 注册路由又有两种方式,第一种是直接使用think 下的Route 类进行动态创建 Route 类的路径为thinkphp/library/think/Route.php

动态方法的格式如下 如果删除think\ ,就得在代码之前添加use think\Route
think\Route::rule('路由规则','路由地址','请求类型',[路由参数],[变量规则])
无传参
<?php
think\Route::rule("test1","test/Test/test1");

有传参,如果参数是必须传入的话可以使用/:变量名 如果参数是可选的,可以使用/[:变量名] 可选变量需要放在变量名列表的后面,否则会导致后面的变量无效
<?php
think\Route::rule("test2/:name/:age","test/Test/test2");

[3]. 配置注册路由
第二种是直接return 一个数组,并在数组中设置路由规则
return [
'路由规则' => '路由地址',
'路由规则' => ['路由地址',[路由参数],[变量规则]]
]
无传参
<?php
return [
"test1" => "test/Test/test1"
];

有传参
<?php
return [
"test2/:name/:age" => "test/Test/test2"
];

四、模式切换

[1]. PATH_INFO模式
默认是可以使用PATH_INFO模式的
<?php
return [
"url_route_must" => false
];
[2]. 兼容模式
既可以使用路由,也可以使用PATH_INFO
<?php
return [
"url_route_on" => true,
"url_route_must" => false
];
[3]. 强制模式
只能使用路由
<?php
return [
"url_route_on" => true,
"url_route_must" => true
];
|