在 laravel 背包中登录后有条件地重定向用户
Posted
技术标签:
【中文标题】在 laravel 背包中登录后有条件地重定向用户【英文标题】:Conditionally redirect users after login in laravel backpack 【发布时间】:2019-10-23 04:29:12 【问题描述】:我为用户分配了一些角色。我希望在登录后将用户重定向到不同的 url,以便用户所属的角色。 我已经关注了这篇文章,但它对我没有用。不知道用背包会不会不一样。
最好的问候。
编辑。
这是登录控制器中的代码。
<?php
namespace Backpack\Base\app\Http\Controllers\Auth;
use Backpack\Base\app\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
protected $data = []; // the information we send to the view
protected $redirectTo = '/home';
/*
|--------------------------------------------------------------------------
| 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
logout as defaultLogout;
protected function authenticated(Request $request, $user)
/*if ( $user->isAdmin() ) // do your margic here
return redirect('/home1');
*/
return redirect('/myhome');
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$guard = backpack_guard_name();
$this->middleware("guest:$guard", ['except' => 'logout']);
// ----------------------------------
// Use the admin prefix in all routes
// ----------------------------------
// If not logged in redirect here.
$this->loginPath = property_exists($this, 'loginPath') ? $this->loginPath
: backpack_url('login');
// Redirect here after successful login.
$this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo
: backpack_url('dashboard');
// Redirect here after logout.
$this->redirectAfterLogout = property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout
: backpack_url();
/**
* Return custom username for authentication.
*
* @return string
*/
public function username()
return backpack_authentication_column();
/**
* Log the user out and redirect him to specific location.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
// Do the default logout procedure
$this->guard()->logout();
// And redirect to custom location
return redirect($this->redirectAfterLogout);
/**
* Get the guard to be used during logout.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
return backpack_auth();
// -------------------------------------------------------
// Laravel overwrites for loading backpack views
// -------------------------------------------------------
/**
* Show the application login form.
*
* @return \Illuminate\Http\Response
*/
public function showLoginForm()
$this->data['title'] = trans('backpack::base.login'); // set the page title
$this->data['username'] = $this->username();
return view('backpack::auth.login', $this->data);
如果我把这段代码放在路径中
vendor\backpack\base\src\app\Http\Controllers\Auth\LoginController.php
它工作正常。但是如果我把代码放在
app\Http\Controllers\Auth\LoginController.php
没有用
我正在尝试像这样扩展控制器
use Backpack\Base\app\Http\Controllers\Auth\LoginController as OriginalLoginController;
class MyLoginController extends OriginalLoginController
.....
【问题讨论】:
提供您遇到问题的代码示例 我已将此添加到 loginController。受保护的函数已通过身份验证(请求 $request,$user) 返回重定向('/home3');如果我将它添加到 \vendor\backpack\base\src\app\Http\Controllers\Auth\LoginController.php 它工作正常,但如果我将它添加到 \app\Http\Controllers\Auth\LoginController.php 它不会不行。我不知道为什么。 【参考方案1】:在 Laravel 中创建重定向:
<?php
if($x)
return redirect()->route('routeName1', [$arr, $of, $params]);
else if($y)
return redirect()->route('routeName2', [$arr, $of, $params]);
【讨论】:
以上是关于在 laravel 背包中登录后有条件地重定向用户的主要内容,如果未能解决你的问题,请参考以下文章
如何从本地存储中获取每个用户在 laravel 6 中登录的数据?
是否可以使用RedirectMatch有条件地重定向URL?