Home > Article > PHP Framework > laravel jump after login
When developing web applications, user login authentication is an essential function. The Laravel framework provides a variety of ways to implement user authentication, and also provides a default identity authentication system (namely LaravelIlluminateAuth) to facilitate developers to implement user registration, login, logout and other functions in the application.
After successful login authentication, we often want to jump to a specific page, such as the user's profile page or a specific function page. In the Laravel framework, it is very simple to implement a jump after login.
This article will introduce several ways for users to jump after logging in in the Laravel framework.
The identity authentication system provided by LaravelIlluminateAuth configures a jump after user login by default. In the config/auth.php configuration file, there is the following default configuration:
'redirect' => [ 'login' => '/login', 'logout' => '/logout', 'home' => '/home', 'register' => '/register', 'verify' => '/email/verify', 'reset' => '/password/reset', 'confirm' => '/password/confirm', ],
Among them, 'home' represents the jump page after login, and the default is the /home path. If you need to modify the default jump page, you only need to modify the path to the page you want.
If you need to manually specify the jump page after login in the controller, we can use the RedirectResponse instance provided by the Laravel framework and implement it through the redirect() method.
For example, in the user controller, we can override the authenticated() method in the IlluminateFoundationAuthAuthenticatesUsers trait:
use IlluminateSupportFacadesAuth; class UserController extends Controller { use AuthenticatesUsers; protected function authenticated(Request $request, $user) { return redirect()->route('user.show', $user->id); } }
The above code can jump to the specified user after the user logs in successfully. Information page.
Sometimes, we need to set the jump path after the user logs in to the page before logging in. You can use the session() function and URL provided by Laravel:: previous() method.
For example, in the login controller, we can implement it like this:
use IlluminateHttpRequest; use IlluminateSupportFacadesAuth; use IlluminateSupportFacadesURL; class LoginController extends Controller { public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended(URL::previous()); } return back()->withErrors(['email' => '登录失败']); } }
In the above code, we use the redirect()->intended() method, which will Redirect to the page you visited before logging in. If the user has not visited other pages before, they will be redirected to the default login jump path.
Laravel framework middleware provides convenient identity authentication and authorization functions. We can specify the jump path after login in a middleware.
For example, we can configure the jump path after login in the auth middleware:
namespace AppHttpMiddleware; use IlluminateAuthMiddlewareAuthenticate as Middleware; class Authenticate extends Middleware { protected function redirectTo($request) { if (! $request->expectsJson()) { return route('login'); // 设置默认的跳转路径 } } }
In the above code, we use the redirectTo() method to handle failed login requests. If json format data is expected to be returned during the request, a 401 error will be returned directly; otherwise, the user will be redirected to the login page.
If you need to specify other jump paths, you only need to modify the routing alias in the return statement.
The above are several ways to implement jump after user login in the Laravel framework. The specific method chosen depends on the developer's actual needs and development scenarios. No matter which method is used, it can help us realize the user authentication function and jump after login conveniently and quickly.
The above is the detailed content of laravel jump after login. For more information, please follow other related articles on the PHP Chinese website!