私はLaravel Authの足場に取り組んでいます。私のLoginController.php
ファイルの中に私のredirectTo
メソッドがあります。私はそれが条件付きのリダイレクトを持つようにしていません。しかし、私がログインルートをテストするたびに、私の条件の中で指定されたルートの代わりに/home
にルートが行きます。Laravel 5.4の認証ミドルウェアがredirectToメソッドの問題をオーバーライドしました
LoginController.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', '[email protected]');
Route::get('/home', '[email protected]')->name('home');
私は私のif
文の中dd()
機能を書き、それが作業を行います。問題はリダイレクトが何であっても常に/home
に行くことです。
redirectTo
メソッドを使用している点は、guest
認証ミドルウェアが常にその機能をオーバーライドする場合はありませんか?
ミドルウェアでそのメソッドをオーバーライドしなくても、このリダイレクトメソッドにどのように依存することができますか?あなたの条件に応じて、あなたのRedirectIfAuthenticated
ミドルウェアを変更する必要が
おかげ
web.phpファイルを共有できますか? – HasilT
私はちょうどそれを追加しました。 – 75Kane
EmployerController @ index()関数にアクセスするときにミドルウェアが動作しますか? – HasilT