Laravel路线重定向次数过多

时间:2017-08-15 06:03:02

标签: php laravel laravel-5

我正在使用Laravel 5.4进行项目,方案是user在登录重定向clubsuser页面时没有创建任何clubs/create

以下是我的路线页面编码

目录结构为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');

        });

    });
});

以下是我的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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

因为当它重定向到创建页面时,中间件将再次运行!它将检查俱乐部并再次将您重定向到同一页面,依此类推。

您需要从FirstRunMiddleware范围中创建创建路径。