致命错误:未捕获错误:未找到类“ home”

时间:2020-10-29 13:14:08

标签: php oop model-view-controller

我遇到以下错误:致命错误:未捕获的错误:找不到类'home' 他说的是没有找到家庭班,但它存在, 下面的代码:

index.php

<?php
  require 'App/autoload.php';

  $controllerFile = isset($_GET['uri']) ? $_GET['uri'] : 'home';

  $path = __DIR__.'/App/Controllers/';
  $extension = '.php';
  $fullpath = $path . $controllerFile . $extension;

   //verifica se existe
  if(file_exists($fullpath)) {
     $controller = new $controllerFile;
     $controller->index();
  } else {
     echo "Pagina inexistente...";
     return 0;
  }

?>

App / autoload.php

<?php
spl_autoload_register('autoLoader');
function autoLoader($className) {
  $path = __DIR__."/Controllers/";
  $ext = ".php";
  $fullPath = $path . $className . $ext;
  
  if (!file_exists($fullPath)) {
    return false;
  }
  include_once $fullPath;
}

App / Controller / Home.php

<?php

namespace App\Controllers;

class Home
{
    public function index() {
        echo 'home';
    }
}

关于index.php,我可以毫无问题地收到.htaccess定义的$ _GET ['uri'],唯一的问题是因为$controller = new $controllerFile; 我在教程中搜索了他们使用这种方式实例化该类的方法,但我没有。如果有人可以帮助我,我将不胜感激。

0 个答案:

没有答案