Search code examples
laraveldependency-injectionpackagerepository-pattern

How we can have dependency injection in laravel package


I develop the Laravel package and I want to use Repository Pattern and put services in it and then inject this Repository to my Controller.

Pay attention that I need a solution in package development.


Solution

  • you may define the provider in the extra section of your package's composer.json file. In addition to service providers, you may also list any facades you would like to be registered:

    "extra": {
        "laravel": {
            "providers": [
                "Barryvdh\\Debugbar\\ServiceProvider"
            ],
            "aliases": {
                "Debugbar": "Barryvdh\\Debugbar\\Facade"
            }
        }
    },
    

    source: https://laravel.com/docs/9.x/packages#package-discovery