skip to Main Content

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.

  1. 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');



  1. 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


  1. 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,

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

    And when making post request in laravel, don’t forget to add csrf token in your form.

    you can add it like this

    <form action="{{ route('signup') }}" method="post">
      @csrf
      //rest of your code
    </form>
    
    Login or Signup to reply.
  2. 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');

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search