I’m trying to run a Sign up form in Laravel, however, upon clicking Signup, I get this error "The POST method is not supported for route signup. Supported methods: GET, HEAD."
Now I asked around in Laracast and was instructed to produce two files; 1. routes/web.php file and 2. the Signup HTML Form.
- routes.web.php
<?php
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/signup', 'UserController@postSignUp')->name('signup');
- Signup HTML Form
<div class="row">
<div class="col-md-6">
<h3 class="text-center text-success">Sign Up!</h3>
<form action="{{ route('signup') }}" method="post">
<div class="form-group">
<label for="email">E-mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="first_name">First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
2
Answers
Your route is defined as GET request, but your HTML form is attempting to make a POST request.
To resolve this issue, you need to define a route post in web.php like this,
And when making post request in laravel, don’t forget to add csrf token in your form.
you can add it like this
The error occurs because you are trying to send a POST request using the GET method. When we send data using a form, we should use the POST method (it’s more secure). By the way, in your form, you can add @csrf encryption for additional security.
here is the correct route
Route::get('/signup', 'UserController@postSignUp')->name('signup');