Home  >  Article  >  PHP Framework  >  How to use Laravel 5 to implement login registration function

How to use Laravel 5 to implement login registration function

PHPz
PHPzOriginal
2023-04-21 10:06:24632browse

Laravel is an open source web framework based on the PHP language and is used by more and more developers. The Laravel framework has many useful built-in functions, such as views, routing, database operations, etc., and also supports expansion. This article will introduce how to use Laravel 5 to implement the login and registration function.

  1. Installing Laravel

First you need to install Laravel in the system. For installation methods, you can refer to Laravel official documentation or search by yourself.

  1. Create database

The login and registration function requires the use of a database to store user information. Before doing this, you need to create a database named "laravel_login". You can enter the following command on the command line:

mysql -u username -p
CREATE DATABASE laravel_login;

where "username" is your MySQL username. This will create an empty database named "laravel_login".

  1. Creating user tables

In Laravel 5, create and manage database tables through migrations. First you need to generate a migration file. Enter in the command line:

php artisan make:migration create_users_table

This command will generate a migration file, which will be saved in the "database\migrations" folder by default.

Edit this migration file and define the fields of the user table in the "up()" method:

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

Then enter the following command to run this migration and generate the user table:

php artisan migrate
  1. Create controllers and views

Next create controllers and views to display the registration and login pages and process the information entered by the user. Enter in the command line:

php artisan make:controller AuthController

This command will create a controller named "AuthController", which will be saved in the "app\Http\Controllers" folder by default.

The controller code is as follows:

<?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)
    {
        // 注销逻辑
    }
}

Then you need to create a view template for rendering the form. Create files named "login.blade.php" and "register.blade.php" in the "resources\views\auth" folder, which are login and registration page templates respectively.

The template code is as follows:

Login template:

@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

Registration template:

@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. Complete routing

The last step is to set up the routing so that Laravel can call the controller and view correctly. Add the following code to the "routes\web.php" file:

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

Now you can visit the following address to view the registration and login page:

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

and the following address to complete login and logout Operation:

http://yourdomain.com/login
http://yourdomain.com/logout
  1. Conclusion

Through this tutorial, you have learned how to implement the login and registration function in Laravel 5. Of course, this is just the most basic implementation. In terms of operation, security, etc., there are still many things to pay attention to, which need to be continuously learned and improved in actual development.

The above is the detailed content of How to use Laravel 5 to implement login registration function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn