2015-02-11 58 views
0

我正在尝试创建一个适用于特定域的过滤器,并在用户从他们尝试访问的任何页面重定向用户时,如果他们超过了配额。到目前为止,代码根本不会重定向。如何在Laravel中为域创建过滤器

这里是我迄今为止在filters.php

Route::filter('domain', function() { 
    if (stripos(Request::root(), Config::get('domains.main')) !== false) { 
     if (Auth::check()) { 
      $user = Auth::user(); 

      $max_quota = Plan::where('id', $user->plan_id)->where('is_active', true)->pluck('max_quota'); 

      $quota_used = Quota::where('user_id', $user->id)->count(); 

      if (empty($max_quota)) { 
       return Redirect::to('account/error/inactive'); 
      } elseif ($quota_used >= $max_quota) { 
       return Redirect::to('account/error/over_quota'); 
      } 
     } 
    } 
}); 

即使我把这个routes.php下:

Route::group(['domain' => Config::get('domains.main')], function() { 

    Route::filter('*', function() { /* Same code here... */ }); 

} 

然后它会进入过滤功能,成功地检查标准,但重定向仍然不会发生。

我想我在这里错过了多个关键点。想法?

回答

0

为了使这项工作,我需要改变:

Route::group(['domain' => Config::get('domains.main')], function() { 

要:

Route::group(['domain' => Config::get('domains.main'), 'before' => 'domain'], function() { 

我还需要重新定向环路保护添加到该行:

if (Auth::check()) { 

所以它变成:

if (Auth::check() && !strpos(Request::fullUrl(), 'account/error')) {