能直接用PhpStorm内置服务器跑ThinkPHP,但默认会404——根本原因是入口路径和路由脚本未正确配置:Document root须设为项目根目录(含public),Router script必须填public/index.php,且需标记vendor为Sources Root并添加@var类型注释。

能直接用 PhpStorm 内置服务器跑 ThinkPHP,但默认会 404 或跳转失败——根本原因不是 PHP 版本或 Xdebug,而是入口路径和路由脚本没对上。
为什么 Open in Browser 点开就 404
PhpStorm 的 Open in Browser 右键菜单默认把当前文件当入口,比如点 index.php 就只加载它,不走框架的自动路由机制。ThinkPHP(尤其 6/8)要求所有请求都经由 public/index.php 统一调度,否则 app()、request、中间件全失效。
- 别在
app/或config/目录下右键 PHP 文件选Open in Browser - 必须确保当前打开/选中的文件是
public/index.php,再右键执行 - 如果项目结构是根目录直出
index.php(非标准 TP 结构),需手动指定Router script为index.php,否则静态资源和子路由 404
PHP Built-in Web Server 配置要点
这才是稳定启动 ThinkPHP 的方式,绕过 Apache/Nginx 依赖,且支持断点调试。
-
Document root必须设为项目根目录(含public/的那个目录),不是public/本身 -
Router script填public/index.php(TP 6/8 要求),路径相对于Document root -
Port建议避开 80/443/8080,用 8001、8002 等避免冲突 - 启动后访问地址是
http://localhost:8001/,不是http://localhost:8001/public/
vendor 类不识别?先标 Sources Root 再补 @var
自动补全失效、Undefined class \think\App 报红,不是 Composer 没装好,是 PhpStorm 根本没扫描 vendor 里的类。
立即学习“PHP免费学习笔记(深入)”;
- 右键项目下的
vendor目录 →Mark Directory as→Sources Root - 不要只标
vendor/topthink/thinkphp,TP 8 的核心包已拆成topthink/framework、topthink/think-orm等多个独立包 - 控制器里写
$this->request前,加一行/** @var \think\Request $request */,否则 PhpStorm 不知道类型 - 索引滞后?按
Ctrl + Shift + O(Win/Linux)或Cmd + Shift + O(macOS)强制刷新
Xdebug 断点进不了 think\Route::dispatch()
入口文件能断,但进不了框架内部方法,说明 Xdebug 没覆盖到自动加载的类——常见于没启用 xdebug.start_with_request=yes 或监听配置错位。
- 确认
php.ini有xdebug.mode=debug且xdebug.start_with_request=yes - PhpStorm 的
Settings → PHP → Servers中,Host和端口必须与内置服务器一致(如localhost:8001) - 别在
app/controller里随便打点,优先在public/index.php第一行、think\Route::dispatch()、think\Pipeline::then()这些调度枢纽处设断点 - Chrome 装
Xdebug Helper插件并点 “Debug” 按钮,否则请求不会带XDEBUG_SESSION_START参数
内置服务器跑 TP 最容易卡在 router 脚本路径不对或 vendor 没标记,这两步漏掉,后面怎么调 Xdebug 都白搭。



















