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

david19's avatar

Load Routes from a Service Provider

Hello Team. I created a new service provider.

In the boot method i call migrations and a route file. The migrations works, but for the routes, i get the error, failed to open stream.

php artisan route:list --compact

The routes are there! But it will not work.

<?php

namespace App\Providers\Schema;

use Illuminate\Support\ServiceProvider;

class OrganizationServiceProvider extends ServiceProvider
{
   
    public function register()
    {
        //
    }

  
    public function boot()
    {
        $this->loadMigrationsFrom('database/migrations/organization');
        $this->loadRoutesFrom('routes/organization/web.php');
        
    }
}


The Migration works, but not the route file. Many thanks for help :)

0 likes
11 replies
jlrdw's avatar

Somewhere in docs he covers the order of things in the laravel cycle, the boot method may be too early for the route in cycle.

1 like
david19's avatar

@jlrdw yes, it is correct. Maby the problem is, i dont use this in a package. Its a application service provider. But the migration works, and the routes are there, in the route list. I dont know why the error:

require(routes/organization/web.php): Failed to open stream: No such file or directory

thinkverse's avatar

That might have to do with the path given, loadRoutesFrom requires the file directly if the routes are not already cached, so it's likely the path given doesn't point to the actual file you want to load, you might have to use __DIR__ as a prefix and use that as a base to travel to the correct folder, or use any of Laravel's path helpers to travel to the correct folder.

Laravel internally in it's RouteServiceProvider uses the base_path() function when getting the routes.

Route::middleware('web')
    ->namespace($this->namespace)
    ->group(base_path('routes/web.php'));
1 like
david19's avatar

Thanks for your answere. But why the routes will found, when i check " php artisan route:list "?

The DIR is for the package path, because packages are in the root folder. But my route file is in the normal /routes/organization/web.php folder.

When i try this, it will also not work :(

$this->loadRoutesFrom(__DIR__.'/routes/organization/web.php');
thinkverse's avatar

What is your exact structure? OrganizationServiceProvider is located where? And the routes are located where?

1 like
david19's avatar

Thanks for your help. Normal structure,

app/Providers/OrganizationServiceProvider

Register the Provider, in Config/App.php

The Path of the Route File
/routes/organization/web.php
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;


class OrganizationServiceProvider extends ServiceProvider
{
  
    public function register()
    {
        
    }

    public function boot()
    {
       
        $this->loadMigrationsFrom('database/migrations/organization');

        $this->loadRoutesFrom('routes/organization/web.php');
       
}

}

The Migrations works, and also the command

php artisan route:list

The routes are there.

<?php

use Illuminate\Support\Facades\Route;


use App\Http\Controllers\Organization\OrganizationController;


Route::middleware(['auth:sanctum', 'verified'])->group(function () {
   
    Route::resource('/dashboard/organization',OrganizationController::class)->only('edit','update');

    
       
});
require(routes/organization/web.php): Failed to open stream: No such file or directory
thinkverse's avatar
Level 15

In this case $this->loadRoutesFrom(base_path('routes/organization/web.php')); will work just fine. 👍

1 like
david19's avatar

Many thanks :) Yes, now it works. But if i visit this route, i automatically redirect to /dashboard. Maby it is because, the middlewares.

Route::middleware(['auth:sanctum', 'verified'])->group(function () {
   
    Route::resource('/dashboard/organization',OrganizationController::class)->only('edit','update');

  });
thinkverse's avatar

Most likely, had issues with that too so I removed them and I didn't get the redirect.

Tested loading routes using an app-based provider and package-based provider, it's available to view on my GitHub under the scratch repo and provider branch if you're interested in the different approaches.

1 like
david19's avatar

I will try fix this issue. But your answere wars correct, many thanks for your help :)

david19's avatar

@thinkverse I fix the issue. You must add the middleware 'web',too.

Route::middleware(['web','auth:sanctum', 'verified'])->group(function () {
   
    Route::resource('/dashboard/organization',OrganizationController::class)->only('edit','update');

  });

Now it works!!! :)

Please or to participate in this conversation.