Laravel is a commonly used PHP framework with elegant syntax, powerful functions and rich documentation. It has become the framework of choice for many PHP developers. This article will introduce how to use the Laravel framework to implement registration and login functions.
1. Create a Laravel application
Before starting to implement the registration and login functions, you first need to create a Laravel application. You can use the Composer command officially provided by Laravel to create a new application, as follows:
composer create-project --prefer-dist laravel/laravel your-project-name
where your-project-name
is the name of the application you want to create.
After the creation is completed, enter the application directory and start the local server:
cd your-project-name php artisan serve
Enter http://localhost:8000
in the browser to access the application welcome page.
2. Create an authentication system
Before implementing the registration and login functions, you need to create a basic authentication system first. Laravel provides the make:auth
Artisan command to quickly generate authentication-related views and controllers.
php artisan make:auth
After executing the above command, Laravel will automatically create register
, login
and logout
related views and controllers, and add them added to the application. Additionally, Laravel will create relevant user and password reset tables in the database.
3. Create a database table
By default, Laravel uses the MySQL database. In this example, you need to create a data table named users
to store user data. The table can be created using the following Artisan command:
php artisan make:migration create_users_table --create=users
After executing the above command, Laravel will create a file named create_users_table
in the database/migrations
directory of the application. migration file. Open the file and modify the up
method as follows:
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(); }); }
The above migration file defines the fields of the users
data table, including name
(username), email
(email address), password
(password), etc.
After modifying the migration file, you need to execute the following Artisan command to actually create the users
data table:
php artisan migrate
4. Create the registration page
Next, You need to create a registration page that allows users to enter their username, email address, and password. Open the resources/views/auth/register.blade.php
file, and you can see that Laravel has created a basic registration form for us.
At this point, we need to delete some fields and add the name
field and password confirmation password_confirmation
field that must be filled in. The modified code is as follows:
<form method="POST" action="{{ route('register') }}"> @csrf <div> <label for="name">{{ __('Name') }}</label> <div> <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus> </div> </div> <div> <label for="email">{{ __('E-Mail Address') }}</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required> </div> </div> <div> <label for="password">{{ __('Password') }}</label> <div> <input id="password" type="password" name="password" required> </div> </div> <div> <label for="password-confirm">{{ __('Confirm Password') }}</label> <div> <input id="password-confirm" type="password" name="password_confirmation" required> </div> </div> <div> <button type="submit"> {{ __('Register') }} </button> </div> </form>
5. Processing the registration request
Now, the registration form has been created, but it will not take effect when the user submits the form. So we need to modify the app/Http/Controllers/Auth/RegisterController.php
file to handle registration requests.
public function store(Request $request) { $this->validate($request, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|unique:users|max:255', 'password' => 'required|string|min:8|confirmed', ]); User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), ]); return redirect()->intended('dashboard'); }
The above code defines a method named store()
, which is used to handle registration requests. First, the validate()
method will verify the requested data. If the verification passes, use the User
model to create a new user and encrypt the password. Finally, redirect to the application's dashboard page.
6. Create a login page
Next, you need to create a login page so that users can enter their registered email address and password. Open the resources/views/auth/login.blade.php
file, and you can see that Laravel has created a basic login form for us.
7. Processing login requests
The registration form has been created, but it will not take effect when the user submits the form. So we need to modify the app/Http/Controllers/Auth/LoginController.php
file to handle login requests.
public function store(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended('dashboard'); } return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); }
The above code defines a method named store()
, which is used to handle login requests. First, the attempt()
method attempts to authenticate using the credentials provided by the user. If the verification is successful, a new session is generated and the user is redirected to the application's dashboard page. If this fails, the back()
method is used to redirect the user back to the original login page with an error message.
8. Set up authentication middleware
Once the registration and login functions have been implemented, certain routes of the application need to be protected so that only authenticated users can access them. In Laravel, you can use middleware to achieve this. Laravel provides a middleware called auth
, which verifies that the user is logged in to the application.
Laravel by default applies the auth
middleware to any route using the auth
route name. One or more middlewares can be specified using the following code:
Route::get('/dashboard', function () { // 仅允许已登录用户访问 })->middleware(['auth']);
Now only authenticated users can access the /dashboard
route.
Summary
This article introduces how to use the Laravel framework to implement registration and login functions. With the help of the Laravel framework, you can quickly and easily build a safe and reliable web application and promote the web application development process. Whether you are a beginner or an experienced developer, you can try using the Laravel framework to build your own web applications.
The above is the detailed content of laravel implements registration and login. For more information, please follow other related articles on the PHP Chinese website!