How to download laravel 5 service providers from the provider package itself?

I am working on a Laravel 5 application, and now the application code should be reused in several laravel 5 applications, so I create a composer package and then I would like to install this package in any number of laravel 5 applications to have the same functionality and create on it also.

I am new to composer package development and especially connect packages to Laravel 5 using service providers. So far, I have learned that if I use a service provider as below, I can use the routes in the laravel 5 application:

<?php namespace Uppdragshuset\AO\Tenant; use Illuminate\Support\ServiceProvider; class TenantServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { include __DIR__.'/routes.php'; } } 

Now, to make this work, I just require the package through the composer to any new Laravel 5 installation, and then I just need to update the providers array in app.php as follows:

 Uppdragshuset\AO\Tenant\TenantServiceProvider::class 

It makes sense to me and works. But now the package I'm developing also has its own dependencies, and many of these dependency packages also include laravel 5 service providers, so I have to manually include all of them in the laravel5 installations for them to work.

But I suppose there should be a way to register these dependent service providers in a package that I create myself, so I just need to register one provider mentioned above. The problem is that I do not know how to do this, and I can not find a similar link anywhere. How to register several service providers from the composer package itself?

+5
source share
2 answers

So, I finally figured out how to register dependent service providers from the composer package itself.

I have the main TenantServiceProvider in my package, which should connect to the routes in the main application and is also responsible for publishing migrations, configs, etc.

It turns out that I can register any dependent service providers through the same provider using the register() method on the App facade and calling it in the register method of my main TenantServiceProvider as follows:

 public function register() { include __DIR__.'/routes.php'; App::register(RepositoryServiceProvider::class); App::register(\Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class); } 

Thus, I need to register TenantServiceProvider in the providers array in the app.php configuration file of the laravel application. When it is called and the registration method on it is called, all other providers will be registered through calls to App::register() . Hope this helps someone.

+6
source

you can create a package composer.json file and add dependencies there for the package itself, so when you need a composer, author / package, it will look at the dependencies of this package and require them automatically below, is an example. Composer requirement .json for the package I often pulling

 "require": { "php": ">=5.5.0", "illuminate/console": "~5.0", "illuminate/support": "~5.0", "illuminate/cache": "~5.0" 

you can add the following download method to publish your service provider

  public function boot() { $this->publishes([ __DIR__ . '/config/configifyouhaveone.php' => config_path('pathtotheconfig.php') ]); AliasLoader::getInstance()->alias( 'serviceprovidername', 'Namespace\Subfolder\PackageName\PackageFacade' ); } 

after that you will need to do php artisan vendor: publish, so don't forget that

+1
source

Source: https://habr.com/ru/post/1240091/


All Articles