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

BCleverly's avatar

Get two Laravel composer packages working together.

I'm looking to build several composer packages for Laravel mainly for personal use to start with and see where things go. I want to break the components into their own composer packages. The main issue I'm seeing is the best way to get the packages to talk to each other automatically. The point I've gotten to at the moment is to have a "Master package" that the others will extend with a class allowing them to talk to each other where necessary.
Has anyone done this before or able to offer up some simple code examples to help get me started?

Many thanks

0 likes
3 replies
d3xt3r's avatar

I want to break the components into their own composer packages.

If you require packages to talk to each other, think again and refactor? If you are unable to refactor for some reason, use interfaces as contracts. If just utilizing package classes/functions add them as dependencies.

Its equally good to have an adapter for packages to interact with each other, though it will create redundancy but the packages will be independent.

bbloom's avatar

It depends what you mean by "talk to each other".

The Laravel Framework itself is comprised of multiple packages that talk to each other. Although the Framework is installed via composer in one fell swoop, note composer.json in the individual packages.

PSR-4 namespacing makes "packages talking to each other" easy. A class in Package A can inject a class in Package B with "use namespace/class_location/class_name".

bilal_ch93's avatar

@bcleverly Did you manage to make that communication possible between the packages? Can you please elaborate on your solution as I tend to have 12 packages, all belonging to the same big application. I have the same requirement as of packages communicating with each other. For example:

Let's say I have two packages named A and B. I have migrations in package A that referees to a foreign key present in package B. I can definitely use polymorphic relationship here. But the problem arises when I'll have to design other part of relationship as I want my package to be completely independent of each other. I can create a trait for inverse relationship but then I'll have to write down that trait in other package and hence dependency is created. I can ofcourse try to glue it all up in main laravel app. But I'm not sure which path to follow. Can you please share your experience?

Please or to participate in this conversation.