2013-10-08 18 views
2

我正在使用Laravel 4作为我的应用程序。 在这个应用程序中,我有两个身份验证模型:买家和用户。我不会使用User-> type字段,因为这些模型具有完全不同的逻辑。Laravel不会在旅途中更改用户模型

这里是我的登录控制器:

public function postIndex() 
{ 

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) { 
     Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id. 
     return Redirect::to('/'); 
    } 

    return $this->userauth(); 
} 

public function userauth() { 

    Config::set('auth.model', 'User'); 
    Config::set('auth.table', 'users'); 
    $test = Config::get('auth.model'); 

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) { 
     Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id. 
     return Redirect::to('/'); 
    } 

    Session::flash('error', 'Auth not excepted. '. implode(' ', array_only(Input::get(), array('email', 'password')))); 

    return Redirect::to('logins')->withInput(Input::except('password')); 

} 

我已经在auth.php更改的设置与买家合作。当我为买家输入登录名和密码时,一切都很好。看来,在Auth :: attempt()之后它不会改变设置。看起来我必须重新加载Auth对象。有人能帮助我吗?

购买的方式,如果我写这样的:

public function postIndex() 
{ 


    Config::set('auth.model', 'User'); 
    Config::set('auth.table', 'users'); 
    $test = Config::get('auth.model'); 

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) { 
     Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id. 
     return Redirect::to('/'); 
    } 

    Session::flash('error', 'Auth not excepted. '. implode(' ', array_only(Input::get(), array('email', 'password')))); 

    return Redirect::to('logins')->withInput(Input::except('password')); 
} 

一切都很正常了。

回答

4

简短回答: 你说得对。在第一次调用Auth::attempt()之后,对配置的更改无效。在运行期间,您必须使用Auth::setProvider()来设置将要使用的模型。

龙答: 我不知道该怎么好,这将在特定的设置工作,但我发现你的问题,同时试图做同样的事情,所以我会告诉你我是如何结束这样做。

在我的情况下,要求不是简单地有两种不同类型的用户。我在同一代码库的不同域中运行两个网站,一个为学生,另一个为寄宿家庭。我有一个Student类,将取代User在一个站点和Host为另一个相同的目的。两者都实施UserInterfaceRemindableInterface,所以我们很好。

在app/filters.php,我创建了两个特殊的过滤器:

Route::filter('prep.student', function() { 
    Auth::setProvider(new Illuminate\Auth\EloquentUserProvider(App::make('hash'), 'Student')); 
}); 

Route::filter('prep.host', function() { 
    Auth::setProvider(new Illuminate\Auth\EloquentUserProvider(App::make('hash'), 'Host')); 
}); 

这里的“主机”和“学生”是你想要的雄辩认证驱动程序使用的类名。

在应用程序/ routes.php文件,我把路由分成两组,并且在每个组我用上述适当的过滤器:

/** 
* Routes for Students 
*/ 
Route::group(array('domain' => '[student domain]', 'before' => 'prep.student'), function() { 

    Route::get('test', function() { 
    return View::make('student/test'); 
    }); 
    ... 
}); 

/** 
* Routes for Hosts 
*/ 
Route::group(array('domain' => '[host domain'], 'before' => 'prep.host'), function() { 

    Route::get('test', function() { 
    return View::make('host/test'); 
    }); 
    ... 
}); 

其结果是,所述用户提供被设定为合适的模型为每组路线。您可能也可能在app/start/global.php中设置了提供程序,但我决定将其与路由保持一致,以便非常清楚地看到,这两组路由针对两个不同的域,并且具有两种不同的身份验证模型。

我会做这个有点不同,但我建立新的,但这两个网站是一个大的遗留代码库的前端,我不能显着改变数据库,所以我坦率地承认这是一个有点黑客攻击。如果您不是这种情况,更好的解决方案可能是使用User类进行身份验证,然后将其附加到您需要的其他两个模型中。