Routes and post laravel

Viewed 32

When i create the function on controller to show with method 'index' and add this route on my web.php works, but when try the form, with post method to 'login', Laravel alert me on my browser: Route [samein.login] not defined. What is my mistake? I'm new on Laravel, and not understand completely: ¿What is my error? Controller:

<?php

namespace App\Http\Controllers;
use App\Http\Requests\loginrequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Queue\RedisQueue;

class logincontroller extends Controller
{

    public function index(){
        return view('login');
    }

    public function login( )
    {
        $credentiales = $request->getCredentials();

        if( Auth::validate($credentiales) ){
            return redirect()->to('login')->withErrors('auth.failed');
        }

        $user = Auth::getProvider()->retrieveByCredentials($credentiales);

        Auth::login($user);
        return $this->authenticated($request,$user);
    }

    public function authenticated (Request $request,$user){
        return redirect('accountModule.indexusers');
    }
}

Route on web.php :

Route::resource('/samein',logincontroller::class);

Template:

@extends('components\header')
<section class="vh-100">
  <div class="container-fluid h-custom">
    <div class="row d-flex justify-content-center align-items-center h-100">
      <div class="col-4">
          <img src="{{ URL::asset('img/logo.png') }}"
          class="img-fluid" height="500">
      </div>
      <div class="col-md-8 col-lg-6 col-xl-4 offset-xl-1">
        <form action="{{ route('samein.login') }}" method="POST">
          @csrf
          <!-- Email input -->
          <div class="form-outline mb-4">
            <input type="email" name="username" class="form-control form-control-lg"
              placeholder="Enter a valid email address" />
            <label class="form-label" for="form3Example3">Usuario</label>
          </div>

          <!-- Password input -->
          <div class="form-outline mb-3">
            <input type="password" name="password" class="form-control form-control-lg"
              placeholder="Enter password" />
            <label class="form-label" for="form3Example4">Contraseña</label>
          </div>

          <div class="text-rigth text-lg-start mt-4 pt-2">
            <button type="button" class="btn btn-primary btn-lg"
              style="padding-left: 2.5rem; padding-right: 2.5rem;">Iniciar Sesión</button>
          </div>

        </form>
      </div>
    </div>
  </div>
</section>

[enter image description here][1] [1]: https://i.stack.imgur.com/trktp.png

1 Answers

Resource controllers have default route names assigned to them by Laravel.

Instead of:❌

{{ route('samein.login') }}

Use this:✅

{{ route('samein.store') }}

Actions Handled By Resource Controller

POST | /photos | store | photos.store

Addendum

Naming Resource Routes

By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your desired route names:

use App\Http\Controllers\LoginController;
 
Route::resource('samein', LoginController::class)->names([
    'store' => 'samein.login'
]);
Related