0

Lets say I'm building a laravel package, which has hierarchy like this:

PackageContainerClass > *PackageClass* > SomePackageClassMethod()

I want to make PackageClass swappable by a class from main App.

I know I could create a publishable config/package_config.php where the user could override PackageClass location and then use it for creating PackageClass object in the package. But I have quite a few classes to be 'swapped', and dont really feel that its the right approach. What's the best approach for this, is there any Laravel magic I dont know about?

  • 1
    You could use a service provider class to bind the swappable classes to a singleton. `app()->singleton(PackageInterface::class, PackageClass::class)`. I recommend checking the source code for `laravel/fortify` as it uses this approach quite a lot. – IGP Jan 03 '22 at 14:24
  • 1
    This sounds like a much better approach, will explore. Thanks! – Dainius Vaičiulis Jan 03 '22 at 14:36

0 Answers0