Reputation: 2972
I am using Laravel 5.4 for a project and scenario is if user
does not have any clubs
created then on login redirect user
to clubs/create
page.
Below is my route page coding
directory structure is routes/backend/access.php
Route::group([
'prefix' => 'access',
'as' => 'access.',
'namespace' => 'Access',
], function () {
Route::group([
'middleware' => ['access.routeNeedsRole:3', 'first.run']
], function () {
/*
* Clubs Management
*/
Route::group(['namespace' => 'Clubs'], function () {
Route::get('create', 'ClubsController@showCreateOrganiser')->name('clubs.showCreateOrganiser');
/*
* Specific Club
*/
Route::group(['prefix' => 'clubs/{club}'], function () {
// Access
Route::get('login-as', 'ClubsController@loginAs')->name('clubs.login-as');
});
//For when admin is logged in as user from backend
Route::get('logout-as', 'ClubsController@logoutAs')->name('clubs.logout-as');
/*Clubs CRUD*/
Route::resource('clubs', 'ClubsController');
//For DataTables
Route::post('clubs/get', 'ClubsTableController')->name('clubs.get');
});
});
});
Below is my FirstRunMiddleware.php
code
Class FirstRunMiddleware
{
public function handle($request, Closure $next)
{
if (Clubs::scope()->count() === 0) {
return redirect()->route('admin.access.clubs.showCreateOrganiser');
}
$response = $next($request);
return $response;
}
}
It redirects too many times and i get the error message that redirected you too many times
.
What is it i am doing wrong here??
Upvotes: 0
Views: 1021
Reputation: 1935
its because when its redirected to create page, the Middleware will work again! and it will check for clubs and redirect you to the same page again, and so on.
you need to make the create rout out of FirstRunMiddleware
scope.
Upvotes: 2