Install new Laravel 5.4 project.
Run php artisan make:auth
.
Go to http://localhost:8000/home (without being logged in).
I get redirected to http://localhost:8000/login
This redirect seems like magic. This is the route for home:
|| GET|HEAD |home||App\Http\Controllers\HomeController@index|web,auth|
We never get to the index()
method because the auth
middleware takes over. I open the auth
middleware file Illuminate\Auth\Middleware\Authenticate.php
and there's no mention of the /login
redirect.
Where is the redirect from /home
to /login
defined?
Check app/Exceptions/Handler.php
file and you'll see Exception handler
for unauthenticated users:
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
P.S. redirect to home
you can see in app/Http/Middleware/RedirectIfAuthenticated.php
file.
UPD1: redirect to home after login/register you can set in app/Http/Controllers/Auth/LoginController.php
and app/Http/Controllers/Auth/RegisterController.php
. It looks like
protected $redirectTo = '/home';