Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bootstrap laravel 5 service providers from the vendor package itself?

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?

like image 223
Rohan Avatar asked Jan 07 '16 12:01

Rohan


People also ask

How can I get service provider in Laravel?

If you open the config/app. php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application.

What is the register and boot method in the service provider class?

Providers have two lifecycle methods: register and boot . The register method is where you should add bindings to the service container. The boot method is for performing actions after all service providers have registered their services.

Where can I register a package in Laravel?

Once the workbench command has been executed, your package will be available within the workbench directory of your Laravel installation. Next, you should register the ServiceProvider that was created for your package. You may register the provider by adding it to the providers array in the app/config/app. php file.

What is route service provider in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.


2 Answers

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.

like image 60
Rohan Avatar answered Oct 10 '22 08:10

Rohan


you can create a package composer.json file and add the dependencies in there for the package itself so when you do composer require author/package it will look at the dependencies of that package and require them automatically below is an example of a composer.json requirement for a package I often pull

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

you can add the following Boot 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 do not forget that

like image 20
Sari Yono Avatar answered Oct 10 '22 07:10

Sari Yono