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

时间:2019-02-04 18:01:16

标签: php oop autoload psr-4

当我尝试使用psr-4自动加载选项加载类时,出现此错误'致命错误:未捕获的错误:在C:\ xampp \ htdocs \ gacho \ App \ Core \ Application中找不到类'HomeController'。 php:17堆栈跟踪:#0 C:\ xampp \ htdocs \ gacho \ public \ index.php(16):App \ Core \ Application-> __ construct()#1 {main}抛出C:\ xampp \ htdocs \ gacho \ App \ Core \ Application.php在第17行'这是我的代码结构和代码:

代码结构:

|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

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:

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

Application.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:

<?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();
    }
}

2 个答案:

答案 0 :(得分:0)

FQCN为App\Core\Application,路径为app\core\Application.php。使用PSR-4,它将尝试从App\Core\Application.php进行加载,但是该路径不存在(至少在区分大小写的文件系统中不存在)。

您最好的选择是更改目录结构以匹配名称空间。它们需要完全匹配,包括大小写。

答案 1 :(得分:0)

尝试运行此命令:composer update