EventFellows's avatar

Order of routes being applied

Hi Everyone, I have a stupid issue that I have been unable to get my head around in hours. So hopefully someone has a hint...

I have set up two routes that belong to the same URI (they will later have different patterns so there is a sense to it... - this is the most minimal version I used for testing). My clear expectation would be that the first matching route would be used according to their order in the routes.php file.

But actually the LAST matching route is being applied. Why is that? (It totally messes up the whole routing files logic for me so it is actually important to understand)

Here are my routes in question.

Route::group(['middleware' => 'web'], function () {
    Route::get('/', array('as' => 'home', function () { 
        return view('index');
    }));
    Route::get('/', array('as' => 'home2', function () { 
        return view('home2');
    }));
});

This code will show the 'home2' view on my L5.2 homestead setup. When switching the order around it will also show the last view for the specific URI - so it is probably not a before/after Filter or so. The same is true if I add a third URI...

Any ideas how to look into this are highly appreciated.

0 likes
4 replies
d3xt3r's avatar

do php artisan route:list, it will show you all the routes applicable to your projects. You will find it interesting ... :)

EventFellows's avatar

Well, not sure if I understand your hint about finding it interesting...

It shows all routes of the application (as expected) and only shows the last route as described above. Which still leaves me puzzled as to why the last URI is being used instead of the first...?

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

and only shows the last route as described above?

Right? when application is bootstraped, it load all the routes in memory, any similar route will be over ridden by the last route (as in your case). But when, the route matching is done, it quits at the very first match,

So. if you have

Route::get('/', array('as' => 'home', function () { 
        return view('index');
    }));
    Route::get('/', array('as' => 'home2', function () {  
    // This route will always be loaded as the router does not know about the first route since its overridden.
        return view('home2');
    }));

and if you have

   Route::get('/user/{user}', array( ... ));

    Route::get('/user/create', array( ... )); // This route will never be called even with user/create
Grumbleman's avatar

i am having the same problem...

if i put the route order like

Route::resource('user','UserController'); Route::get('user/{role}','UserController@index');

the last route in the list doesnt work.

if i change the order the not all of the resource routes will work...

what i did to get this working was changing the route itself like adding something different to the uri (e.g. adding /by/):

Route::get('user/by/{role}','UserController@index');

Please or to participate in this conversation.