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

alenn's avatar

How to load package dependencies

Hi all I'm building a package which needs another package to function properly, but no matter what I do service provider can't be found. This is composer.json inside my package:

...
"minimum-stability": "dev",
    "require": {
        "bschmitt/laravel-amqp": "dev-master",
        "illuminate/support": "^5.2"
    }

Now I run composer update and composer dump-autoload, then I register this package in my service provider:

...
public function register()
    {
        $this->app->register(Bschmitt\Amqp\AmqpServiceProvider::class);
        AliasLoader::getInstance()->alias("Amqp", Bschmitt\Amqp\Amqp::class);
    }

And when I visit any route I get this:

Class 'Bschmitt\Amqp\AmqpServiceProvider' not found

Package works if there are no other dependencies included. Can someone help me with this, I really don't know what to do.

0 likes
3 replies
austenc's avatar

@alenn -- did you ever find a workable solution for this?

To everyone else, sorry in advance for bumping this 3 month old thread... but it was both the most recent and most relevant. I have been searching for quite a while now and haven't yet found a 'good' solution to this problem. I understand that when the package is defined in an app's composer.json require section, composer will automatically autoload that package's dependencies... but what does everyone do while they are developing their packages? Here are some proposed solutions I've seen, but I'd love to hear how other people are doing this too!

  • Define the package as a local repository in your composer.json file -- this could work... but you'll have to composer update every time you want to reflect package changes. Seems cumbersome.
  • Add a require() statement that requires the package vendor/autoload.php file. Not really a solution because this won't exist when the package is actually installed via composer.
  • Drop in the package's git repo in the main app's vendor/myvendor/packagename folder. Seems pretty hacky and just wrong! Also don't want to replace development files if i do a composer update in the root app

How does everyone handle the "develop a package while using it in a 'root' laravel app" problem in L5?

alenn's avatar
Level 7

@austenc Nope, eventually I added instructions to Readme file so users must manually add required packages in their composer.json file.

austenc's avatar

@alenn -- I was finally able to figure this out! Just wanted to follow up here in case it helps you and/or future readers. The reason you were getting the Class not found error is because the root composer.json (and /vendor folder) doesn't know about the package-specific packages/xyz/vendor folder. In L4, the workbench module used to look for nested workbench/your/package/vendor/autoload.php files and automatically include them. In L5 this doesn't happen, and I wasn't able to get it to work the same way even when I tried to re-implement that piece.

Anyway, the real solution here is to utilize composer's ability to define a local repository. This basically creates a symlink from /packages/xyz (or wherever your package repo is) to the root /vendor folder. Note that on windows you need to select Run As Administrator before you start your terminal (and virtual machine -- assuming you're using vagrant / homestead here), otherwise the symlinks won't work. Then you'll add this to your composer.json file (I put my package in a /core directory):

    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "austenc/core": "*@dev"
    },

... 

    "repositories": [
        {
            "type": "path",
            "url": "./core"
        }
    ]

This will make it behave as a composer-installed package, but because of the symlink, you'll be able to work on the package files in the /core directory without having to run any composer install / update commands! Now, as for dependencies defined in the package, you simply put them in your require section of your package's composer.json and then the $this->app->register('Dependency\Name\ServiceProvider') calls should work from within the register() method of your package's service provider. If any of the above needs clarification, let me know!

1 like

Please or to participate in this conversation.