ThinkPHP6 两步解决去除多应用目录设置伪静态
时间: 2022-12-23 19:41:12
怎么才能让ThinkPHP6 URL访问的方式更美观,两步解决URL访问方式
例子: 原链接 http://www.ex60.com/index.php/home/Goods/index.html?goods_id=1
配置后链接 http://www.ex60.com/product-1.html
一、配置Nginx文件
//为了把访问路径带了index.php 和 home应用目录去掉,并且不影响原有访问方式
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/admin/(.*)$ /index.php?s=/admin/$1; //后台应用
rewrite ^((?!/home).*)$ /index.php?s=/home/$1; ##隐藏默认模块名 /home
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
#autoindex on;
}
二、在home应用目录文件夹下添加route/app.php,文件内容如下:
//设置home/goods/index 控制器url访问方式为 product,pattern定义参数
use think\facade\Route;
//说明 :cate_id 表示只能带/参数 <b_id?>表示可选参数
//-?表示可选符号 -<cate_id> 表示可带符号的参数
//【:cate_id】表示只能带/的可选参数
Route::get('product-<goods_id>', 'home/goods/index','GET')->pattern(['goods_id' => '\d+']);
Route::get('category-<cate_id>', 'home/search/index','GET')->pattern(['cate_id' => '\d+']);