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

marghoobsuleman's avatar

Laravel 11 Upgrade

Hi,

In Laravel 11, how can we ensure that routes defined in routes/web.php take priority over routes defined in packages? For example, if both routes/web.php contains /welcome and somepackage/src/routes/web.php also contains /welcome, how do we ensure that /welcome from routes/web.php takes precedence over /welcome from somepackage/src/routes/web.php?

0 likes
10 replies
DhPandya's avatar

@marghoobsuleman In such cases you should go with the naming routes. For example in your web.php

Route::get('/welcome',function(){
echo "Some action";
})->name('welcome.web');

It will search the routes available with the name and take it on the priority where it is found.

jlrdw's avatar

I suggest avoid duplicates or switch to named routes for some of them.

marghoobsuleman's avatar

Thank you all for your responses. I understand the importance of proper naming conventions. However, the issue arises with a wildcard (*) route that has been defined within the package. Could you please suggest a method to ensure that the package routes are loaded after the routes defined in the main route/web.php file?

LaryAI's avatar
Level 58

To ensure that routes defined in your application's routes/web.php take priority over routes defined in packages in Laravel 11, you can control the order in which routes are loaded. Laravel loads service providers in the order specified in the config/app.php file. By default, your application's routes are loaded by the RouteServiceProvider. To give priority to your application's routes over package routes, you should ensure that your RouteServiceProvider is loaded after all package service providers.

Here's how you can adjust the loading order:

  1. Check the Order in config/app.php: Ensure that your App\Providers\RouteServiceProvider is listed after any package service providers in the providers array of your config/app.php file.

  2. Modify RouteServiceProvider: In your RouteServiceProvider, specifically in the map method, make sure to load the web.php routes after any package routes are registered. You can do this by ensuring the mapWebRoutes call is placed at the end of the map method.

Here is an example of what the map method in RouteServiceProvider might look like:

public function map()
{
    $this->mapApiRoutes();

    // Ensure web routes are loaded last
    $this->mapWebRoutes();
}

protected function mapWebRoutes()
{
    Route::middleware('web')
         ->namespace($this->namespace)
         ->group(base_path('routes/web.php'));
}
  1. Load Package Routes Before Your Routes: If you have control over how package routes are loaded (e.g., you are developing the package), you can modify the package's service provider to load its routes before your application's routes. This can be done by placing the package's service provider before the App\Providers\RouteServiceProvider in config/app.php.

  2. Override Package Routes Explicitly: If a package does not provide a way to prevent route registration or if you need a quick fix, you can explicitly override the package route by redefining the same route in your web.php after the package routes are loaded. Laravel will use the last registered route when multiple routes have the same path and method.

By following these steps, you can ensure that your application's routes in routes/web.php take precedence over any conflicting routes defined in packages.

marghoobsuleman's avatar

Laravel 11 should not remove the route provider from the app config. This is a workaround to define the priority of routes.

I do not like this hack but it's working.

if(is_file(resource_path('../routes/web.php'))) { $this->loadRoutesFrom(resource_path('../routes/web.php'), function ($router) { // silence is the bliss }); } $this->loadRoutesFrom(__DIR__.'/routes/web.php');

Snapey's avatar

remember, L11 still supports any routing methods defined in L10 so that people upgrading don't have to change anything if they dont want. So you could still have a RouteServiceProvider - if that helps ?

kossa's avatar

I have the same issue, where I have this route:

Route::get('{slug}', [FeedController::class, 'show'])->name('feeds.show')->where(['slug' => '.*']);

My package routes not loading

Tray2's avatar

@kossa That route will take anything with a '/whatever'

It's like a catch all route, and you should put that last in your stack of routes.

Please or to participate in this conversation.