Reputation: 13811
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?
Upvotes: 6
Views: 2921
Reputation: 518
Since Laravel 5.5 you can now register your package's service provider with the application which depends on it, by adding the following block to your package's composer.json:
...
"extra": {
"laravel": {
"providers": [
"Acme\\Package\\ServiceProvider"
]
}
},
...
See the documentation: https://laravel.com/docs/5.5/packages#package-discovery
Upvotes: 0
Reputation: 13811
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.
Upvotes: 9
Reputation: 577
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
Upvotes: 1