带控制器动作的表单不发送路由到正确的路由


laravel 4.1 Form with controller action dosent route to correct route

我有一个应该进入TestsController控制器的表单它是takeTest方法,这是下面生成form的代码:

{{ Form::open(array('action' => 'TestsController@takeTest')) }}

生成的HTML:

<form method="POST" action="http://192.168.0.8/tests" accept-charset="UTF-8"

routes.php文件声明的路由:

Route::get('tests', 'TestsController@index');
Route::post('tests', 'TestsController@takeTest');
Route::post('tests', 'TestsController@processMarking');

它应该转到takeTest方法,但它转到processMarking方法。为什么会这样?怎样才能解决这个问题?

因为您使用相同的URI/tests和相同的方法(post)声明了两条路由,就像这样:

Route::post('tests', 'TestsController@takeTest'); // first
Route::post('tests', 'TestsController@processMarking'); // second

所以第二个route覆盖了第一个route。如果你将第二个routeURI更改为其他内容,则可以正常工作,例如:

Route::post('moretests', 'TestsController@processMarking');

你不能使用相同的URI为两个路由使用相同的方法(即post在你的情况下)。