Home  >  Article  >  PHP Framework  >  laravel framework login registration process

laravel framework login registration process

WBOY
WBOYOriginal
2023-05-29 09:58:071042browse

The Laravel framework is an excellent and popular web application development framework based on the PHP language. The Laravel framework has a very complete user login and registration system. This registration and login system can implement basic authentication functions for users, and can perform a series of authorization operations on users, such as: user rights management, password reset, email verification, etc. . In the following article, we will elaborate on the login and registration process of the Laravel framework.

  1. User registration process

First, we need to create a user table to save the user's basic information. We can create this model using the "make:model" command provided by Laravel. Enter the following command in the terminal:

php artisan make:model User -m

This command will generate a User model in the app directory and a users# in the database. ##surface. In the User model, we need to specify the authentication method used by the user. The Laravel framework provides a variety of user authentication methods, which we can set in the guard attribute in the model:

protected $guard = 'web';

After creating the user model, we need to create another one to display the user To register the controller of the page, we use the following command to create the controller:

php artisan make:controller AuthRegisterController --resource

After running this command, the Laravel framework will create a file named

in the appHttpControllersAuth directory. RegisterController.php file. In this controller, we need to implement the following method:

use AppUser;
use IlluminateFoundationAuthRegistersUsers;
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return redirect($this->redirectTo);
}

// 验证用户输入的数据
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

// 保存注册用户信息到数据库
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

Next, we need to add the route of this controller in

routes/web.php:

Route::get('register', 'AuthRegisterController@showRegistrationForm')->name('register');
Route::post('register', 'AuthRegisterController@register');

On the front page, we use the following form to collect user data:

<form method="POST" action="{{ route('register') }}">
    @csrf
    <div>
        <label for="name">用户名</label>
        <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus>
    </div>

    <div>
        <label for="email">邮箱</label>
        <input id="email" type="email" name="email" value="{{ old('email') }}" required>
    </div>

    <div>
        <label for="password">密码</label>
        <input id="password" type="password" name="password" required>
    </div>

    <div>
        <label for="password-confirm">确认密码</label>
        <input id="password-confirm" type="password" name="password_confirmation" required>
    </div>

    <div>
        <button type="submit">注册</button>
    </div>
</form>

Then we successfully create the user registration process.

    User login process
Laravel provides a very fast user login method, we can use this method to log in. Add the following route in

routes/web.php:

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

Then create a

LoginController controller corresponding to the User model. In the controller, we need to implement the following method:

use IlluminateFoundationAuthAuthenticatesUsers;
public function login(Request $request)
{
    $this->validateLogin($request);

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    return $this->sendFailedLoginResponse($request);
}

// 认证用户名和密码
protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
}

// 获取用户名和密码
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'password');
}

public function username()
{
    return 'email';
}

// 用户退出登录方法
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->invalidate();

    return redirect('/');
}

Next, we need to create a user login view. In the view, we use the following form to receive user data:

<form method="POST" action="{{ route('login') }}">
    @csrf
    <div>
        <label for="email">邮箱</label>
        <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus>
    </div>

    <div>
        <label for="password">密码</label>
        <input id="password" type="password" name="password" required>
    </div>

    <div>
        <input type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
        <label for="remember">记住我</label>
    </div>

    <div>
        <button type="submit">登录</button>
    </div>
</form>

After creating the above view and controller, we implemented the user login and registration process of the Laravel framework. What needs to be noted is that after the user successfully logs in, the user information needs to be stored in the session to facilitate subsequent function calls. In the second method of the controller, we can use the

attemptLogin method provided by the Laravel framework to verify the user's login information. If the verification is passed, the user information will be stored in the session and the user will be redirected to the child URL.

To sum up, the user login and registration process of the Laravel framework is very simple and easy to understand. It only requires a few simple lines of code to implement the user login and registration functions through the above steps. Of course, if users need more advanced authentication and permission management, they can also use the extensibility of the Laravel framework to add some custom methods based on the above models and controllers.

The above is the detailed content of laravel framework login registration process. 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
Previous article:laravel remove csrfNext article:laravel remove csrf