当前位置: 首页 > 知识库问答 >
问题:

PHP致命错误:未捕获错误:在控制器的负载模型中找不到类

太叔睿
2023-03-14

我有这个MVC文件夹结构:

application
-----------catalog
------------------controller
----------------------------ProfileContoller.php
------------------model
----------------------------UserModel.php
------------------view
------------------language
-----------admin
------------------controller
------------------model
------------------view
------------------language
core
---- Controller.php
public
vendor
....

现在,在ProfileController.php中,我有:

namespace Application\Catalog\Controller;

use Application\Core\Controller as Controller;

use Application\Core\Model\UserModel;

class ProfileController extends Controller
{
    /**
     * Construct this object by extending the basic Controller class
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * This method controls what happens when you move to /overview/index in your app.
     * Shows a list of all users.
     */
    public function index()
    {
        $this->View->render('profile/index','', array(
            'users' => UserModel::getPublicProfilesOfAllUsers())
        );
    }

}

在usermodel.php中,我有:

namespace Application\Catalog\Model;

class UserModel
{

    public static function getPublicProfilesOfAllUsers()
    {
        $database = DatabaseFactory::getFactory()->getConnection();

        $sql = "SELECT user_id, user_name, user_email, user_active, user_has_avatar, user_deleted FROM users";
        $query = $database->prepare($sql);
        $query->execute();

        $all_users_profiles = array();

        foreach ($query->fetchAll() as $user) {

            array_walk_recursive($user, 'Filter::XSSFilter');

            $all_users_profiles[$user->user_id] = new stdClass();
            $all_users_profiles[$user->user_id]->user_id = $user->user_id;
            $all_users_profiles[$user->user_id]->user_name = $user->user_name;
            $all_users_profiles[$user->user_id]->user_email = $user->user_email;
            $all_users_profiles[$user->user_id]->user_active = $user->user_active;
            $all_users_profiles[$user->user_id]->user_deleted = $user->user_deleted;
            $all_users_profiles[$user->user_id]->user_avatar_link = (Config::get('USE_GRAVATAR') ? AvatarModel::getGravatarLinkByEmail($user->user_email) : AvatarModel::getPublicAvatarFilePathOfUser($user->user_has_avatar, $user->user_id));
        }

        return $all_users_profiles;
    }

并使用Composer Psr4 I自动加载文件:

"autoload": {
        "psr-4": { "Application\\": "application/","Application\\Core\\": "application/core/","Application\\Catalog\\Model\\": "application/catalog/model/"}
    }
    }

现在,当我需要路由我的URL时,我会采取行动:

$route->get('/cms/profile/index/', 'Application\Catalog\Controller\ProfileController@index');

我看到这个错误:

>致命错误:未捕获错误:在C:\XAMPP\HTDOCS\CMS\Application\Catalog\Controller\ProfileController.php:25堆栈跟踪:#0[内部函数]:Application\Catalog\Controller\ProfileController->索引()#1 C:\XAMPP\HTDOCS\CMS\Vendor\Router\Route\System\Route.php(649):call_user_func_array(数组,数组)#2

共有1个答案

那昊
2023-03-14

在这里,您调用的函数的类名为model class在控制器中不存在。UserModel::GetPublicProfileSofAllUsers()

使用Application\Controller\Model\UserModel作为

use Application\Controller\Model\UserModel;

class ProfileController extends Controller
{

    protected $userModel;
    /**
     * Construct this object by extending the basic Controller class
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('UserModel');
    }

您可以调用为

$this->View->render('profile/index','', array(
            'users' => $this->UserModel->getPublicProfilesOfAllUsers())
        );

最好的方法是通过对象调用非静态函数。在PHP5.3中,我们可以将非静态函数作为静态函数,但在PHP7+中,它被标记为不推荐使用,并将在未来删除它。

 类似资料:
  • 我收到了以下错误消息: 致命错误:未捕获错误:在F:\projects\websites\main\u website\app\Exceptions\Handler中找不到类“Auth”。php:65堆栈跟踪:#0 F:\projects\websites\main\u website\vendor\laravel\framework\src\illighted\Foundation\Http\K

  • 我现在必须学习通过fire base编写移动应用程序web服务。我点击了这个链接:https://firebase-php.readthedocs.io/en/stable/ 在我的核心网站中,我创建web服务文件夹,然后创建我的fire。php文件。这个文件代码在这里, 我得打电话给我的支持档案:https://github.com/kreait/firebase-php/ 但我还是得到了一个:

  • 我目前在一个项目上工作,它显示一个配方,配料在一个公式中检查。我快到最后了,但我真的不明白我的错误是什么。如果有人能向我解释: 致命错误:未捕获的PDOException:SQLState[42S22]:找不到列:1054/homepages/30/d675437312/htdocs/assets/recettemalin/recettemalin/recettemelectionnee.php:

  • 我得到这个错误。我创建了一个按钮来更新表。当我点击按钮时,我得到一个错误。如何修复它? 致命错误:Uncaught ArgumentCounter错误:函数personel::update_form(),0的参数太少,在C:\xampp\htdocs\warehouse\panel\system\core\CodeIgniter中传递。php在第360行,C:\xampp\htdocs\wareh

  • 尝试在我的stripe帐户上提交测试付款时,我遇到以下错误: 致命错误:未捕获错误:在/home/dh#u y3rvc7/vvnow.dreamhosters.com/wp-content/plugins/wp-Stripe-integration/includes/process payment.php:25堆栈跟踪:#0/home/dh#uy3rvc7/vvnow.dreamhosters.c

  • 我的代码怎么了 帕西恩视图- 控制器- 注意:未定义的索引:psn在C:\xampp\htdocs\hamil\app\view\pasien\index.php行202 警告:在第202行的C:\xampp\htdocs\bunil\app\view\pasien\index.php中为Foreach()提供的参数无效 致命错误:未捕获错误:调用C:\xampp\htdocs\bumil\app