Home > Article > Backend Development > Steps to implement user registration and login functions using Laravel framework
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.
AuthController:
php artisan make:controller AuthControllerIn 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 routesIn 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:
<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 functionsNow, 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 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!