1

In a project with Symfony 4, I import a self-made bundle with composer

"require-dev": {
    "symfony/dotenv": "^4.0",
    "symfony/profiler-pack": "^1.0",
    "myusername/my-bundle": "dev-master"
},
"repositories" : [{
    "type" : "vcs",
    "url" : "git@github.com:myusername/my-bundle.git"
}],

The bundle is correctly installed but I got the following error:

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "MyBundle" from namespace "My\Bundle\MyBundle".

The bundle is loaded in the Kernel from the bundles.php file

My\Bundle\MyBundle\MyBundle::class => ['dev' => true, 'test' => true]

Here you are the composer.json of my bundle:

{
  "name" : "myusername/my-bundle",
  "type" : "symfony-bundle",
  "autoload" : {
    "psr-4" : {
      "myusername\\MyBundle\\" : ""
    }
  }
}

And finally the main file of the bundle is MyBundle.php

<?php
namespace My\Bundle\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use My\Bundle\MyBundle\DependencyInjection\MyExtension;

class MyBundle extends Bundle
{
    public function getContainerExtension()
    {
        return new MyExtension();
    }
}

I'm aware that the problem is caused by the autoloader but I can't figure out how to fix it. I tried the composer dump-autoload command and I have consulted the following similar questions but without luck:

Thank you!

Gianluca78
  • 794
  • 1
  • 9
  • 24
  • 1
    Where is the file located in your folder structure? – dbrumann Jan 03 '18 at 12:30
  • vendor/myusername/my-bundle/MyBundle.php – Gianluca78 Jan 03 '18 at 12:49
  • Take a look at the psr-4 section of composer.json for some of your other bundles. You don't include the actual directory (myusername) but you do need to spell out the full root namespace (My\Bundle\MyBundle). So something like: "psr-4": { "My\\Bundle\\MyBundle\\": "" }, And of course be sure to run composer dump-autoload after making changes locally. – Cerad Jan 03 '18 at 13:51
  • Ok it worked. Thank you, please write an answer and I will approve it... – Gianluca78 Jan 04 '18 at 08:43

1 Answers1

1

Just composer.json:

"autoload": {
        "psr-4": {
            "": "src/"
        }
    },
Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23