如何在Laravel 5.6中选择外部供应商的订单?

I need prioritize the project routes vs the packages routes in Laravel 5.6.12. I've read that one solution could be placing the RouteServiceProvider call before than the packages call. All right, but defaultly, when I install with composer the dependencies, all the external ServiceProviders appears before to RouteServiceProvider.

If I check my bootstrap/cache/services.php generated:

23 => 'Fideloper\\Proxy\\TrustedProxyServiceProvider',
24 => 'Laravel\\Tinker\\TinkerServiceProvider',
25 => 'Yajra\\DataTables\\DataTablesServiceProvider',
26 => 'Spatie\\Permission\\PermissionServiceProvider',
27 => 'Intervention\\Image\\ImageServiceProvider',
28 => 'Spatie\\MediaLibrary\\MediaLibraryServiceProvider',
29 => 'Spatie\\LaravelImageOptimizer\\ImageOptimizerServiceProvider',
30 => 'Laracasts\\Flash\\FlashServiceProvider',
31 => 'Jenssegers\\Agent\\AgentServiceProvider',
32 => 'DaveJamesMiller\\Breadcrumbs\\BreadcrumbsServiceProvider',
33 => 'JoseAragon\\MyPackage\\MyPackageServiceProvider',
34 => 'App\\Providers\\AppServiceProvider',
35 => 'App\\Providers\\AuthServiceProvider',
36 => 'App\\Providers\\EventServiceProvider',
37 => 'App\\Providers\\RouteServiceProvider',

RouteServiceProvider is the last item. I cant put it before the package, because in my config/app.php I don't have the ServiceProviders thats appear in the services.php generated.

I need put 37 -> RouteServiceProvider before 33 -> MyPackageServiceProvider that have a lot of routes.

Can you help me?

Really I need use the package routes, but if I need create a new route in the Laravel project, override and prioritize this routes before that the package routes.

Do you know other solution?

Thanks a lot!!!

in your config/app.php

inside providers array where you are registering the ServiceProvider

$providers = [
//othere Services providers
 MyPackageServiceProvider::class,
  RouteServiceProvider::class
];

if you run php artisan optimize Your MyPackageServiceProvider will loads first.

Illuminate\Foundation\Application::registerConfiguredProviders is an issue here.

Solution: Create namespace like Illuminate\CustomServices and place your ServiceProvider within it.


More background on the problem: Illuminate\Foundation\Application::registerConfiguredProviders

  1. creates a collection from your App config providers array;
  2. splits this array in 2 chunks [Everything that starts with Illuminate\, rest of it];
  3. adds all the composer packages service providers in between;

And this will give you a result array where all your ServiceProviders are ranked as you ranked them, but after everything that starts with Illuminate\ and after 3rd Party Composer ServiceProviders.

You have to disable the auto-discovering feature of the 3rd party library. To do that, open your composer.json file and add the libraries you want to disable auto-discovery for in the extra like this

"extra": {
"laravel": {
    "dont-discover": [
        "vendor/library-name",
        "spatie/laravel-permission"
    ]
},

Then manually set the auto-discovery of the libraries in any order you want in your config/app file of you laravel project.

This will fix the problem of having auto-generated-provider before some laravel default provider. You can now make out your own Provider order as you want.