0

我想知道是否有可能使我的每个控制器的身份验证重定向不同?目前,一切都重定向到/ home。这是为我的HomeController设计的。但对于ClientController,我希望它重定向到/客户端(如果通过身份验证)而不是/ home。我是否必须为每个控制器创建一个新的中间件,或者是否有办法通过重用auth来完成此操作?Laravel 5.2如何根据控制器更改RedirectIfAuthenticated的重定向?

RedirectIfAuthenticated.php

if (Auth::guard($guard)->check()) { 
    return redirect('/home'); //anyway to change this to /client if coming from ClientController? 
} 

我有这对我的ClientController.php

public function __construct() 
{ 
    $this->middleware('auth'); 
} 

提前感谢!对Laravel和中间件来说相当新颖。

回答

0

没关系,我能够通过正确的路由工作。 在web中间添加ClientController,负责所有的身份验证。

Route::group(['middleware' => ['web']], function() { 
    Route::resource('client', 'ClientController'); 
} 

而且在 ClientController.php,增加可使用auth中间件。

public function __construct() 
{ 
    $this->middleware('auth'); 
} 

public function index() 
{ 
    return view('client'); 
} 
0

User模型只需使用这样的:

protected $redirectTo = '/client'; 

您还可以通过更改Laravel的核心文件,实现这一目标。如果您正在使用Laravel 5.2去project_folder\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php

您可以找到下面的代码:

public function redirectPath() 
{ 
    if (property_exists($this, 'redirectPath')) { 
     return $this->redirectPath; 
    } 

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; //Change the route in this line 
} 

现在,改变/home/client。不过,我建议不要更改核心文件。你可以使用第一个。

+0

尝试过,但它不会改变一件事,加$ redirectTo ='/ client';在我的用户模型上。一切工作正常,只要放置Route :: resource('client','ClientController');在Route :: group(['middleware'=> ['web']],在routes.php中的function(){ } –

相关问题