Max
Max

Reputation: 83

Laravel routes for user profiles? without /user

In Laravel Routing:

I wanna make profile users with a URL nice: example.com/steve

Actually I have: example.com/user/steve

When I make the routing update this works:

Route::get('{username}', ['as' => 'profile', 'uses' => 'ProfileController@show']);

But when I try enter to another routes (/guide):

Route::get('guide', ['as' => 'guide', 'uses' => 'GuestController@showGuide']);

This "collapse" with the user profiles route. How can I make that works without 404 errors?

I wanna try with a different method that "the i method" (put the letter i before all the URLs: Route::get('i/guide'))

Upvotes: 2

Views: 1263

Answers (3)

xenish
xenish

Reputation: 1424

Route::group(['prefix' => 'i'], function()
{
    Route::get('guide', function()
    {
        // Matches The "/i/guide" URL
    });
});

Group the routes and add prefix to them.

Upvotes: -1

Emeka Mbah
Emeka Mbah

Reputation: 17545

Laravel matches listed routes from top to bottom. So \{username} will match \{anyword}

One of the ways to workaround this is;

In your routes.php Route::get('guide', ['as' => 'guide', 'uses' => 'GuestController@showGuide']); should come first.

Let Route::get('{username}', ['as' => 'profile', 'uses' => 'ProfileController@show']); be placed after every other routes

Upvotes: 2

Khan Shahrukh
Khan Shahrukh

Reputation: 6361

You should first create the route for guide

Route::get('guide', ['as' => 'guide', 'uses' => 'GuestController@showGuide']);

and then for user

Route::get('{username}', ['as' => 'profile', 'uses' => 'ProfileController@show']);

the sequence does matter in routes.php file. If the {username} route is above guide route then it will treat guide as {username} and hence redirect you accordingly but then you dont have a user with a username guide

Upvotes: 1

Related Questions