I need help with composer autoloader. Well in my opinion I have set up everything correctly, but still I am having an error "class has been not found".

So maybe someone here will be able to help me. Look at the screenshoots below to understand the way I structured my project and the way I autoloaded namespace for my Test class.

enter image description here

enter image description here

enter image description here

The question is why I am having an error, class has been not found?

有帮助吗?

解决方案

You’re autoload array is wrong in your composer.json file. If your root namespace is app then it should look like this:

{
    "autoload": {
        "psr-0": {
            "app": "/"
        }
    }
}

You can then use your classes in the app namespace like this:

<?php
require('../vendor/autoload.php');

$test = new \app\controller\Test();

However, I would camel-case your namespaces, as is the PSR way. So in my case, I have a directory structure like this:

  • src/
    • MCB/
      • Controller/
        • PagesController.php
  • vendor/
    • autoload.php

My composer.json file looks like this:

{
    "autoload": {
        "psr-0": {
            "MCB": "src/"
        }
    }
}

And I can then use my classes like this:

<?php
require('../vendor/autoload.php');

$controller = new \MCB\Controller\PagesController();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top