I'm working with the Laravel Auth scaffold. Inside my LoginController.php
file I have my redirectTo
method. I wan't it so that it has a conditional redirect. However, every time I test the login route, the route goes to /home
instead of the route specified inside my conditional.
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', 'EmployerController@index');
Route::get('/home', 'HomeController@index')->name('home');
I wrote a dd()
function inside my if
statement and it does work. The problem is the redirect always goes to /home
no matter what.
I don't get the point of having the redirectTo
method when the guest
auth middleware will always override that function?
How would I be able to depend on this redirect method without having the middleware override that method?
Thanks