Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lucassimines's avatar

Why my custom package class just works if I add it into the root composer.json?

Hello guys, I created a package but it just works I if add the packaged src directory to the root package.json file.. Why doesn't it just work with my package composer.json file like all other packages?

I added it into my package composer.json file:

"autoload": {
    "psr-4": {
      "Fmcdigital\\MyPackage\\": "src/"
    }
  },

But to make it work, I also need to put on root composer.json.

Thanks.

0 likes
35 replies
lucassimines's avatar

It seems that my package composer.json is not being read.

And yes, I did composer dump-autoload many times, but no success..

sirhxalot's avatar

Hey lucassimines

Do you have an example of a package which doesn't determine the autoloading? Usually all Laravel packages will have autoloading defined within composer.json?

Some examples:

In my case i am doing:

  "autoload": {
    "psr-4": {
      "Sirthxalot\\Cache\\": "src/Sirthxalot/Cache"
    }
  },

If I look at your autoloading you may missing the directories of your package.

Can you please let us know where your repository is located at?

regards sirthxalot

1 like
lucassimines's avatar

My autoload is:

"autoload": {
    "psr-4": {
      "Fmcdigital\\EmailSignature\\": "src/"
    }
},

and the EmailSignatureServiceProvider is inside vendor/fmcdigital/emailsignature/src

It seems that composer is not reading my package autoload, because it only reads if I put inside the Application Folder/composer.json

sirhxalot's avatar

Are you sure with: mcdigital/emailsignature/src ?

How does your directory stucture is look like?

Where is the class you need to load (path of service provider or similar)?

lucassimines's avatar

the structure is vendor/fmcdigital/emailsignature/src

and the composer.json file is inside vendor/fmcdigital/emailsignature

the EmailSignatureServiceProvider.php is inside vendor/fmcdigital/emailsignature/src

sirhxalot's avatar

If I am correct than you will have the source files in your root directory? So the you only can load from there?

Please take a look at: https://github.com/sirthxalot/laravel-matryoshka/tree/master/src/Sirthxalot/Cache

My structure is: src/Sirthxalot/Cache/ .... with the namespace: Sirthxalot\Cache. So I need to determine:

  "autoload": {
    "psr-4": {
      "Sirthxalot\\Cache\\": "src/Sirthxalot/Cache"
    }
  },

So your autoloading works correct, if you only have a src directory in your root:

"autoload": {
    "psr-4": {
      "Fmcdigital\\MyPackage\\": "src"
    }
  },

However if you want another sub directory let's say package than you determine:

"autoload": {
    "psr-4": {
      "Fmcdigital\\MyPackage\\": "src/package"
    }
  },

See the diffrence?

lucassimines's avatar

Yea, I know that difference.

But my Class is just recognized if I put the autoload it on the root App composer.json, do you get that?

On the root composer:

"autoload": {
    "psr-4": {
      "Fmcdigital\\EmailSignature\\": "vendor/fmcdigital/emailsignature/src/"
  }
  },

When I add to the root App composer.json my service provider is added to cache/services.php and cache/config.php.

What I don't understand is why when I simply put my autoload on my packages composer file it doesnt add into the cache/services.php and cache/config.php files, making my class not being found.

sirhxalot's avatar

What do you mean with root App composer.json? And how does your file structure looks like?

lucassimines's avatar

I mean to the main composer.json file that is located inside that starts with "name": "laravel/laravel"

sirhxalot's avatar

Are you trying to create your package within a laravel application in the vendor directory? So you installed laravel and add your src to the vendor directory?

sirhxalot's avatar

Why? The vendor directory should only contain installed packages from composer!!!!! I highly recommend to extract your code to a directory of your laravel application lets say: ./src than you can use:

"autoload": {
    "psr-4": {
      "Fmcdigital\\MyPackage\\": "src"
    }
  },

If you want to let it in the vendor directory than your autloading should be:

"autoload": {
    "psr-4": {
      "Fmcdigital\\MyPackage\\": "vendor/fmcdigital/emailsignature/src"
    }
  },
lucassimines's avatar

Yea.. got it.

Well my package is required on composer "fmcdigital/emailsignature": "*" but it installs from my git repo, I'll try to move it into another folder outside vendor then.

lucassimines's avatar

Ok, I removed and created a packages folder, but I still need to put the autoload on the main composer.json file...

      "Fmcdigital\\EmailSignature\\": "packages/fmcdigital/emailsignature/src/"

sirhxalot's avatar

Yes what did you expect there is only one composer.json file? And no the please copy my code from before you always add a slash!

lucassimines's avatar

Yea..

What I want to do is to use autoload on my packages composer.json and not in the main composer.json.

Like this:

"autoload": {
    "psr-4": {
      "Fmcdigital\\EmailSignature\\": "src/"
    }
  },
sirhxalot's avatar

Where is your package located at Github, Gitlab? Please send me your package link.

lucassimines's avatar

Yea.. it's my companies package.

Will my package only works with it's composer file if I submit on Composer?

lucassimines's avatar

Publishing my package or use Satis then I will be able to use my own packages composer?

lucassimines's avatar

I'll just able to use the autoload inside my package's composer.json If I put it on packagist? Just want to know this doubt.

sirhxalot's avatar

Ahh okay yes this looks like our solution but I didn't set it up my collegue did. I will ask him if has a good day but this is all I can do for you. hope it helped?

sirhxalot's avatar
Level 9
  1. You need to change the privacy of your GitHub repository to public.
  2. Tag your version (https://help.github.com/articles/working-with-tags/)
  3. Visit: https://packagist.org/ and create an account.
  4. After login to packagist go to: https://packagist.org/packages/submit
  5. Paste in the URL to your repository and click submit.
  6. Your repository is now downloadable using: composer install fmcdigital/emailsignature in your application or wherever.

You than don't need to add the autoloading within your Laravel application, Composer will do that for as long as autoloading.php is required (Laravel does this by default).

But be aware anybody will have access to your repository and can download or install it with Composer as long as it is public.

1 like
Next

Please or to participate in this conversation.