Home  >  Article  >  PHP Framework  >  laravel login default jump

laravel login default jump

WBOY
WBOYOriginal
2023-05-20 19:03:37485browse

Laravel is a very popular PHP framework that provides many convenient functions and powerful functions to help developers develop web applications faster and more efficiently. One of the powerful features is the user login system. In Laravel, you can quickly create a user authentication system to log users into your application. Many common user authentication functions such as registration, login, password reset, etc. can be easily implemented using simple codes.

When a user successfully logs into your application, Laravel will redirect the user to the root directory of the application by default. However, sometimes you want users to be redirected to a specific page after logging in, such as the user's profile or control panel. In this article, we will explain how to set the default login redirect URL in Laravel.

First, let’s take a look at how Laravel’s user authentication system works. Laravel uses guard by default to manage user authentication, and the "web" guard is the guard used by Laravel by default. Before using guard, you need to configure it in the config/auth.php file. In this file, you can define different guards and their configuration. For example, below is an example of a config/auth.php file that defines two guards: "web" and "admin".

return [

    'guards' => [

        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

    ],

    'providers' => [

        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => AppModelsAdmin::class,
        ],

    ],

];

In the above example, we can see that both the "web" guard and the "admin" guard use the session driver for authentication, where the "web" guard uses the "users" provider for user authentication, and the "admin" ” guard uses the “admins” provider for administrator authentication. In addition, we can also configure the default redirect URL of each guard in the config/auth.php file. For example, here is an example of using the default redirect URL:

'guards' => [

        'web' => [
            'driver' => 'session',
            'provider' => 'users',
            'redirect' => '/',
        ],

    ],

In the above example, we set the default redirect URL of the "web" guard to "/", which is the root directory of the application. When the user successfully logs in, they will be redirected to this URL. Likewise, you can change the redirect URL if needed, for example, to the user's profile or control panel. You just need to change the 'redirect' option in guard's configuration to the URL that needs to be redirected.

However, if you have multiple guards in your application, such as the "web" and "admin" guards in the example above, how to set the default redirect URL? In this case, Laravel provides a simple solution that allows you to easily set the default redirect URL for each guard.

By default, Laravel provides a LoginController in the app/Http/Controllers/Auth/LoginController.php file for managing user logins. You can set the default redirect URL for each guard in this controller. For example, here is an example of changing the default redirect URL for "web" guard and "admin" guard:

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function redirectTo()
    {
        if (auth()->user()->isAdmin()) {
            return '/admin/dashboard';
        } else {
            return '/home';
        }
    }
}

In the above example, we override the redirectTo() method in LoginController and redirect The user's guard returns a different redirect URL. If the current user's guard is "admin", it will redirect to the administrator control panel, otherwise it will redirect to the user's personal homepage. You can change the redirect URL according to your needs.

It should be noted that the redirectTo() method can be used to redirect users only if the 'redirect' option is set in guards. If you do not set the 'redirect' option in the config/auth.php file, you need to use the default redirect URL. Additionally, redirectTo() method overriding is inherently different from directly changing the 'redirect' option. In the redirectTo() method you can write any logic code to achieve fine control of the redirect URL.

In general, setting Laravel's default login redirect URL is very simple. You can set this through the config/auth.php file or the redirectTo() method in LoginController. If your application has multiple guards, you can fine-grained control of each guard's default redirect URL in the LoginController. These methods give you more control over the user experience and the flow of your application, making them smoother and more user-friendly.

The above is the detailed content of laravel login default jump. 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