For a web project that requires a login/register system and lot of CRUD operations, I choose to learn Laravel. I have some experience from school with MVC .NET and Spring and while there are a lot of similarities, some things are a little bit different... The requirement of this project is that it has a login form on every page when not logged in (guest). That form changes to "the profile" of the user when logged in (auth). The problem I'm having is that when I log in and try to redirect using the middleware RedirectIfAuthenticated, I come in an infinite loop of redirects for some reason. Is there a solution to create a login form on every page, and instead of redirecting to another view, just refresh the page and set the correct values using Blade's templating engine?
I've created the base with the php artisan make:auth
command, and made some changes myself. I'll post snippets of what I have with the according class.
To give an idea how it's gonna look & why there need to be a form on every page: (btw I'm putting it in the footer in layouts/app.blade.php.
Routes/web.php: I unraffeld the Facade Auth::routes() to this so I can make changes. I left login temporarly but it should be gone when I got my things working. The bottom 2 comments I ommitted.
Route::get('/', 'HomeController@index')->name('home');
Route::get('home', 'HomeController@index')->name('home');
Route::get('teams', 'HomeController@teams')->name('teams');
Route::get('schedules', 'HomeController@schedules')->name('schedules');
Route::get('tables', 'HomeController@tables')->name('tables');
Route::get('rules', 'HomeController@rules')->name('rules');
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
// Password Reset Routes...
HomeController.php
Here I'm doubting the configuration in the constructor should it be 'guest' or 'auth' as middleware?
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the homepage.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
//other pages omitted
}
LoginController.php (no difference with artisan auth):
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;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
RedirectIfAuthenticated.php:
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);
}
}
App.blade.php:
@if (Auth::guest())
//login form omitted ...
@else
<!-- TODO: check for role & display correct nav -->
<h2>Logged in!</h2>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
@endif
Tell me if you need more info. Thanks in advance.