laravel学习笔记--登录,注册页面

用户登录

login.blade.php

<body>
    <div class="login">
        <form action="{
    
    {url('loginCheck')}}" method="post">
            <h2>用户登录</h2>

            <div>
                <label for="username">用户:</label>
                <input id="username" type="text" name="username" placeholder="请输入用户名" required>
            </div>
            <div>
                <label for="password">密码:</label>
                <input id="password" type="password" name="password" placeholder="请输入密码" required>

            </div>
            <div>
                <input type="submit" value="登录">
                <input type="reset">
            </div>

            @csrf
        </form>
    </div>

    <style>
        body{
    
    
            background-color: lightgoldenrodyellow;
        }
        body *{
    
    
            box-sizing: border-box;


        }
        .login form{
    
    
            display: flex;
            flex-direction: column;

            width: 340px;
            background-color: lightblue;
            margin: 50px auto;
            padding:10px 40px;
            box-shadow: 0px 3px 6px #888;
        }
        .login h2{
    
    
            text-align: center;
            color: red;
        }
       /* .login div{
            text-align: center;
        } */
        .login *{
    
    
            margin-bottom: 10px;
        }
        form div:last-of-type{
    
    
            /*margin-left: 80px;*/
        }
        form div:last-of-type input{
    
    
            margin-left: 55px;
        }
        .login label{
    
    
            display: inline-block;
            width: 50px;
            text-align: right;
        }
    </style>

    @if(!empty($data['message']))
        <script>alert("{
    
    {
      
      $data['message']}}")</script>
    @endif
</body>

路由

Route::get('/login',[UserController::class,'login']);
 Route::post('/loginCheck',[UserController::class,'loginCheck']);

app/Http/Controllers/UserController

方法一:login登录

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
    
    
public function login(){
    
    
        return view('login');
    }
       
    }
}

在这里插入图片描述
方法二:loginCheck 登录检查

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
    
    
 public function login(){
    
    
        return view('login');
    }
 public function loginCheck(Request $request){
    
    
        $data = $request->all();
        $username=$request->input('username');
        
         dd($data,$username);
        
       
    }
}

在这里插入图片描述

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
    
    
 public function login(){
    
    
        return view('login');
    }
 public function loginCheck(Request $request){
    
    
        $data = $request->all();
        $username=$request->input('username');
        $password=$request->input('password');
        // dd($data,$username);
        $res=DB::table('users')
        ->where('name',$username)
        ->where('password',$password)
        ->first();
        dd($res);
       
    }
}

在这里插入图片描述
请添加图片描述
如果输入数据库中没有保存得数据 则显示空(用户名 密码错误 也显示空)请添加图片描述
请添加图片描述

用户注册

regist.blade.php

<body>
<div class="regist">
    <form action="{
    
    {url('/user')}}" method="post" onsubmit="return false">
        @method('put')
        <h2>用户注册</h2>

        <div>
            <label for="username">用户名:</label>
            <input id="username" type="text" name="username" placeholder="请输入用户名" required>
        </div>
        <div>
            <label for="email">邮箱:</label>
            <input id="email" type="email" name="email" placeholder="请输入电子邮箱" required>
        </div>
        <div>
            <label for="password">密码:</label>
            <input id="password" type="password" name="password" placeholder="请输入密码" required>

        </div>
        <div>
            <label for="repwd">确认密码:</label>
            <input id="repwd" type="password" name="repwd" placeholder="请再次输入密码" required>

        </div>
        <div>
            <input type="submit">
            <input type="reset">
        </div>

        @csrf
    </form>
</div>

<style>
    body{
    
    
        background-color: lightgoldenrodyellow;
    }
    body *{
    
    
        box-sizing: border-box;

    }
    .regist form{
    
    
        display: flex;
        flex-direction: column;

        width: 400px;
        background-color: lightblue;
        margin: 50px auto;
        padding:10px 40px;
        box-shadow: 0px 3px 6px #888;
    }
    .regist h2{
    
    
        text-align: center;
        color: red;
    }
    .regist *{
    
    
        margin-bottom: 10px;
    }
    form div:last-of-type{
    
    
        margin-left: 80px;
    }
    form div:last-of-type input{
    
    
        margin-left: 40px;
    }
    form label{
    
    
        display: inline-block;
        width: 80px;
        text-align: right;
    }
</style>
</body>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lj1641719803hh/article/details/124475061