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

服务器端出现App\Controllers\Personalbudget控制器未找到错误的解决求助

问题分析与解决

问题现象

访问链接 my-site/personalbudget/addexpense 时触发异常:

Uncaught exception: 'Exception',Message: 'Controller class App\Controllers\Personalbudget not found'
堆栈信息指向Core\Router类的dispatch方法。该问题仅在服务器环境出现,本地环境运行正常。

相关代码

router.php

<?php

namespace Core;

class Router
{

public function dispatch($url)
    {
        $url = $this->removeQueryStringVariables($url);

        if ($this->match($url)) {
            $controller = $this->params['controller'];
            $controller = $this->convertToStudlyCaps($controller);
            $controller = $this->getNamespace() . $controller;

            if (class_exists($controller)) {
                $controller_object = new $controller($this->params);

                $action = $this->params['action'];
                $action = $this->convertToCamelCase($action);

                if (is_callable([$controller_object, $action])) {
                    $controller_object->$action();

                } else {
                    throw new \Exception("Method $action (in controller $controller) not found");
                }
            } else {
                throw new \Exception("Controller class $controller not found");
            }
        } else {
            throw new \Exception('No route matched.', 404);
        }
    }
}

personalBudget.php

<?php

namespace App\Controllers;

use \Core\View;
use \App\Models\ModelPersonalBudget;
use \App\Models\User;

use \App\Auth;
use \App\Flash;

#[\AllowDynamicProperties]
class personalBudget extends \Core\Controller

问题根源

本地环境(多为Windows系统)文件系统对大小写不敏感,服务器环境(多为Linux系统)对大小写严格敏感:

  • 路由的convertToStudlyCaps方法将URL中的personalbudget转换为Personalbudget(首字母大写,其余字母小写)
  • 实际控制器类名为personalBudget(首字母小写,第二个单词首字母大写),文件名也是personalBudget.php
  • 服务器上class_exists检查时,App\Controllers\Personalbudget与实际类名大小写不匹配,导致类查找失败

解决方案

  1. 统一控制器类名与文件名的大小写
    将控制器类名修改为符合StudlyCaps命名规范的PersonalBudget,同时将文件名改为PersonalBudget.php:
    #[\AllowDynamicProperties]
    class PersonalBudget extends \Core\Controller
    
  2. 校验路由转换逻辑
    检查convertToStudlyCaps方法的转换规则,确保URL中的personalbudget转换后得到PersonalBudget(每个单词首字母大写),与修改后的类名完全匹配。若原转换逻辑无法实现,需调整拆分和大写规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 18:25:00