2017-08-15 189 views
0

我使用Laravel 5.4的項目和方案是,如果user沒有創造那麼在登錄重定向userclubs/create頁任何clubsLaravel路由重定向次數太多

下面是我的路線頁面編碼

目錄結構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', '[email protected]')->name('clubs.showCreateOrganiser'); 
     /* 
     * Specific Club 
     */ 
     Route::group(['prefix' => 'clubs/{club}'], function() { 

      // Access 
      Route::get('login-as', '[email protected]')->name('clubs.login-as'); 

     }); 

     //For when admin is logged in as user from backend 
     Route::get('logout-as', '[email protected]')->name('clubs.logout-as'); 


     /*Clubs CRUD*/ 
     Route::resource('clubs', 'ClubsController'); 


     //For DataTables 
     Route::post('clubs/get', 'ClubsTableController')->name('clubs.get'); 

     }); 

    }); 
}); 

下面是我FirstRunMiddleware.php代碼

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; 
    } 
} 

它重定向過很多次,我也得到該錯誤消息redirected you too many times

這是什麼我在這裏做錯了?

回答

2

其因爲當它重定向創建頁面時,中間件將再次工作!它會檢查俱樂部並重新將您重定向到同一頁面,等等。

您需要將範圍從FirstRunMiddleware範圍內創建。

+0

Git it。需要添加if條件。謝謝。 –