You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Leaf PHP中DsosController路由配置后出现404错误,请求帮助

解决Leaf PHP中DsosController路由404的问题

可能的问题及修复方案

1. 路由路径大小写不匹配

你配置的路由是/dsos(全小写),但访问地址是/Dsos(首字母大写)。Leaf PHP路由默认大小写敏感,两种路径会被识别为不同地址。

  • 修复方案:
    • 修改路由为大写开头:app()->get('/Dsos', 'DsosController@index');
    • 或者统一访问路径为全小写:https://myserver/dsos

2. 控制器命名空间缺失

Leaf PHP生成的控制器默认带有命名空间(如App\Controllers\DsosController),仅写DsosController@index会导致框架无法定位类。

  • 修复方案:
    指定完整命名空间:
    app()->get('/dsos', 'App\Controllers\DsosController@index');
    
    或通过use导入后使用类名:
    use App\Controllers\DsosController;
    
    app()->get('/dsos', [DsosController::class, 'index']);
    

3. 路由文件未被加载

检查项目入口文件(如根目录index.php),确保包含加载路由的代码:

require __DIR__ . '/app/routes/_app.php';

若缺失,手动添加该行以加载你的路由配置。

4. Web服务器重写规则未配置

Leaf依赖URL重写将请求转发到入口文件,未配置会导致非入口路径返回404。

  • Apache:确保根目录有.htaccess文件,内容如下:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  • Nginx:在站点配置中添加规则:
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    

5. 路由缓存未清除

若启用过路由缓存,修改路由后需执行命令清除缓存:

php leaf cache:clear

内容的提问来源于stack exchange,提问作者GLDavid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:13:13