Reputation: 501
I am new to Laravel and am trying to do a simple post
request but it is not working. It is says object not found
after redirecting to /o2
. Could someone shed some light on it? I am using CSRF Token
too but things aren't working. Using v5.2 of Laravel.
Route::get('/o1', function(){
echo '<form method="post" action="/o2"><input name="_token" value="' . csrf_token() . '" type="hidden"><button type="submit" value="Submit">Submit</button></form>';
});
Route::post('/o2', function(){
echo "It works";
});
Upvotes: 0
Views: 287
Reputation: 7073
You are trying to access a post route in your browser to print the form. This is not going to work. You need to create a get route to print the form and then set the action to the post route. Try something like this:
Route::get('/o1', function(){
echo '<form method="post" action="/o2"><input name="_token" value="' . csrf_token() . '" type="hidden"><button type="submit" value="Submit">Submit</button></form>';
});
Route::post('/o2', function(Request $request) {
echo $request->all();
});
Upvotes: 1
Reputation: 443
How are you calling this route when you get the MethodNotAllowedHttpException ?
This post request should be called by the actual form you are creating. And should generally not return the actual form but persist the data and then redirect the user to the page to show the result.
So in general you would have a get and a post:
Route::get('/o1', function(){
echo '<form method="post" action="/o2"><input name="_token" value="' . csrf_token() . '" type="hidden"><button type="submit" value="Submit">Submit</button></form>';
});
Route::post('/o1', function(){
// persist your form here
});
I strongly recommend using a controller for this. https://laravel.com/docs/5.6/controllers
Upvotes: 0