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

pavsid's avatar

Multiple routes with same URL, but different names

Hi all, i have set up two routes like so..

Route::get('messages', [
            'as' => 'messages.inbox',
            'uses' => 'MessagesController@inbox'
        ]);
        Route::get('messages', [
            'as' => 'messages',
            'uses' => 'MessagesController@inbox'
        ]);

So, they have the same URL, Controller, Action, but have different names.

You might wonder why anyone would do this, and my only reason is that the app points to the URL in various places using either of the two names, and so I didn't want to go through and change them all to use one named route.

What i've done has been working fine, but today I noticed that my local copy is throwing an error saying that the route messages.inbox doesn't exist.

It was working previously on my local copy, and the only change I can think of is that I ran a composer update yesterday.

Presumably the errors is being thrown because the second route is overwriting the first, due to them having the same URL.

Is this caused by the composer update? Both the composer.js files on my local copy and the test server are the same, so why is one throwing an error and the other not?

0 likes
9 replies
danmatthews's avatar
Level 5

I believe that URLs are indexed and keyed on their URL - so therefore the first one would be overridden completely by the first one.

I think so anyway, can anyone else confirm this?

1 like
ohffs's avatar

I think you'd be quicker doing a global search&replace on the name of the route rather than trying to debug this, tbh :-)

pmall's avatar

Use one route names and replace the wrong name in your views

laracoft's avatar

I'm aware this is 6 years old, but there is a use case and solution:

Use case: same routes with different names are sometimes required to be compatible with popular and not-so-popular packages at the same time.

Since routes are keyed by their URLs, we can create multiple names for the "same" route using:

  1. Route::get('/route/that/i/need', ...)->name('original'); - original
  2. Route::get('/route/that/i/need/{clone1?}', ...)->name('1');
  3. Route::get('/route/that/i/need/{clone2?}', ...)->name('2');
laracoft's avatar

@Snapey

That is correct, because the intention was to give one route many names (not many logic).

e.g. Laravel resource routes require them to be named a certain way, customers.index, customers.edit etc But I also need customers.index to be named customers

yasir786's avatar

@laracoft @snapey

I'm also aware this is 6 years old, but i found one solution:

In my case i have two routes with same method and URL but with different name. I have used optional parameter and pass this param from my blade file link or where we like to call this route

  1. Route::get('/', [AuthController::class, 'welcome'])->name('welcome');
  2. Route::get('/{lang?}', [FrontendController::class, 'home'])->name('home')->defaults('lang', "en")->where('lang', 'en');

so in my blade or where we call this route i pass optional param en like this

Blade File

{{ route('home', ['lang' => en]) }}

if you want to call route from controller then like this

return redirect()->action([FrontendController::class, 'home'], [ 'lang' => 'en']);

1 like

Please or to participate in this conversation.