LaravelのAuthでのログインフォーム実装がうまくいきません
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- Laravelのログインフォームの動作がうまくいかずログインできない
前提
Laravel初心者です。ログイン機能を実装しようとyoutubeの参考動画(URLは後述)を参考にしてコーディングしましたがうまくいきません。
Laravel uiもインストールしました。
ググりましたが、原因と思われる今回の問題に合致する参考サイトも見つからなかったため助言を頂きたいです。
発生している問題・エラーメッセージ
具体的な問題点としては、MySQLでdbとテーブル作成してレコードに適合したデータをログインフォームに入力しているのにホーム画面にページが飛びません。
入力するデータは[email][password]です。
(ホーム画面=home.blade.php)(ログインフォームのページ=index.blade.php)!
該当のソースコード
php/web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LoginController;
use App\Http\Requests\LoginFormRequest;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/index', [App\Http\Controllers\LoginController::class, 'Loginshow']);
Route::post('/index', [LoginController::class, 'login'])->name('login');
//新規登録フォーム表示
Route::get('/index/create', [App\Http\Controllers\UserController::class, 'showUserCreateForm']);
//新規登録
Route::post('/index/create', [App\Http\Controllers\UserController::class, 'newUser']);
//ホーム画面
Route::get('/home', [HomeController::class, 'homeshow']);
php/LoginController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginFormRequest;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function Loginshow()
{
return view('index');
}
public function login(LoginFormRequest $REQUEST)
{
$credentials = $REQUEST->only('email','password');
if(Auth::attempt($credentials)) {
$REQUEST->session()->regenerate();
return redirect('home'); /*->with('Login_success','ログインが成功しました!');*/
}
return back()->withErrors([
'Login_error'=> 'メールアドレスかパスワードが間違っています。'
]);
}
}
php/home.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ホーム画面</title>
</head>
<body>
<h1>ホーム画面</h1>
</body>
</html>
php/HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function homeshow()
{
return view('home');
}
}
php/index.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ログイン画面</title>
</head>
<body>
<div>
<h1>ログイン画面</h1>
<div>
{!! Form::open(['url' => route('login') , 'method' => 'post']) !!}
<div>
{!! Form::input('text', 'email', null, ['required', 'class' => 'form-control', 'placeholder' => 'メールアドレス']) !!}
{!! Form::input('password', 'password', null, ['required', 'class' => 'form-control', 'placeholder' => 'パスワード']) !!}
</div>
<button type="submit" class="">ログイン</button>
{!! Form::close() !!}
</div>
</div>
<div>
<button><a href="/index/create">ユーザー登録</a></button>
</div>
</body>
</html>
php/LoginFormRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class LoginFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|max:255',
'password' => 'required',
];
}
}
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
試したこと
ルーティングに誤りがあるかと思いましたが、僕では見つかりませんでした。
補足情報(FW/ツールのバージョンなど)
PHP 8.2.4
Laravel Framework 8.83.27
【参考サイトURL】
① https://www.youtube.com/watch?v=xKcWZqLoeDs&t=177s
② https://www.youtube.com/watch?v=cgyMT-67cm4
0 likes
