Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AndrykVP's avatar

Routes from multiple Service Providers in Custom Package

Hello everyone. I'm making a custom Laravel Package and I'm running into a problem with the package's routes.

First of all, I'm making several Service Providers that are registered by a "master" provider:

    public function register()
    {
        // Register all the Package's Service Providers
        $this->app->register(
            FirstServiceProvider::class,
            SecondServiceProvider::class,
            ...
        );  
    }

Both service providers have routes of their own, but only the ones from the first provider are loaded properly (as proved by running php artisan route:list

<?php 

Route::group(['namespace' => 'Vendor\Package\First\Http\Controllers', 'middleware' => ['web']], function(){
	Route::get('example1', function() {
		print_r('Hello World');
	});
});

However, the second service provider's routes are not loaded, despite having the exact same code in both their web.php file and their ServiceProvider.php. The only thing that changes is the namespace and the route uri.

<?php 

Route::group(['namespace' => 'Vendor\Package\Second\Http\Controllers', 'middleware' => ['web']], function(){
	Route::get('example2', function() {
		print_r('Hello World');
	});
});

Am I missing something, or can I not load multiple routes from separate service providers? I could load all routes from the master provider instead, but it wouldn't work well with the modularity of my code for git branches. I hope you guys can help me.

Thanks in advance

0 likes
6 replies
bobbybouwmann's avatar

How do you register the routes in your service provider?

I'm not sure if you can register multiple service providers in a service provider in Laravel at all. I would probably create multiple methods that create routes per functionality.

You need to load all the routes in the boot method of the service provider, that is very important

Documentation: https://laravel.com/docs/7.x/packages#routes

AndrykVP's avatar

They're being registered in the boot method:

    public function boot()
    {
      // Load routes
      $this->loadRoutesFrom(__DIR__.'/../First/Routes/web.php');
    }

and

    public function boot()
    {
      // Load routes
      $this->loadRoutesFrom(__DIR__.'/../Second/Routes/web.php');
    }

My package is using 4 different Service Providers right now and I'm only having problems with the routes. The other Service Providers have migrations, Traits, Models and Commands and everything else is working just fine.

bobbybouwmann's avatar

Are you sure the path is correct?

Have you tried registering all the routes in the "master" provider, just to try it out and see if that works?

AndrykVP's avatar

@bobbybouwmann I followed your advise and loaded the routes from the master provider.

    public function boot()
    {
        $this->loadRoutesFrom(__DIR__.'/../First/Routes/web.php');
        $this->loadRoutesFrom(__DIR__.'/../Second/Routes/web.php');
    }

I didn't change the path at all, and I commented out these lines from the respective provider. This way works, so the path is correct. But the moment I return them to being loaded by the child providers, the problem persists and only the ones from First are registered.

For clarity, this is my folder structure:

src/
    First/
        Controllers/
        Routes/
            web.php
    Second/
        Controllers/
        Routes/
            web.php
    Providers/
        FirstServiceProvider.php
        SecondServiceProvider.php
        MasterServiceProvider.php

What I gather from moving the route loading to the master provider is that this is not a problem of using separate loadRoutesFrom() methods, because I did it right there, and it worked just fine. And like I mentioned earlier, every other Service Provider loads Traits, Models, Facades, etc. of their own without a problem.

P.S. I just moved the order in which the child providers are registered:


    public function register()
    {
        // Register all the Package's Service Providers
        $this->app->register(
            SecondServiceProvider::class,
            FirstServiceProvider::class,
            ...
        );  
    }

And this loads the routes from Second but not from First. So I guess this means that you can only load routes from 1 single service provider; which in my case should be Master. That's a bummer

AndrykVP's avatar
AndrykVP
OP
Best Answer
Level 2

I got an answer to this in Laravel's GitHub issue #33460. The problem is that I was registering the child providers wrongly. In the master provider, the register() method should've been like this:

$this->app->register(FirstServiceProvider::class);
$this->app->register(SecondServiceProvider::class);
$this->app->register(ThirdServiceProvider::class);

Not this

$this->app->register(
    FirstServiceProvider::class,
    SecondServiceProvider::class,
    ThirdServiceProvider::class,
); 

Please or to participate in this conversation.