Php 若用户未登录,则将其重定向至登录,登录后将其重定向至上一个路由
我有一条受Php 若用户未登录,则将其重定向至登录,登录后将其重定向至上一个路由,php,laravel,Php,Laravel,我有一条受Auth::check()保护的路由。所以,如果它通过,用户可以访问它,如果没有-用户是redirect()->route('login') 当用户输入他的凭证时,我想将他重定向回他试图实现的路径 我的登录控制器: <?php namespace App\Http\Controllers\Auth; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Foundati
Auth::check()
保护的路由。所以,如果它通过,用户可以访问它,如果没有-用户是redirect()->route('login')代码>
当用户输入他的凭证时,我想将他重定向回他试图实现的路径
我的登录控制器:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
public function __construct()
{
session(['url.intended' => url()->previous()]);
$this->redirectTo = session()->get('url.intended');
$this->middleware('guest')->except('logout');
}
protected function credentials(Request $request)
{
$data = $request->only($this->username(), 'password');
$data['is_active'] = true;
return $data;
}
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
}
}
您只需使用这个
return Redirect::back();
如果你想重定向回一些消息或数据,你可以使用
return Redirect::back()->with();
在with()
中,您可以传递任何消息或数据,如
return Redirect::back()->with('message', 'Success');
这应该用于重定向到上一个位置back()
是Laravel辅助函数。此功能利用会话。您只需使用此功能
return Redirect::back();
如果你想重定向回一些消息或数据,你可以使用
return Redirect::back()->with();
在with()
中,您可以传递任何消息或数据,如
return Redirect::back()->with('message', 'Success');
这应该用于重定向到上一个位置back()
是Laravel辅助函数。此功能利用会话。您可以在您的中间件中使用此功能
if (Auth::check() && (Auth::user()->int_role_id == Roles::ROLE_USER)) {
return $next($request);
}else if (!Auth::check() ){
$url = Request()->path();
Session::put('loginRedirect', $url);
return redirect('/login');
}
在登录控制器中执行此操作
if(Session::get('loginRedirect')){
$url = Session::get('loginRedirect');
Session::forget('loginRedirect');
return Redirect::intended(url($url));
}
return Redirect::intended(route('user.get_home'));
更新请更新您的路线或URL
public function handle($request, Closure $next, $guard = null)
{
if (Auth::check()) {
return $next($request);
}else if (!Auth::check() ){
$url = Request()->path();
Session::put('loginRedirect', $url);
return redirect('YOUR LOGIN ROUTE OR URL');
}
}
登录控制器
public function redirectPath()
{
if(Session::get('loginRedirect')){
$url = Session::get('loginRedirect');
Session::forget('loginRedirect');
return Redirect::intended(url($url));
}
return Redirect::intended(route('YOUR INTENDED URL OR ROUTE after user is logged in'));
}
您可以在您的中间件中使用它
if (Auth::check() && (Auth::user()->int_role_id == Roles::ROLE_USER)) {
return $next($request);
}else if (!Auth::check() ){
$url = Request()->path();
Session::put('loginRedirect', $url);
return redirect('/login');
}
在登录控制器中执行此操作
if(Session::get('loginRedirect')){
$url = Session::get('loginRedirect');
Session::forget('loginRedirect');
return Redirect::intended(url($url));
}
return Redirect::intended(route('user.get_home'));
更新请更新您的路线或URL
public function handle($request, Closure $next, $guard = null)
{
if (Auth::check()) {
return $next($request);
}else if (!Auth::check() ){
$url = Request()->path();
Session::put('loginRedirect', $url);
return redirect('YOUR LOGIN ROUTE OR URL');
}
}
登录控制器
public function redirectPath()
{
if(Session::get('loginRedirect')){
$url = Session::get('loginRedirect');
Session::forget('loginRedirect');
return Redirect::intended(url($url));
}
return Redirect::intended(route('YOUR INTENDED URL OR ROUTE after user is logged in'));
}
如果您自己使用Auth::check()
保护路由,请使用以下方法:
return redirect()->guest(route('login')); // Don't use redirect()->route('login)
protected $redirectTo = '/home'; // this is default/fallback path
而不是redirect()->路由('login)
。然后,在成功登录中,使用:
return redirect()->intended('/home'); // home is the default/fallback url
您可能需要根据路由声明更改默认url
更新:(问题编辑后):
在应用程序\Http\Controllers\Auth\LoginController
中,只需使用以下命令:
return redirect()->guest(route('login')); // Don't use redirect()->route('login)
protected $redirectTo = '/home'; // this is default/fallback path
Laravel
使用illumb\Foundation\Auth\AuthenticatesUsers::sendLoginResponse
,它使用了预期的
方法,因此您无需执行任何其他操作(希望您使用的不是非常旧的版本)
通过OP
在您的重定向验证中,尝试以下操作:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
$user = Auth::user();
// Replace the ? marks with appropriate value
if ($user->int_role_id == ? || $user->int_role_id == ?) {
return redirect('/manage');
}
return redirect('/');
}
return $next($request);
}
如果您自己使用Auth::check()
保护路由,请使用以下方法:
return redirect()->guest(route('login')); // Don't use redirect()->route('login)
protected $redirectTo = '/home'; // this is default/fallback path
而不是redirect()->路由('login)
。然后,在成功登录中,使用:
return redirect()->intended('/home'); // home is the default/fallback url
您可能需要根据路由声明更改默认url
更新:(问题编辑后):
在应用程序\Http\Controllers\Auth\LoginController
中,只需使用以下命令:
return redirect()->guest(route('login')); // Don't use redirect()->route('login)
protected $redirectTo = '/home'; // this is default/fallback path
Laravel
使用illumb\Foundation\Auth\AuthenticatesUsers::sendLoginResponse
,它使用了预期的
方法,因此您无需执行任何其他操作(希望您使用的不是非常旧的版本)
通过OP
在您的重定向验证中,尝试以下操作:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
$user = Auth::user();
// Replace the ? marks with appropriate value
if ($user->int_role_id == ? || $user->int_role_id == ?) {
return redirect('/manage');
}
return redirect('/');
}
return $next($request);
}
请展示一些你已经尝试过的代码Ravel为你做开箱即用请展示一些你已经尝试过的代码Ravel为你做开箱即用我必须在每条路线上做?是否有一种最简单的方法来实现它?Y每个路由??只要登录正确,您将重定向您的用户登录,如果未登录,那么只需更新登录控制器并验证middlewareem,对不起,再次,第一个代码片段的确切位置?对不起,忘了提到这一点,请在中间件中使用该代码,重定向未经验证的中间件。因此,我必须在每条路线上做吗?是否有一种最简单的方法来实现它?Y每个路由??只要登录正确,如果未登录,您将重定向您的用户登录,因此只需更新登录控制器并验证middlewareem,抱歉,再次说明,第一个代码片段的确切位置?抱歉,忘了提及这一点,请在中间件中使用该代码,重定向未经验证的中间件。我没有得到它。我给这个问题添加了一些代码,请检查一下。谢谢不起作用。我还尝试了用于身份验证角色和权限的LaraTrust语法if($user->hasRole('superadministrator')|$$user->hasRole('administrator'))`而不是if($user->int|u role|id==?|$$user->int|u role|id==?)
而且,每次它重定向我只是返回()。所以,对我来说,函数似乎对这个过程没有任何影响。我通过在LoginController中重写AuthenticatedUsers
方法解决了这个问题,方法如下:protectedfunctionauthenticated(Request$Request$user){if($user->hasRole('superadministrator')| |$user->hasRole('administrator'){return redirect()->route('manage.dashboard');}}
很高兴知道你解决了这个问题。是的,这是另一种方法,成功登录后,authenticated
方法被调用。我没有得到它。我给这个问题添加了一些代码,请检查一下。谢谢不起作用。我还尝试了用于身份验证角色和权限的LaraTrust语法if($user->hasRole('superadministrator')|$$user->hasRole('administrator'))`而不是if($user->int|u role|id==?|$$user->int|u role|id==?)
而且,每次它重定向我只是返回()。所以,对我来说,函数似乎对这个过程没有任何影响。我通过在LoginController中重写AuthenticatedUsers
方法解决了这个问题,就像这样:protectedfunctionauthenticated(Request$Request$user){if($user->hasRole('superadministrator')| |$user->hasRole('administrator'){return redirect()->route('m')