0

I have a form which should go to the TestsController Controller and it's takeTest method, this is the code below that generated the form:

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

The generated HTML:

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

The routes declared in routes.php file:

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

It should go to takeTest method but it goes to the processMarking method instead. Why is this and how can it be fixed?

The Alpha
147k30 gold badges294 silver badges313 bronze badges
asked Jun 9, 2014 at 21:10
1
  • How do you expect it to differentiate between two routes with the same uri and request method? Commented Jun 9, 2014 at 21:20

1 Answer 1

2

Because you have declared two routes using same URI/tests and same method (post) like this:

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

So the second route is overriding the first route. If you change the URI of your second route to something else then it'll work, for example:

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

You can't use same URI for two routes using same method (i.e. post in your case).

answered Jun 9, 2014 at 21:19
Sign up to request clarification or add additional context in comments.

4 Comments

I would of thought the Controller action would match against the same one in the routes. Otherwise what is the point of stating a controller action in the form?
The 'action' => 'TestsController@takeTest' generated action="http://192.168.0.8/tests" URI as action.
When you have used Form::open(array('action' => 'TestsController@takeTest')) it searched for value in the RouteCollection object and found tests as the key for TestsController@takeTest, that's it.
Cheers for the explanation and answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.