该路由不支持POST方法。支持的方法:Laravel中的GET,HEAD

时间:2020-06-17 09:04:04

标签: php laravel

当我单击发送按钮时,我收到此错误消息: “此路由不支持POST方法。受支持的方法:GET,HEAD。”

我的观点:

<form action="" method="post">

@csrf
<h1> Gmail Sender </h1>

        <div class='form-group'>
            <label>Email:</label>
            <div class="form-group col-md-3">
                <input name="email" type="email" placeholder= "Email to .."/>
          </div>
        </div>

        <div class='form-group'>
            <label>Subject:</label>
            <div class="form-group col-md-3">
                <input name="subject" type="subject" placeholder= "Email to .."/>
          </div>
        </div>

          <div class='form-group'>
            <label>Content:</label>
            <div class="form-group col-md-4">
            <textarea  class="form-control" name="txtDescription"
             id="txtDescription" cols="30" rows="3" ></textarea>
          </div>
          </div>

          <button> SEND</button>

        </div>
    </div>
</div>
</div>
 @endsection

我的控制器:

<?php
namespace App\Http\Controllers;
use\Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use App\Mail\MyMail;

class MyMailController extends Controller
{
public function sendMail(Request $request){
$this->validate($request,[
'email'=>'required',
'subject'=>'required',
'content'=>'required',
]);

   $myMail= new MyMail(
      $request->input('subject'),
    $request->input('content')
    );

   Mail::to($request->input('email'))->send($myMail);
   return redirect()->back()->with('success', 'Email sent successfully. to:' .$request->input('email'));
}
}

我的路线:

Route::view('/sendmail', 'sendmail');
Route::post('/' , 'MyMailController@sendMail');

我想发送电子邮件。但是,当我单击“发送”按钮时,出现了此错误消息“此路由不支持POST方法。受支持的方法:GET,HEAD。”

1 个答案:

答案 0 :(得分:0)

我使用命名路由来更好地管理表单操作。

Route::post('/' , 'MyMailController@sendMail')->name('sendMail');

在您看来,别忘了向表单添加操作

<form action="{{ route('sendMail') }}" method="post">