Skip to content

Instantly share code, notes, and snippets.

@TerrePorter
Last active January 25, 2024 21:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerrePorter/4d2ed616c6ebeb371775347f7378c035 to your computer and use it in GitHub Desktop.
Save TerrePorter/4d2ed616c6ebeb371775347f7378c035 to your computer and use it in GitHub Desktop.
Laravel: Giving a service provider load priority
<?php
/*
In the bootstrap/app.php file, add one of the following above the return statement.
*/
/*
Option 1, if you want to be ballsey and load before any service provider in the config/app.php file.
*/
$app->resolving(Illuminate\Foundation\Bootstrap\RegisterProviders::class, function () use ($app) {
// need? necessary? - hell no, its just because I want to
dump('I want to be first before any service provider listed in config/app.php');
$app->register(\MyClass::class);
});
/*
Option 2, load you service provider after the View service provider, aka before the vendor autoload service providers.
*/
$app->resolving('view', function () use ($app) {
dump('I want to be next in line after the view service provider');
$app->register(\MyClass::class);
});
@arfar-x
Copy link

arfar-x commented Aug 21, 2022

Hi there,
How can we specify that our PackageServiceProvider loads after loading all of other packages' providers ?

@TerrePorter
Copy link
Author

Option 1, should work although I have not tested it in the latest version of Laravel.

Just replace the \MyClass::class with your package service provider reference.

@arfar-x
Copy link

arfar-x commented Aug 22, 2022

Option 1, should work although I have not tested it in the latest version of Laravel.

Just replace the \MyClass::class with your package service provider reference.

In that way you mentioned, we have to specify the service provider which we want to load our one after. It causes some problems while adding third-party packages.

Clearly, I want to Artisan::all() to get all commands of both Laravel and other packages. My service provider loads before third-party packages and cannot get commands.

For more info, please check this repository : arfar-x/artisan-api#2 - branch bugfix-dev

Thanks

@TerrePorter
Copy link
Author

@aariow

This might work

$app->resolving(NAMESPACE/TO/THIRD-PARTY/Service provider::class , function () use ($app) {
    $app->register(\MyServiceProvider::class);
});

Here is what i understand your looking for. You want to load your service provider after a thirparty's service provider. I am assuming this is using the auto-registere thingy. where you didn't have to put in the namespace/path to the service provider.

I haven't looks at this code in sometime, and i couldn't find the parent project where i was using it. This is my best guess as to what your wanting to do. if its not or this doesn't work, give me a better break down of the goal, laravel version, and i will make some time to dig in to it some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment