简体   繁体   English

PSR-4 自动加载选项没有在我的项目中加载类?

[英]PSR-4 Autoloading option is not loading classes in my project?

When I try to load my classes using psr-4 autoloading option, I get this error 'Fatal error: Uncaught Error: Class 'HomeController' not found in C:\xampp\htdocs\gacho\App\Core\Application.php:17 Stack trace: #0 C:\xampp\htdocs\gacho\public\index.php(16): App\Core\Application->__construct() #1 {main} thrown in C:\xampp\htdocs\gacho\App\Core\Application.php on line 17 ' Here is my code structure and code:当我尝试使用 psr-4 自动加载选项加载我的类时,我收到此错误“致命错误:未捕获的错误:在 C:\xampp\htdocs\gacho\App\Core\Application.php:17 中找不到类 'HomeController'堆栈跟踪:#0 C:\xampp\htdocs\gacho\public\index.php(16): App\Core\Application->__construct() #1 {main} 在 C:\xampp\htdocs\gacho\App 中抛出第 17 行的 \Core\Application.php '这是我的代码结构和代码:

code structure:代码结构:

|gacho
  |- App
     |- Controller
        |- HomeController.php
     |- Core
        |- Application.php
     |- Model
     |- View
  |-public
     |- .htaccess
     |- index.php
  |-vendor
     |- composer
        |- autoload_classmap.php
     |- autoload.php
  |-composer.json

index.php:索引.php:

<?php

use App\Core\Application;

define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
define('APP', ROOT . 'App' . DIRECTORY_SEPARATOR);
define('CONTROLLER', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Controller' . 
DIRECTORY_SEPARATOR);
define('VIEW', ROOT . 'App' . DIRECTORY_SEPARATOR . 'View' . 
DIRECTORY_SEPARATOR);
define('MODEL', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Model' . 
DIRECTORY_SEPARATOR);
define('CORE', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Core' . 
DIRECTORY_SEPARATOR);

$modules = [ROOT, APP, CORE, CONTROLLER];

require_once __DIR__ . '\..\vendor\autoload.php';

$app = new Application();

composer.json:作曲家.json:

{
 "autoload": {
     "psr-4": {
        "App\\": "App/"
      }
   }
}

Application.php:应用程序.php:

<?php
 namespace App\Core;

 use App\Core\Controller;
 use App\Controller\HomeController;

 class Application
 {
    protected $controller = 'HomeController';
    protected $action = 'index';
    protected $params = [];

    public function __construct()
    {
       $this->prepareURL();
       if (file_exists(CONTROLLER. $this->controller . '.php')) {
        $this->controller = new $this->controller;
        if (method_exists($this->controller, $this->action)) {
            call_user_func_array([$this->controller, $this->action], $this->params);
          }
       }
    }

protected function prepareURL()
{
    $request = trim($_SERVER['REQUEST_URI'], '/');
    if (!empty($request)) {
        $url = explode('/', $request);
        $this->controller = isset($url[0]) ? $url[0].'Controller' : 'HomeController';
        $this->action = isset($url[1]) ? $url[1] : 'index';
        unset($url[0], $url[1]);
        $this->params = !empty($url) ? array_values($url) : [];
     }
  }
}

HomeController.php: HomeController.php:

<?php
 namespace App\Controller;

 use App\Core\Controller;

 class HomeController extends Controller
 {
    public function index($id= '', $name='')
    {
       $this->view('home\index', [
         'name' => $name,
         'id' => $id
      ]);
      $this->view->page_title = 'Home Page';
      $this->view->render();
    }

    public function users()
    {
       $this->view('home\users', []);
       $this->view->page_title = 'Users';
       $this->view->render();
    }
}

The FQCN is App\Core\Application and the path is app\core\Application.php . FQCN 是App\Core\Application ,路径是app\core\Application.php Using PSR-4 it will try to load from App\Core\Application.php , but that path does not exist (at least not in a case sensitive file system).使用 PSR-4 它将尝试从App\Core\Application.php加载,但该路径不存在(至少在区分大小写的文件系统中不存在)。

Your best option is to change the directory structure to match the namespaces.您最好的选择是更改目录结构以匹配名称空间。 They need to match exactly, including case.它们需要完全匹配,包括大小写。

尝试运行此命令: composer update

请检查您的 .env 文件和数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM