当前位置:网站首页>Larvel document reading notes custom authentication login and registration using larvel 8
Larvel document reading notes custom authentication login and registration using larvel 8
2022-07-02 17:45:00 【IT1995】
Blog posts mainly use Laravel8 Create custom login and registration .
Create custom user logins using traditional simple methods 、 register 、 Panel page .
Case study
There are the following steps :
① establish Laravel application ;
② Connect MySQL database ;
③ Set up auth Of Controller;
④ establish auth route ;
⑤ establish auth Of blade view file ;
⑥ start-up Laravel service .
establish Laravel application
Installation and configuration composer after , Use the following command to create Laravel project .
composer create-project --prefer-dist laravel/laravel laravel_new Enter the directory where you created the application .
cd laravel_new Connect to database
Give Way Laravel Connect mysql database , open .env The configuration file , Modify the database name , user name , password :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password Laravel The default user model and migration file are provided , So it can be used php The command is created from the migration file given by default mysql surface .
php artisan migrate Set the permissions Controller
Call the following command , establish Controller
php artisan make:controller CustomAuthControllerstay app\Http\Controllers\CustomAuthController.php Replace with the following code :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('auth.registration');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("dashboard")->withSuccess('You have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function dashboard()
{
if(Auth::check()){
return view('dashboard');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function signOut() {
Session::flush();
Auth::logout();
return Redirect('login');
}
} establish Auth route
stay routes/web.php Add the following code to :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuthController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('dashboard', [CustomAuthController::class, 'dashboard']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('signout', [CustomAuthController::class, 'signOut'])->name('signout'); establish auth Of blade view file
stay resources/views Created in login.blade.php:
@extends('app')
@section('content')
<main class="login-form mt-5">
<div class="cotainer">
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card">
<h3 class="card-header text-center">Login</h3>
<div class="card-body">
<form method="POST" action="{
{ route('login.custom') }}">
@csrf
<div class="form-group mb-3">
<input type="text" placeholder="Email" id="email" class="form-control" name="email" required
autofocus>
@if ($errors->has('email'))
<span class="text-danger">{
{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="password" placeholder="Password" id="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="text-danger">{
{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group mb-3">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
<div class="d-grid mx-auto">
<button type="submit" class="btn btn-dark btn-block">Signin</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
@endsection Then create registration.blade.php stay resources/views/auth Under the table of contents :
@extends('app')
@section('content')
<main class="signup-form mt-5">
<div class="cotainer">
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card">
<h3 class="card-header text-center">Registration</h3>
<div class="card-body">
<form action="{
{ route('register.custom') }}" method="POST">
@csrf
<div class="form-group mb-3">
<input type="text" placeholder="Name" id="name" class="form-control" name="name"
required autofocus>
@if ($errors->has('name'))
<span class="text-danger">{
{ $errors->first('name') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="text" placeholder="Email" id="email_address" class="form-control"
name="email" required autofocus>
@if ($errors->has('email'))
<span class="text-danger">{
{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group mb-3">
<input type="password" placeholder="Password" id="password" class="form-control"
name="password" required>
@if ($errors->has('password'))
<span class="text-danger">{
{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group mb-3">
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember Me</label>
</div>
</div>
<div class="d-grid mx-auto">
<button type="submit" class="btn btn-dark btn-block">Sign up</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
@endsection The next step is resources/views Created in dashborad.blade.php file :
@extends('layouts.app')
@section('content')
<nav class="navbar navbar-light navbar-expand-lg mb-5" style="background-color: #e3f2fd;">
<div class="container">
<a class="navbar-brand mr-auto" href="#">Example</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
@guest
<li class="nav-item">
<a class="nav-link" href="{
{ route('login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{
{ route('register-user') }}">Register</a>
</li>
@else
<li class="nav-item">
<a class="nav-link" href="{
{ route('signout') }}">Logout</a>
</li>
@endguest
</ul>
</div>
</div>
</nav>
@endsection Finally create resources/views/app.blade.php file :
<!DOCTYPE html>
<html lang="{
{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{
{config('app.name','LSAPP')}}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
@yield('content')
</body>function laravel service
php artisan serve register URL And login URL Respectively :
http://127.0.0.1:8000/registration
http://127.0.0.1:8000/login Output :


边栏推荐
- SAP Commerce Cloud 架构概述
- JDBC
- Platform management background and business menu resource management: business permissions and menu resource management design
- LeetCode:1380. Lucky number in matrix -- simple
- 嵌入式开发板 ~ 说明
- OpenHarmony如何启动FA(本地和远程)
- 每日一题——小乐乐改数字
- Meanings of SNAT, DNAT and masquerade in iptables
- MATLAB中nexttile函数使用
- 海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
猜你喜欢

Ssm+ wechat applet to realize property management system

简单线性规划问题

Platform management background and business menu resource management: business permissions and menu resource management design

Virtual lab basic experiment tutorial -7 Polarization (1)

Use of nexttile function in MATLAB

USB interface powered Bluetooth color light strip controller

外包干了五年,废了...

VirtualLab基础实验教程-7.偏振(1)
![[how is the network connected] Chapter 4 explores access networks and network operators](/img/50/d16f4dca571a5a5f9b20fada289d45.png)
[how is the network connected] Chapter 4 explores access networks and network operators
![[target tracking] | data set summary](/img/2f/39a56d8cfb1638697735616b5e0412.png)
[target tracking] | data set summary
随机推荐
[非线性控制理论]8_三种鲁棒控制器的比较
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
【网络是怎么连接的】第四章 探索接入网和网络运营商
USB interface powered Bluetooth color light strip controller
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
智能水电表能耗监测云平台
嵌入式开发板 ~ 说明
Dstat use [easy to understand]
Virtual lab basic experiment tutorial -7 Polarization (1)
ceph 原理
阿里天池SQL学习笔记——DAY3
Chmod command principle and usage details [easy to understand]
pytorch支持32位吗?
em120.gige.h
The difference of message mechanism between MFC and QT
Wechat applet - arrows floating up and down
Use of nexttile function in MATLAB
Niuke JS2 file extension
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)
嵌入式 ~ 介绍