laravel框架之自帶登錄&註冊

//控制器層php

<?php

namespace App\Http\Controllers\admin;

use App\Models\admin\Users;
use Illuminate\Http\Request as request;
use App\Http\Controllers\Controller;
use App\Http\Requests\register;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
public function login()
{
return view('admin.login');
}
public function login_do(request $request){
$model = new Users();
if ($model->lo($request->only('email','password'))){
return redirect()->route('admin.showlist')->with('msg','登錄成功');
}
}
public function register()
{
return view('admin.register');
}
public function register_do(register $request)
{
$model = new Users();
if ($model->zhuce($request->all())){
return redirect()->route('admin.login')->with('msg','用戶已註冊成功');
}
}

//模型層css

<?php

namespace App\Models\admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authors;

class Users extends Authors
{
public $table = "users";
public function zhuce($data)
{
$this->username =$data['username'];
$this->password =bcrypt($data['password']);
$this->email =$data['email'];
return $this->save();
}
public function lo($data)
{
if (auth()->attempt($data)){
return auth()->user();
}
}
}

//視圖層html

//註冊頁面laravel

@extends('layouts.app')
@section('title','註冊頁面')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>

<div class="card-body">
<form method="POST" action="{{ route('admin.register_do') }}" aria-label="{{ __('Register') }}">
@csrf

<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('UserName') }}</label>

<div class="col-md-6">
<input id="username" type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" name="username" value="{{ old('username') }}" required autofocus>

@if ($errors->has('username'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>

<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
//登錄頁面
@extends('layouts.app')
@section('title','登錄頁面')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ session('msg' ?? 'Login') }}</div>

<div class="card-body">
<form method="POST" action="{{ route('admin.login_do') }}" aria-label="{{ __('Login') }}">
@csrf

<div class="form-group row">
<label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>

<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>

<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

//佈局頁面 layouts
<!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">    <!-- CSRF Token -->    <meta name="csrf-token" content="{{ csrf_token() }}">    <title>@yield('title')</title>    <!-- Scripts -->    <script src="{{ asset('js/app.js') }}" defer></script>    <!-- Fonts -->    <link rel="dns-prefetch" href="https://fonts.gstatic.com">    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">    <!-- Styles -->    <link href="{{ asset('css/app.css') }}" rel="stylesheet"></head><body>    <div id="app">        <nav class="navbar navbar-expand-md navbar-light navbar-laravel">            <div class="container">                <a class="navbar-brand" href="{{ url('/') }}">                    {{ config('app.name', 'Laravel') }}                </a>                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">                    <span class="navbar-toggler-icon"></span>                </button>                <div class="collapse navbar-collapse" id="navbarSupportedContent">                    <!-- Left Side Of Navbar -->                    <ul class="navbar-nav mr-auto">                    </ul>                    <!-- Right Side Of Navbar -->                    <ul class="navbar-nav ml-auto">                        <!-- Authentication Links -->                        @guest                            <li class="nav-item">                                <a class="nav-link" href="{{ route('admin.login') }}">{{ __('Login') }}</a>                            </li>                            <li class="nav-item">                                <a class="nav-link" href="{{ route('admin.register') }}">{{ __('Register') }}</a>                            </li>                        @else                            <li class="nav-item dropdown">                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>                                    {{ Auth::user()->name }} <span class="caret"></span>                                </a>                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">                                    <a class="dropdown-item" href="{{ route('logout') }}"                                       onclick="event.preventDefault();                                                     document.getElementById('logout-form').submit();">                                        {{ __('Logout') }}                                    </a>                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">                                        @csrf                                    </form>                                </div>                            </li>                        @endguest                    </ul>                </div>            </div>        </nav>        <main class="py-4">            @yield('content')        </main>    </div></body></html>
相關文章
相關標籤/搜索