Norgul
Norgul

Reputation: 4783

PSR-4 autoloader issue

I have trouble seeing what am I doing wrong with autoloader. My folder structure is as follows:

| - src/
|    - Files/
|        - Bla.php
|    - Models/
|        - ...
| - vendor/
|     - ...
| composer.json

And composer.json autoload part looks like this:

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

Now the Bla.php looks like this:

<?php

namespace Migrations\Files;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Bla extends AbstractMigration
{
    ...
}

And I am getting the error:

Fatal error: Class 'Doctrine\Migrations\AbstractMigration' not found in /var/www/html/migrations/src/Files/Bla.php

When I look at the vendor folder, package is there. From my IDE also indexing works fine so that I can CMD+click to the AbstractMigration file without problem.

I have tried deleting the vendor folder, clearing composer cache, doing a dump-autoload and reinstalling all packages, but nothing seems to work.

Where am I making the mistake?

Upvotes: 1

Views: 396

Answers (1)

ColinMD
ColinMD

Reputation: 964

You need to include the Composer autoload file otherwise your application doesn't know what classes exist. It is a file created by composer when you install the dependencies, a lot of frameworks that use Composer will include this file for you automatically, but if you are not using a framework you will need to include the file yourself.

require __DIR__ . '/vendor/autoload.php';

Where you need to put it will depend on your application but its best to load as early as possible, if you have a bootstrap file then that would be the place to put it.

You can read about it here

Upvotes: 1

Related Questions