Ad

How To Bootstrap Laravel 5 Service Providers From The Vendor Package Itself?

- 1 answer

I am working on a Laravel 5 application and now the code of the application is supposed to be re-used in multiple laravel 5 applications which is why I am creating 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 build over it too.

I am new to composer package development and especially hooking packages into Laravel 5 using Service Providers. So far I have learnt that if I use a service provider as the one below, I will be able to 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 via composer into any brand new Laravel 5 installation and then I just need to updated the provider's array in app.php with this:

Uppdragshuset\AO\Tenant\TenantServiceProvider::class

This makes sense to me and works too. But now the package that I am developing also has dependencies of its own and many of the those dependency packages also have laravel 5 service providers included so I have to manually include all of them in the laravel5 installations to make them work.

But I am guessing there must be a way to register these dependent service providers in the package that I am creating itself somehow so that I just need to register the one provider I mentioned above. The problem is I don't know how to do this and cannot find a similar reference anywhere. How to register multiple service providers from the composer package itself?

Ad

Answer

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

I have the main TenantServiceProvider in my package which is supposed to hook in routes into the main application and is also responsible for publishing migrations, configs and so on.

Turns out I can register any dependant service providers via the same provider using the register() method on the App facade and calling it in the register method of my main TenantServiceProvider like so:

public function register()
{
    include __DIR__.'/routes.php';

    App::register(RepositoryServiceProvider::class);
    App::register(\Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);
}

In this way I only have to register the TenantServiceProvider in the provider's array in the app.php config file of the laravel application. When it is called and the register method on it is called, all the other providers will be registered via the App::register() calls. Hope this helps someone.

Ad
source: stackoverflow.com
Ad