-1

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?

Dariss
  • 1,258
  • 1
  • 12
  • 27

1 Answers1

2

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();
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Thank you mate. Working well. I will go for your structure, looks clear comparing to my one. :) – Dariss Dec 01 '13 at 20:34