'The POST method is not supported for this route. Supported methods: GET, HEAD for register users. - LARAVEL
Hi guys I saw many times this problem and I solved it but this time I cant see where is the problem, I have been blocked for several days, so I would like someone more experienced to help me, I await your response and thank you very much in advance.
A week ago I could register users without problems, but then they cant get loged (idk why), now they cant even register.
Route:
Route::get('/registerCreate', [App\Http\Controllers\Auth\RegisterController::class, 'create'])->name('register.create');
Route::get('/registerStore', [App\Http\Controllers\Auth\RegisterController::class, 'store'])->name('register.store.usu');
Controller:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Usuario;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
public function index()
{
return view('auth.register');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'nombre' => ['required', 'string', 'max:50'],
'apellido' => ['required', 'string', 'max:50'],
'email' => ['required', 'string', 'email', 'max:50', 'unique:users'],
'telefono' => ['required', 'string', 'number', 'max:9', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'nacionalidad' => ['required', 'string', 'max:50'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\Usuario
*/
protected function create(array $data)
{
return Usuario::create([
'nombre' => $data['nombre'],
'apellido' => $data['apellido'],
'password' => Hash::make($data['password']),
'telefono' => $data['telefono'],
'email' => $data['email'],
'nacionalidad' => $data['nacionalidad'],
]);
}
public function store()
{
$usuario = Usuario::create(request([
'nombre',
'email',
'password' => Hash::make('password'),
'telefono',
'email',
'nacionalidad',
]));
auth()->login($usuario);
return redirect()->to('/login');
}
}
Form:
<body class="body">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header text-center h5 py-4" style="font-weight:600">Registrate en Chani</div>
<div class="card-body">
<form action="{{ route('register.store.usu') }}" method="post">
@csrf
<div class="modal-body">
@if($message = Session::get('ErrorInsert'))
<div class="col-12 alert alert-danger alert-dismissable fade show" role="alert">
<h5>Errores:</h5>
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<input type="hidden" class="form-control" name="id">
</div>
<div class="form-group">
<input type="text" class="form-control" name="nombre" placeholder="Nombre">
@error('nombre')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="text" class="form-control" name="apellido" placeholder="apellido">
@error('apellido')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email">
@error('email')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="number" class="form-control" name="telefono" placeholder="Telefono">
@error('telefono')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="password" class="form-control" name="password"
placeholder="Contraseña">
@error('contrasena')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="password" class="form-control" name="password2"
placeholder="Confirmar Contraseña">
@error('contrasena2')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
<div class="form-group">
<input type="text" class="form-control" name="nacionalidad"
placeholder="Nacionalidad">
@error('nacionalidad')
<p class="border border-red-500 rounded-md bg-red-100 w-full text-red-600 p-2 my-2">
{{$message}}</p>
@enderror
</div>
</div>
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-lg btn-block mb-3"
style="color:white; background-color: #1cc88a; font-weight:600">Registrarme</button>
</div>
</form>
<div class="d-flex justify-content-center">
<a type="button" href="login" class="btn linkColor"
style="cursor:pointer; color:#1cc88a; font-weight:640">Ya tengo una cuenta</a>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
Solution 1:[1]
You are submitting form using the post method
<form action="{{ route('register.store.usu') }}" method="post">
But in Route you are using Route::get
you need to change it to Route::post -
Route::post('/registerStore', [App\Http\Controllers\Auth\RegisterController::class, 'store'])->name('register.store.usu');
Solution 2:[2]
You cant run a create or store because your routes are set to GET in your web.php, change them to
Route::post('/registerCreate', [App\Http\Controllers\Auth\RegisterController::class, 'create'])->name('register.create');
Route::post('/registerStore', [App\Http\Controllers\Auth\RegisterController::class, 'store'])->name('register.store.usu');
And I recommend reading up on some laravel docs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dhairya Lakhera |
| Solution 2 | dz0nika |
