2

I have a very weird problem. I have a post route but I receive an error that The GET method is not supported for this route.

This is my web.php function:

Route::post('/sender',function () {
    $text = request()->text;
    event(new FormSubmitted($text));
});

I'm definitely sending a post request. I've already checked this: Laravel: POST method returns MethodNotAllowedHttpException

But the chosen answer is unclear.

My View Code:

<?php echo csrf_field(); ?>

{{ csrf_field() }}



<form action="/sender" method="post>
First name: <input type="text" name="fname"><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<input type="text" name="content"><br>
<input type="submit">
Joe
  • 51
  • 1
  • 1
  • 8

2 Answers2

4

I believe that this might just be a typo error - you have missed a quotation mark (") after 'post'

view:

<form action="/sender" method="post">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    First name: <input type="text" name="fname"><br>
    <input type="text" name="content"><br>
    <input type="submit">
</form>

controller

Route::post('/sender',function () {
    $name = request->fname;
    $content = request->content
    event(new FormSubmitted($name, $content));
});

EDIT: updated controller code, you were requesting the data from an input called 'text', but there wasn't any inputs with the name of 'text' in the view, only input type's

CodeBoyCode
  • 2,227
  • 12
  • 29
  • Fixed it but nothing changed. I tried changing the post that I have to "GET" request and this isn't throwing any errors. Is that normal? – Joe Mar 18 '19 at 10:34
  • what do you mean Joe - personally the best way of doing this for me would be to open the form with action and method, and then declare your CSRF token, and then your inputs, remembering to close your form with (i know you haven't included within your code, have you done that within your code? – CodeBoyCode Mar 18 '19 at 10:38
  • hey Joe I just noticed something wrong with your controller aswell – CodeBoyCode Mar 18 '19 at 10:42
  • Did this fix your issue @Joe – CodeBoyCode Mar 18 '19 at 14:06
0

First, check you define route proper or not by php artisan route:list command

Blade file

<form action="{{ route('sender') }}" method="post">
@csrf
First name: <input type="text" name="fname"><br>

<input type="text" name="content"><br>
<input type="submit">

Route

Route::post('/sender',function () {
    $text = request()->fname; //access by input field name
    event(new FormSubmitted($text));
})->name('sender');

or

Route::post('/sender', 'UserController@sender')->name('sender');

if you're using route with controller then your controller seems like that

public function sender(Request $request)
{
    $fname = $request->fname;
    event(new FormSubmitted($fname));
}
bhavinjr
  • 1,663
  • 13
  • 19