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

RicardoAbel's avatar

Laravel 11 service provider are not auto discovered

I created my own packages in Laravel/packages/Myname/User

my /User/composer.json contain the following :

"autoload": {
      "psr-4": {
      "Myname\\User\\": "src/"
  }
 },
 "extra": {
     "laravel": {
      "providers": [
          "Myname\\User\\Providers\\UserServiceProvider"
       ]
   }
 },

here is the main laravel composer.json autoload:

 "autoload": {
     "psr-4": {
      "App\\": "app/",
      "Database\\Factories\\": "database/factories/",
      "Database\\Seeders\\": "database/seeders/",
      "Myname\\User\\": "packages/Myname/User/src/"
   }
 },

Here is my UserServiceProvider.php

  class UserServiceProvider extends ServiceProvider
  {

   public function register()
   {
     $this->app->bind(UserInterface::class, UserRepository::class);
    }

   public function boot()
    {
       $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
    }
  }

Why my service provider is not working, since i have the extra in User/composer.json

I run also :

 composer dump-autoload

it works only when I do manually in AppServiceProvider.php the following:

 public $bindings = [
    UserInterface::class => UserRepository::class,
 ];
0 likes
6 replies
JussiMannisto's avatar

I don't understand your setup.

To use a package in an app, you have to install it via composer. To do that, the package has to be hosted somewhere. If you're hosting it locally, you have to add a custom repository in the composer.json of your app.

Have you installed the package through composer?

JussiMannisto's avatar

@RicardoAbel Your app's composer.json has an autoload entry for the path packages/Myname/(...). This isn't needed when a package is installed correctly.

Right now you don't have an installed package. You just have code in a different directory that you're autoloading. Autodiscovery doesn't work because this is not how packages are used.

See this link on how to host the package locally, then install it using composer:

https://getcomposer.org/doc/05-repositories.md#path

RicardoAbel's avatar

@JussiMannisto I agree with you, but why some projects they don't do it this way. here is an example : https://github.com/krayin/laravel-crm look the main composer.json there is no : "repositories" : [ { } ],

all the packages are loaded in autoload section ( im talking about Webkul packages exactly )

thanks

JussiMannisto's avatar
Level 50

@RicardoAbel I see. The term package is a bit of a misnomer there because they're not really packages, they're modules within the same code base. But that's one way of organizing your code.

I don't know if you'll be able to get auto-discovery working that way. Those Webkul service providers aren't auto-discovered either.

puklipo's avatar

Write the directory structure correctly.

If the package is in the Laravel project

- laravel-project/
- ./packages/Myname/User/

The package's composer.json is not loaded.

The package's classes are loaded according to the autoload in the Laravel project's composer.json.
Manually add the package's ServiceProvider to bootstrap/providers.php.

If the Laravel project and package are separate

- laravel-project/
- ../packages/Myname/User/

Load the local package using the path setting in the Laravel project's composer.json.
https://getcomposer.org/doc/05-repositories.md#path

Please or to participate in this conversation.