Laravel4 route pattern error

筅森魡賤 提交于 2019-12-08 07:24:27

问题


I´m using laravel 4 for a cms project, and i´m having some problems with my routes...

These are my current routes

Route::get('/', 'IndexController@showNews');
Route::get('/logout', 'UserController@logout');
Route::resource('/login', 'UserController');
Route::resource('/user', 'UserController@index');
Route::resource('/user/{route}', 'UserController');


// Routes that shows us the pages...
Route::get('/{page}', 'IndexController@showPage');
Route::get('/{page}/{id}', 'IndexController@showPage');

To my user routes i have a custom router that routes the user information around, not really a problem. But all of that works great, but when i try to navigate to "/test" Wich would link to a test page, it gives me this error.

Route pattern "/user/{route}/{{route}}" cannot reference variable name "route" more than once.

It comes up to router logic, and i´m fairly new to laravel. Is there a way for me to work around this problem? It´s a collision between the user/route and the /route wildcards.


回答1:


Route::resource('/user', 'UserController@index');
Route::resource('/user/{route}', 'UserController');

The problem is that you are using Route::resource to declare the routes, while by using Route::resource you are actually declaring a RESTful controller with a table of actions to be handled by Laravel automatically. You are using it incorrectly.

See the docs to see which routes are handled in the background (and hence the source of the conflict):

Take a look at the table called Actions Handled By Resource Controller

For any route handler that is not within the table you will have to declare separate routes. Something like:

Route::get('foo/filter/{filterName}/{filterValue}',
        array('as'=>'filteredroute','uses'=>'FooController@filter'))

As a summary, Route::resource enables you quick CRUD RESTful access.



来源:https://stackoverflow.com/questions/19608311/laravel4-route-pattern-error

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