douci2516 2013-06-20 16:45
浏览 60
已采纳

Laravel 4 - 动态路由/视图

I have a user login system that consists of two steps:

  1. User Login
    1. Attempt to bind user to AD (This is the LDAP auth module)
    2. If user can login via AD, check if the user exists in the Users table
    3. If user doesn't exist in Users table, add them - only the "username" and "nice name"
      1. If user is added, they will not belong to any teams and will be notified to contact their manager to continue account setup.
    4. Load user columns into Laravel Session credentials - so now we have AD login name, "nice" name (first last), selected team ID, etc.. no passwords because user has authenticated.
  2. Team selection - table userTeams pivot table.
  3. Route user to team dashboard

Right now, I have this route:

// This group forces user to be logged in; auth() will check if user has selected a team and ensures the team exists.
Route::group(array('before' => 'auth'), function() {
  // Handle team - URL will be /dashboard -- Team controller based on view_name from `teamUsers` table

  $teamUser = php_sapi_name() == "cli" ? null : @TeamUser::find(Session::get('credentials.team'));
  if(!empty($teamUser)) {
    $team = ucfirst(strtolower($teamUser->teams()->first()->view_name));
    Route::controller('dashboard', 'Team'.$team.'Controller');
  }

  // Handle index
  Route::controller('/', 'IndexController');
});

However, I don't feel this is a very elegant solution - mostly because I cannot do reverse routing doing this method.

Any tips or suggestions?

  • 写回答

1条回答 默认 最新

  • drm16022 2013-06-20 16:59
    关注

    I would personally choose to do this logic in it's own filter rather than the Route Group closure, since what you are doing is simply 'filtering' users to different routes depending on variables (same thing an Auth filter does).

    update

    Route::filter('whatever', function()
    {
      $teamUser = php_sapi_name() == "cli" ? null : @TeamUser::find(Session::get('credentials.team'));
    
      if ( empty($teamUser) )
        Redirect::route('IndexController');
    });
    
    Route::group(array('before' => 'auth|whatever', function()
    {
      Route::controller('someController');
      Route::controller('/', 'IndexController');
    }));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播