FastAdmin 基于 ThinkPHP5,默认关闭路由,且前台页面 URL 会自动带有 index 前缀(如 域名/index/news),这不仅影响美观,还不利于 SEO 优化。
本文将手把手教你彻底移除 index 前缀、完全开启 FastAdmin 路由,并详解路由规则配置技巧与顺序优先级,助你实现简洁优雅的项目 URL!
一、核心前提:修改行为扩展,移除 index 强制关闭路由逻辑
FastAdmin 默认通过 appDispatch 行为扩展,强制关闭以 index、api 开头的 URL 路由检测。因此,第一步必须修改这段代码。
1. 找到目标文件
- 文件路径:application/common/behavior/Common.php
2. 修改代码(去掉 index)
原代码会判断 index 和 api 前缀并关闭路由。只需删除 index 判断,保留 api 即可(API 接口一般无需路由美化)。
修改前代码
php
namespace app\common\behavior;
use think\Config;
class Common
{
public function appDispatch(&$dispatch)
{
$pathinfoArr = explode('/', request()->pathinfo());
if (!Config::get('url_domain_deploy') && $pathinfoArr && \in_array($pathinfoArr[0], ['index', 'api'])) {
// 如果是以index或api开始的URL则关闭路由检测
\think\App::route(false);
}
}
}
修改后代码
php
namespace app\common\behavior;
use think\Config;
class Common
{
public function appDispatch(&$dispatch)
{
$pathinfoArr = explode('/', request()->pathinfo());
// 重点:去掉数组中的 'index',仅保留 api
if (!Config::get('url_domain_deploy') && $pathinfoArr && \in_array($pathinfoArr[0], ['api'])) {
// 仅 api 前缀关闭路由,index 路由正常开启
\think\App::route(false);
}
}
}
二、配置文件开启路由总开关
修改行为扩展后,需要在核心配置中开启路由功能。
1. 找到配置文件
- 文件路径:application/config.php
2. 开启路由配置
搜索 url_route_on 配置项,将其值设为 true:
php
// 是否开启路由
'url_route_on' => true,
// 建议同时开启路由完整匹配(可选)
'url_route_must' => false,
- url_route_on:路由总开关,必须设为 true
- url_route_must:设为 true 时,所有 URL 必须匹配路由规则才能访问。新手建议先设为 false
三、路由规则配置(实用写法 + 示例)
- 路由配置文件路径:application/route.php
- 所有自定义路由规则都写在这里。
1. 路由基础语法
php
'路由地址' => '模块/控制器/操作',
结合 FastAdmin 前台(index 模块),常用写法:
php
'自定义路由' => 'index/控制器/方法',
2. 实战路由示例(直接可用)
php
// 1. 静态路由(无参数,最简洁)
'news' => 'index/news/index', // 访问 域名/news → index/news/index
'about' => 'index/about/index', // 访问 域名/about → index/about/index
// 2. 动态路由(带参数,必学)
'news/:cateid' => 'index/news/index', // 访问 域名/news/8 → index/news/index/cateid/8
'<type>-details/<id>' => 'index/article/detail',
// 访问 域名/product-details/10 → index/article/detail/type/product/id/10
// 3. 带额外参数的路由
'news-cid8' => 'index/news/index?cateid=8', // 固定参数路由
3. 路由配置顺序(优先级)
ThinkPHP 路由从上到下匹配,匹配到即停止。顺序错误会导致路由失效!
- 正确顺序:动态路由在前,静态路由在后
php
// 正确:动态路由优先匹配
'news/:cateid' => 'index/news/index',
'news' => 'index/news/index',
- 错误顺序(会导致动态路由失效):
php
// 错误:静态路由先匹配,动态路由永远不会生效
'news' => 'index/news/index',
'news/:cateid' => 'index/news/index',
四、配置完成后的效果对比
|------------------------------------------|---------------------|-----------------------------------------|
| 原 URL(丑陋) | 新 URL(优雅) | 实际访问地址 |
| /index/news | /news | index/news/index |
| /index/news/index/cateid/8 | /news/8 | index/news/index/cateid/8 |
| /index/article/detail/type/product/id/10 | /product-details/10 | index/article/detail/type/product/id/10 |
五、常见问题排查
- 路由不生效:检查是否已修改 Common.php 中的 index 判断,必须删除 index 字段
- 404 错误:检查路由规则拼写、控制器/方法名称是否正确
- 动态路由失效:检查路由顺序,动态路由必须放在静态路由上方
- 缓存问题:修改配置后,删除 runtime 目录缓存,重新访问
总结
FastAdmin 开启路由只需两步核心操作:
- 修改 application/common/behavior/Common.php,去掉 index 判断
- 修改 application/config.php,开启 'url_route_on' = true
配合正确的路由规则和顺序,就能彻底告别 URL 中的 index 前缀,实现极简、优雅的路由访问,大幅提升项目的美观度和 SEO 效果。