php - Routes collision in Laravel 4

599

I am working on a project using Laravel 4, I have a "user route" to show user profiles by their username:

Route::get("user/{username}", array( 'as' => 'userProfile', 'uses' => '[email protected]') );

But here I have another route which shows a user's messages.

Route::get('user/messages', array( 'as' => 'userMessages', 'uses' => '[email protected]') )

But there is a collision here. Laravel thinks "messages" is a username because of first Route.

How can I work around this? Could some one help me, thanks.

336

Answer

Solution:

You must change the order of these Routes as Laravel processes them in the order they are defined inroutes.php

so,

Route::get('user/messages', array( 'as' => 'userMessages', 'uses' => '[email protected]') )

comes before

Route::get("user/{username}", array( 'as' => 'userProfile', 'uses' => '[email protected]') );

And then in yourUser validation you must prevent anyone from choosing the usernamemessages

People are also looking for solutions to the problem: java - Passing ID with PHP and MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.