2
votes

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">
2
How are you serving your application? What web server software are you using?namelivia
please provide your view code, it looks like you might be trying use a GET method on a POST routeCodeBoyCode
Please check your POST requests are not being redirected to GET requests by your server. I recently discovered this with apache which redirected based on a trailing / in the request url.thisiskelvin
@CodeBoyCode AddedJoe
<form action="/sender" method="post> missing quotation mark after postCodeBoyCode

2 Answers

4
votes

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

0
votes

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));
}