Routes collision in Laravel 4

送分小仙女□ 提交于 2019-12-11 12:19:30

问题


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' => 'UserController@getProfile') );

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

Route::get('user/messages', array( 'as' => 'userMessages', 'uses' => 'MessageController@getMessages') )

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.


回答1:


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

so,

Route::get('user/messages', array( 'as' => 'userMessages', 'uses' => 'MessageController@getMessages') )

comes before

Route::get("user/{username}", array( 'as' => 'userProfile', 'uses' => 'UserController@getProfile') );

And then in your User validation you must prevent anyone from choosing the username messages



来源:https://stackoverflow.com/questions/20314710/routes-collision-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!