路线碰撞在Laravel 4 [英] Routes collision in Laravel 4
问题描述
我正在使用Laravel 4进行一个项目,我有一个用户路线"以按用户名显示用户个人资料:
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') )
但是这里有一个碰撞. Laravel认为消息"是用户名,因为它是第一个路线.
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.
推荐答案
您必须更改这些路由的顺序,因为Laravel按照在routes.php
You must change the order of these Routes as Laravel processes them in the order they are defined in routes.php
所以
Route::get('user/messages', array( 'as' => 'userMessages', 'uses' => 'MessageController@getMessages') )
先于
Route::get("user/{username}", array( 'as' => 'userProfile', 'uses' => 'UserController@getProfile') );
然后在您的User
验证中,您必须阻止任何人选择用户名messages
And then in your User
validation you must prevent anyone from choosing the username messages
这篇关于路线碰撞在Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!