【发布时间】:2018-04-25 00:44:40
【问题描述】:
我正在使用 Laravel Auth 脚手架。在我的LoginController.php 文件中,我有我的redirectTo 方法。我不希望它有条件重定向。但是,每次我测试登录路由时,路由都会转到/home,而不是我的条件中指定的路由。
登录控制器.php
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use App\Role;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
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;
/**
* Create a new controller instance.
*
* @return void=
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function redirectTo()
{
$user = Auth::user();
if($user->isEmployer()){
return '/employer';
}
return '/home';
}
}
RedirectIfAuthenticated.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/employer', 'EmployerController@index');
Route::get('/home', 'HomeController@index')->name('home');
我在if 语句中写了一个dd() 函数,它确实有效。问题是无论如何重定向总是去/home。
当guest auth 中间件将始终覆盖该函数时,我不明白拥有redirectTo 方法的意义吗?
如何在不让中间件覆盖该方法的情况下依赖此重定向方法?
谢谢
【问题讨论】:
-
你能分享你的 web.php 文件吗?
-
我刚刚添加了。
-
访问 EmployerController@index() 函数时是否有中间件动作?
-
不,我在那个函数上根本没有任何中间件
标签: php laravel laravel-5.4