2014-11-21 76 views
0

我想为我的应用程序在Laravel 4中创建一个路由过滤器。如何在Laravel 4中创建路由过滤器?

详情:

  • 我想允许这条路过滤器仅
  • 用户>键入= '代理商'
  • 分销商─>键入= 'OEM'

我当然知道{{ Auth::user()->distributor()->first()->type }}会返回OEM

这里是我的filters.php代码

Route::filter('distributor', function() 
{ 
     if (Auth::user()->distributor()->first()->type !== "OEM") 

     { 
      if (Request::ajax()) 
      { 
       return Response::make('Unauthorized', 404); 
      } 

     } 

     else return View::make('errors.404_auth'); 
}); 

我得到了一个错误:

Error

回答

1

我看见你试图访问用户与经销商之间的关系,但是您没有检查具有该特定类型的用户是否确实存在。

我的建议将是

你的第一个if语句的上方添加if (Auth::user()->type == " You user type in string "){

查看详细信息的代码。我希望这有助于!

Route::filter('oem', function() 
{ 

    if (Auth::user()->type == " You user type in string "){ 

     if (Auth::user()->distributor()->first()->type == "OEM") 
     { 
      if (Request::ajax()) 
      { 
       return Response::make('Unauthorized', 404); 
      } 

     } 

     else return View::make('errors.404_auth'); 

    } 
}); 
相关问题