Home  >  Article  >  Backend Development  >  Steps to implement user registration and login functions using Laravel framework

Steps to implement user registration and login functions using Laravel framework

WBOY
WBOYOriginal
2023-07-28 15:17:101023browse

Steps to implement user registration and login functions using the Laravel framework

Laravel is a popular PHP development framework that provides many powerful functions and tools, allowing developers to easily build various web applications. User registration and login are one of the basic functions of any application. Below we will use the Laravel framework to implement these two functions.

Step 1: Create a new Laravel project

First, we need to create a new Laravel project on the local computer. Open a terminal or command line window, navigate to your working directory and execute the following command:

composer create-project --prefer-dist laravel/laravel user-auth

This will create a new Laravel project in a folder named user-auth. Enter the project directory:

cd user-auth

Step 2: Configure database connection

The application requires a database to store user registration information and login credentials. Open the .env file and configure the database connection information. Find the following line:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Modify these values ​​according to your database settings. Make sure you have created an empty database.

Step 3: Create user model and database migration

Laravel's migration function allows us to easily create and manage database tables through the command line. Execute the following command to create a migration named users:

php artisan make:migration create_users_table --create=users

This will create a new migration file in the database/migrations directory. Open the file and define the structure of the user table as required, like this:

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

Then run the migration command to create the table:

php artisan migrate

This will create a new file called ## in your database The table of #users.

Step 4: Create the registration and login controller

Next, we need to create the controller that handles user registration and login. Execute the following command to create a controller named

AuthController:

php artisan make:controller AuthController

In the

app/Http/Controllers directory, find and open AuthController.php . Add the following two methods:

public function register(Request $request)
{
    $request->validate([
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6',
    ]);

    $user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]);

    Auth::login($user);

    return redirect('/home');
}

public function login(Request $request)
{
    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
    ]);

    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        return redirect('/home');
    }

    return redirect()->back()->withErrors([
        'msg' => '登录失败,请检查您的凭证信息.',
    ]);
}

Step 5: Create registration and login routes

In the

routes/web.php file, add the following route definition:

Route::post('/register', 'AuthController@register');
Route::post('/login', 'AuthController@login');

This will create corresponding routes for the registration and login functions.

Step 6: Create registration and login views

In the

resources/views directory, create two view files register.blade.php and login.blade.php. View files can be customized to suit your design needs, here is a simple example:

register.blade.php:

<form action="/register" method="POST">
    @csrf

    <input type="text" name="name" placeholder="姓名">
    <input type="email" name="email" placeholder="邮箱">
    <input type="password" name="password" placeholder="密码">
    <button type="submit">注册</button>
</form>

login.blade.php:

<form action="/login" method="POST">
    @csrf

    <input type="email" name="email" placeholder="邮箱">
    <input type="password" name="password" placeholder="密码">
    <button type="submit">登录</button>
</form>

Step 7: Test the user registration and login functions

Now, we have completed the implementation of the user registration and login functions. You can test the registration function by visiting

http://localhost/register, and test the login function by visiting http://localhost/login.

The above are the steps to implement user registration and login functions using the Laravel framework. Laravel provides a simple yet powerful set of tools to help you build various web applications easily.

The above is the detailed content of Steps to implement user registration and login functions using Laravel framework. 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