简体   繁体   中英

Laravel route is redirecting too many times

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??

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM