简体   繁体   中英

Fatal error class MainController not found

so i have the following structure for what i'm trying to target but what i'm getting is the Fatal error class MainController not found , i'm new to autoload and namespace thing (but getting into them fast) i just need to know why this is happening , hope you guys have bit explained situation for me? i did saw few of answer around stackoverflow but nuthing helped, i know i'm doing something really wrong, but thats how i will learn :). structure:

composer.json

src/controllers/MainController.php

the following is my autoload inside composer.json file:

"autoload": {
    "psr-4": {
       "controllers\\": "src/controllers/"
     }
}

and this is how my MainController.php looks alike :

namespace MainController;

class MainController{

  function test($name){

    echo 'holaaaa'.$name;

  }

}

controller call inside: app/loads/loadController.php :::

use MainController;

$MainController = new MainController();

more info on vendor/autoload.php

it's included inside : index.php and inside index.php i have included mainapp.php and inside mainapp.php i have included loadcontroller.php vich calls the controller

structure screenshot:

这是文件所在位置的图片。

Okay, in your Composer file you say the namespace is controllers . In your PHP file you say the namespace is MainController . They need to be the same for the autoloading to work.

If we are to go by your Composer file then the PHP should look like this:

namespace controllers;
class MainController {}

And the class should be called like this:

$MainController = new \controllers\MainController;

Or like this:

use controllers\MainController;
$MainController = new MainController;

Or, if you want a nicer-looking class name:

use controllers\MainController as Controller;
$MainController = new Controller;

就我而言,只有在从作曲家( 供应商 )删除文件夹并重新运行命令作曲家dump-autoload后,我才设法纠正它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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