Home >PHP Framework >Laravel >How to redirect users who are not logged in in Laravel

How to redirect users who are not logged in in Laravel

PHPz
PHPzOriginal
2023-04-07 17:02:431007browse

Laravel is one of the most popular PHP development frameworks today. It provides many useful features and tools that can help us quickly develop and test web applications. One of the important functions is the user authentication (Authentication) system. In most applications, we need to ensure that only authenticated users can access sensitive pages and functionality. This article will introduce how to implement non-logged-in user jump in Laravel and provide you with some useful tips and suggestions.

Use middleware to redirect unlogged users

In Laravel, we can use middleware to handle requests. Middleware helps us perform certain actions before or after the request reaches the controller. In our application we can use middleware to check if the user is already logged in. If the user is not logged in, we can redirect them to the login page.

First, let's set up a middleware called guest. We can create it using the Artisan command php artisan make:middleware GuestMiddleware. Then, we can write code in the guest middleware to check if the user is logged in:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }
    return $next($request);
}

Here, we use Auth::guard($guard)->check() to check if the user is logged in . If so, we will redirect the user to the /home page. If not, control is passed to the next middleware or controller action.

Next, we can use guest middleware in the route definition. For example, we can define the following route in the web.php file:

Route::group(['middleware' => ['guest']], function () {
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
});

Here, we apply the guest middleware to the entire route group. This means that when accessing /login and submitting the login form, the guest middleware will check whether the user is already logged in. If the user is already logged in, they will be redirected to the /home page. Otherwise, controller operation will continue.

Use authentication controller to jump to non-logged in users

Another way to jump to non-logged in users is to use traits in the authentication controller. In Laravel, we can use some traits to easily implement user authentication functions. One of these traits is Illuminate\Foundation\Auth\AuthenticatesUsers. This trait contains operations for logged in and logged out users, as well as redirection logic.

First, let's create a controller called CustomLoginController and use the AuthenticatesUsers trait:

use Illuminate\Foundation\Auth\AuthenticatesUsers;

class CustomLoginController extends Controller
{
    use AuthenticatesUsers;
}

Here, we make the CustomLoginController use the AuthenticatesUsers trait.

Next, we can override the showLoginForm method and authenticated method in the AuthenticatesUsers trait. In these methods, we can specify the login page and redirect logic:

use Illuminate\Foundation\Auth\AuthenticatesUsers;

class CustomLoginController extends Controller
{
    use AuthenticatesUsers;

    protected function showLoginForm()
    {
        return view('auth.login');
    }

    protected function authenticated(Request $request, $user)
    {
        return redirect()->route('home');
    }
}

Here, we override the showLoginForm method to return the view "auth.login". We override the authenticated method to redirect to route "home".

Finally, we can define the following routes in the web.php file:

Route::get('login', 'CustomLoginController@showLoginForm')->name('login');
Route::post('login', 'CustomLoginController@login');
Route::get('home', function () {
    return view('home');
})->name('home');

Here, we define the GET route for the login page, the POST route for submitting the login form, and the GET route for the homepage . In CustomLoginController, the showLoginForm and authenticated methods we wrote will handle the login form and redirection logic.

Conclusion

In this article, we introduced how to implement non-logged-in user jump in Laravel. There are two ways to achieve this: using middleware and using an authentication controller. No matter which method you choose, you can protect your sensitive pages and features by ensuring only authenticated users can access them. If you are developing a Laravel application, these tips and advice should be very useful to you.

The above is the detailed content of How to redirect users who are not logged in in Laravel. 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