Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the order of external Providers in 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!!!

like image 322
Jose Aragon Avatar asked Mar 15 '18 14:03

Jose Aragon


People also ask

What is the providers array 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. Of course, many of these are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.

How do I register a service provider in Laravel?

By default, a set of Laravel core service providers are listed in this array. These providers bootstrap the core Laravel components, such as the mailer, queue, cache, and others. To register your provider, simply add it to the array:

How to use orderby() and orderbydesc() methods in Laravel 6?

As of Laravel 6, the orderBy () and orderByDesc () query builder methods support passing a query, instead of just a column name. When you do this, the query is executed as a subquery within the order by statement. $users = User::orderBy (Company::select ('name') ->whereColumn ('companies.user_id', 'users.id') )->get ();

What is the relationship between categories and products in Laravel?

In a one to many relationships between the Categories and the Products, we intend to view the products but in an orderly manner. Preferably by their name. The Eloquent relationship will look like this: The Laravel Orderby promotes order about the parameters provided. In this case, “name”.


1 Answers

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.

like image 157
Emmanuel Avatar answered Oct 19 '22 22:10

Emmanuel