首页 文章

PHP致命错误:未捕获错误:在Controller中的加载模型中找不到类

提问于
浏览
0

我有这个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我使用以下自动加载文件:

"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中找不到类'Application \ Catalog \ Controller \ UserModel':25堆栈跟踪:#0 [内部函数] :Application \ Catalog \ Controller \ ProfileController-> index()#1 C:\ xampp \ htdocs \ cms \ vendor \ router \ route \ system \ Route.php(649):call_user_func_array(Array,Array)#2 C:\ xampp \ htdocs \ cms \ vendor \ router \ route \ system \ Route.php(599):System \ Route-> callback(Array,Array)#3 C:\ xampp \ htdocs \ cms \ public \ index.php(150) ):在第25行的C:\ xampp \ htdocs \ cms \ application \ catalog \ controller \ ProfileController.php中抛出System \ Route-> end()#4

1 回答

  • 1

    在这里,您使用类名称调用函数,模型类不存在于控制器中 . UserModel::getPublicProfilesOfAllUsers()

    使用Application \ Controller \ Model \ UserModel作为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())
            );
    

    最好的方法是通过对象调用非静态函数 . 在PHP 5.3中,我们可以将非静态函数作为静态函数,但在PHP 7中将其标记为已弃用,并将在以后将其删除 .

相关问题