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

RafaelMunoznl's avatar

When routes collide

My app had the following routes

Route::resource('/', 'HomeController');

Route::resource('listings', 'ListingsController');

Now I need to add some pages like Imprints, Contact, About US. So, I created a Pages Controller

Route::get('/page', 'HomeController@show')->name('main.show');

In order to have:

Problem is that after introducing this, the http://myapp.com/listings became a 404 error because this is a "page" that does not exist.

Provided that I will have a blog in the future, I could change the pages to a new PagesController :

Route::resource('pages/{page}', 'PagesController');

But I will have :

But being honest, I do not think it is very elegant and I would like to have a clean structure like this:

Is there any way I could make that Route::resource('listings', 'ListingsController'); and Route::resource('/', 'HomeController'); coexist with each other?

Rerouting or something like that?

0 likes
6 replies
Snapey's avatar

please check over your question.It makes no sense.

You dont show any route that would respond to /contact-us and your last paragraph you repeat the same route

RafaelMunoznl's avatar

@snapey I edited the question.

I have these two options: I make the pages in the HomeController in order to have a clean URL

Route::get('/', 'HomeController@index')->name('main.index');
Route::get('/{page}', 'HomeController@show')->name('main.show');

It gives a nice and clean url: http://myapp.com/contact-us but it collides with the ListingsController because http://myapp.com/listing/ will be understood as a page and gives a 404 back.

Or I move the pages to a second Controller:

Route::resource('pages/{page}', 'PagesController');

But I will have an URL like this: http://myapp.com/pages/contact-us which is not nice

jove's avatar
jove
Best Answer
Level 7
'/{page}'

is a catch all, place it at the end. Do you do this because all your routes are dynamic?

RafaelMunoznl's avatar

@jove I am not sure I understand what you mean with "Place it at the end".

Yes, all my routes are dynamic. I just add a new page in the data base and it will get rendered in front end. For example, if I add now a faq page, I will have a https://myapp.com/faq page in frontend

Snapey's avatar

its about the order of the routes in the file. The routes are evaluated from top to bottom. Put listings before pages and all will be well

Please or to participate in this conversation.