【问题标题】:Auth Issue when upgrading from Laravel 5.2 to Laravel 5.3从 Laravel 5.2 升级到 Laravel 5.3 时的身份验证问题
【发布时间】:2017-02-27 11:52:58
【问题描述】:

我按照官方指南从laravel 5.2升级到laravel 5.3https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

因为我需要对默认身份验证进行一些自定义,所以我将login 函数复制到Http\Controllers\Auth\AuthController.php

现在,当我更新时,`AuthController.php' 被分成了几个其他文件。

我已将login 函数复制到Http\Controllers\Auth\LoginController.php

现在,我在尝试登录时收到以下错误:

Controller.php 第 82 行中的 BadMethodCallException:

方法 [getCredentials] 不存在。

下面的登录功能(可能无关紧要):

public function login(Request $request)
{
$this->validate($request, [
    'email' => 'required|email', 
    'password' => 'required',
]);

$credentials = $this->getCredentials($request);

// This section is the only change
if (Auth::validate($credentials)) {
    $user = Auth::getLastAttempted();
    if ($user->active) {
        Auth::login($user, $request->has('remember'));

        ActivityLog::add("User has successfully logged in.", $user->id);

        return redirect()->intended($this->redirectPath());
    } else {
        return redirect($this->loginPath) // Change this to redirect elsewhere
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'active' => 'This account has been suspended.'
            ]);
    }
}

return redirect($this->loginPath)
    ->withInput($request->only('email', 'remember'))
    ->withErrors([
        'email' => $this->getFailedLoginMessage(),
    ]);

}

我该如何解决这个问题?

【问题讨论】:

  • getCredentials 方法在哪里?它说找不到它
  • 好吧,这个方法可能在5.25.3 之间被删除了。或者换成别的东西……
  • 所以你没有创建它?因为错误清楚地表明你的代码需要它,而你在这里使用它$credentials = $this->getCredentials($request);

标签: laravel laravel-5 laravel-5.2 laravel-5.3


【解决方案1】:

此方法只是从请求数据中返回登录用户名(可以是用户名、电子邮件或自定义字段)和密码。您可以将 getCredentials() 调用替换为:

$request->only($this->username(), 'password');

注意

根据您合并代码的方式,方法$this->username() 也可以用作旧版本中的$this->loginUsername()

【讨论】:

  • Method [loginUsername] does not exist. $this->username() 的方法成功了!谢谢!
【解决方案2】:

现在看到这里的其他人调用 getCredentials(Response $response) 在 5.3 中被替换为 credentials(Response $response)

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 1970-01-01
    • 2017-05-25
    • 2017-03-15
    • 2017-02-04
    • 1970-01-01
    • 2016-12-21
    • 2019-02-14
    相关资源
    最近更新 更多