8

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!!!

R P
  • 711
  • 6
  • 29
Jose Aragon
  • 81
  • 1
  • 3

3 Answers3

3

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.

Emmanuel
  • 121
  • 5
-1

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.

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
  • I already looked there but only have the default laravel service providers. All the dependencies come from the package. By default, my package load before that the default RoutesServicesProvider. I need to do the opposite. – Jose Aragon Mar 15 '18 at 17:24
  • According to the Application::registerConfiguredProviders it's hardcoded to have everything that starts with 'Illuminate\' to go to the starting chunk, all others to the end, and Composer Packages go in the middle. I'm facing the same problem and that's sad. The only option I see right now is not trick the system by creating 'Illuminate\Override\' namespace and put my service provider there. – Alexandr Nov 12 '18 at 13:53
-1

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.

Alexandr
  • 121
  • 1
  • 5
  • Give a more precise feedback please, as 'is not good' don't feel like meaningful and reasonable. – Alexandr Jan 31 '19 at 08:07
  • I mean using official namespace of laravel may confuse teams etc. So this hack is not good. By the way I haven't rated down as it seems there is no proper solution to this problem – Shirshak55 Feb 01 '19 at 06:40
  • Well, the fact that it's not good but the only possible way makes it The way and the right answer :) – Alexandr Feb 14 '19 at 10:40