【问题标题】:Laravel doesn't change user model on the goLaravel 不会随时更改用户模型
【发布时间】:2013-10-15 14:49:48
【问题描述】:

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

这是我的登录控制器:

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::attempted() 之后它不会更改设置。看来我必须重新加载 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'));
}

一切都很好。

【问题讨论】:

    标签: php authentication laravel laravel-4


    【解决方案1】:

    简答: 你说得对。在第一次调用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'));
    });
    

    这里的“Host”和“Student”是您希望 Eloquent 身份验证驱动程序使用的类名。

    在 app/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 中设置提供程序,但我决定将其保留在路由中,以便很清楚两组路由用于两个不同的域并具有两种不同的身份验证模型。

    如果我新建它,我会做一些不同的事情,但是这两个网站是一个大型遗留代码库的前端,我无法对其数据库进行重大更改,所以我坦率地承认这有点像 hack .如果您不是这种情况,更好的解决方案可能是使用 User 类进行身份验证,然后将其附加到您需要的其他两个模型。

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 2020-08-17
      • 2016-09-24
      • 2020-10-19
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多