首頁  >  文章  >  php框架  >  如何使用Laravel 5來實現登陸註冊功能

如何使用Laravel 5來實現登陸註冊功能

PHPz
PHPz原創
2023-04-21 10:06:24634瀏覽

Laravel是一種基於PHP語言的開源Web框架,被越來越多的開發者使用。 Laravel框架內建了許多有用的功能,如視圖、路由、資料庫操作等,同時也支援拓展。本文將介紹如何使用Laravel 5來實現登陸註冊功能。

  1. 安裝Laravel

首先需要在系統中安裝Laravel。安裝方法可以參考Laravel官方文件或自行搜尋。

  1. 建立資料庫

登陸註冊功能需要使用資料庫來儲存使用者資訊。在此之前需要建立一個名為「laravel_login」的資料庫。可以在命令列中輸入以下指令:

mysql -u username -p
CREATE DATABASE laravel_login;

其中,「username」是你的MySQL使用者名稱。這將會建立一個空的名為「laravel_login」的資料庫。

  1. 建立使用者表

在Laravel 5中,透過遷移來建立和管理資料庫表。首先需要產生一個遷移檔案。在命令列中輸入:

php artisan make:migration create_users_table

這個命令將會產生一個遷移文件,預設會保存在「database\migrations」資料夾中。

編輯這個遷移文件,在「up()」方法中定義使用者表的欄位:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

然後輸入以下命令以執行這個遷移並產生使用者表:

php artisan migrate
  1. 建立控制器和視圖

接下來建立控制器和視圖,用於顯示註冊和登陸頁面,並處理使用者輸入的資訊。在命令列中輸入:

php artisan make:controller AuthController

這個指令將會建立一個名為「AuthController」的控制器,預設會被儲存在「app\Http\Controllers」資料夾中。

控制器程式碼如下:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function showLoginForm()
    {
        return view(&#39;auth.login&#39;);
    }

    public function login(Request $request)
    {
        // 登陆逻辑
    }

    public function showRegistrationForm()
    {
        return view(&#39;auth.register&#39;);
    }

    public function register(Request $request)
    {
        // 注册逻辑
    }

    public function logout(Request $request)
    {
        // 注销逻辑
    }
}

然後需要建立視圖模板,用於渲染表單。在「resources\views\auth」資料夾中建立名為「login.blade.php」和「register.blade.php」的文件,分別是登陸和註冊的頁面模板。

範本程式碼如下:

登陸範本:

@extends(&#39;layouts.app&#39;)

@section(&#39;content&#39;)
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Login</div>
                    <div class="panel-body">
                        <form class="form-horizontal" method="POST" action="{{ route(&#39;login&#39;) }}">
                            {{ csrf_field() }}

                            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                                <div class="col-md-6">
                                    <input id="email" type="email" class="form-control" name="email" value="{{ old(&#39;email&#39;) }}" required autofocus>

                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('email') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                <label for="password" class="col-md-4 control-label">Password</label>

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

                                    @if ($errors->has('password'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('password') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-4">
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" name="remember" {{ old(&#39;remember&#39;) ? &#39;checked&#39; : &#39;&#39; }}> Remember Me
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-8 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        Login
                                    </button>

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

註冊範本:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Register</div>
                    <div class="panel-body">
                        <form class="form-horizontal" method="POST" action="{{ route(&#39;register&#39;) }}">
                            {{ csrf_field() }}

                            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                                <label for="name" class="col-md-4 control-label">Name</label>

                                <div class="col-md-6">
                                    <input id="name" type="text" class="form-control" name="name" value="{{ old(&#39;name&#39;) }}" required autofocus>

                                    @if ($errors->has('name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('name') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                                <div class="col-md-6">
                                    <input id="email" type="email" class="form-control" name="email" value="{{ old(&#39;email&#39;) }}" required>

                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('email') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                <label for="password" class="col-md-4 control-label">Password</label>

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

                                    @if ($errors->has('password'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('password') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="password-confirm" class="col-md-4 control-label">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">
                                <div class="col-md-6 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        Register
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
  1. 完成路由

最後一步是設定路由,以便Laravel可以正確呼叫控制器和視圖。在「routes\web.php」檔案中加入以下程式碼:

Route::get('login', 'AuthController@showLoginForm')->name('login');
Route::post('login', 'AuthController@login');
Route::get('register', 'AuthController@showRegistrationForm')->name('register');
Route::post('register', 'AuthController@register');
Route::post('logout', 'AuthController@logout')->name('logout');

現在你可以造訪以下位址來查看註冊和登陸的頁面:

http://yourdomain.com/register
http://yourdomain.com/login

以及以下位址來完成登陸和登出操作:

http://yourdomain.com/login
http://yourdomain.com/logout
  1. 結束語

透過本教學,你學會如何在Laravel 5中實現登陸註冊的功能。當然,這只是最基本的實作方式。在操作、安全性方面等等,還有很多要注意的地方,需要在實際開發中不斷學習和改進。

以上是如何使用Laravel 5來實現登陸註冊功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn