栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8

博文主要是使用Laravel8创建自定义登录和注册。

使用传统简单方法创建自定义用户登录、注册、面板页面。

案例

有如下步骤:

①创建Laravel应用;

②连接MySQL数据库;

③设置auth的Controller;

④创建auth路由;

⑤创建auth的blade视图文件;

⑥启动Laravel服务。

创建Laravel应用

安装陪配置好composer后,使用下面的命令创建Laravel项目。

composer create-project --prefer-dist laravel/laravel laravel_new 

进入创建应用的目录。

cd laravel_new 

连接数据库

让Laravel连接mysql数据库,打开.env配置文件,修改数据库名,用户名,密码:

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提供了默认的用户模型和迁移文件,所以可以用php命令通过默认给的迁移文件创建mysql表。

php artisan migrate 

设置权限Controller

调用下面的命令,创建Controller

php artisan make:controller CustomAuthController

在appHttpControllersCustomAuthController.php替换成如下代码:

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');
    }
} 

创建Auth路由

在routes/web.php中添加如下代码:

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

创建auth的blade视图文件

在resources/views中创建login.blade.php:

@extends('app')

@section('content')

Login
@csrf @if ($errors->has('email')) {{ $errors->first('email') }} @endif @if ($errors->has('password')) {{ $errors->first('password') }} @endif

@endsection

随后创建registration.blade.php在resources/views/auth目录下:

@extends('app')

@section('content')

Registration
@csrf @if ($errors->has('name')) {{ $errors->first('name') }} @endif @if ($errors->has('email')) {{ $errors->first('email') }} @endif @if ($errors->has('password')) {{ $errors->first('password') }} @endif

@endsection

下一步在resources/views中创建dashborad.blade.php文件:

@extends('layouts.app')

@section('content')
    
@endsection 

最后创建resources/views/app.blade.php文件:




    
    
    
    {<!-- -->{config('app.name','LSAPP')}}
    


       @yield('content')

运行laravel服务
php artisan serve 

注册URL和登录URL分别为:

http://127.0.0.1:8000/registration
http://127.0.0.1:8000/login 

输出:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号