【问题标题】:How add parameter in FormRequest Laravel如何在 FormRequest Laravel 中添加参数
【发布时间】:2020-01-23 06:17:24
【问题描述】:

我正在使用护照登录(在 API 中)我正在尝试获取身份验证服务器生成的令牌。但是,我无法向作为 FormRequest 实例的请求添加额外参数。

另一方面,如果我将请求更改为Request 的实例,它会起作用。

所以,我的问题是如何将参数添加到我的查询 $loginRequest(这是 FormRequest 的实例)

$loginRequest->request->add($params);

这是我的代码:

class AuthController extends Controller
{
    use ThrottlesLogins;

    public function store(LoginRequest $loginRequest)
    {
        $loginRequest->validated();
        if ($this->hasTooManyLoginAttempts($loginRequest)) {
            $this->fireLockoutEvent($loginRequest);
            return $this->sendLockoutResponse($loginRequest);
        }
        if (Auth::attempt($this->credentials($loginRequest))){
            $client = $this->getClient($loginRequest->name);
            $params = [
                'grant_type'    => 'password',
                'client_id'     => $client->id,
                'client_secret' => $client->secret,
                'username'      => $loginRequest->email,
                'password'      => $loginRequest->password,
                'scopes'         => 'fd',
            ];
            $loginRequest->request->add($params);
            $req = Request::create('oauth/token', 'POST');
            $response = Route::dispatch($req)->getContent();
            return $response;
        }

        $this->incrementLoginAttempts($loginRequest);
        $this->sendFailedLoginResponse($loginRequest);
    }
}

【问题讨论】:

  • 刚刚检查,在自定义表单请求中使用add() 没有问题。
  • 额外的参数没有加起来,我知道这是因为我有一个服务器错误 oauth @Harven
  • 服务器 oauth 错误不能证明 add() 不起作用。尝试在调用add() 方法后立即转储$login->request 并手动检查。
  • 参数加好了,为什么我的oauth服务器收不到这些参数?

标签: php laravel oauth request laravel-6


【解决方案1】:

要将属性附加到FormRequest 的实例,您可以使用merge() 方法。

public function store(LoginRequest $loginRequest) {
    $params = [
        'foo' => 'bar',
    ];

    $loginRequest->merge($params);
}

【讨论】:

  • 它不起作用,因为身份验证服务器向我发送了此错误"error": "unsupported_grant_type", "error_description": "The authorization grant type is not supported by the authorization server.", "hint": "Check that all required parameters have been provided", "message": "The authorization grant type is not supported by the authorization server."
  • 但是,当我将 Loginrequest 替换为 Request 时,我不再收到奇怪的错误
  • 这似乎是与 Laravel Passport 无关的问题,您必须独立于 FormRequest 问题进行修复。在不使用 Passport 的 Laravel 项目中尝试解决方案。
  • 确实,mergeadd 方法有效。但是,它不适用于护照。
  • 听起来您缺少与授权相关的中间件或其他东西。了解如何将自定义 FormRequests 与 Passport 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2017-09-03
  • 2018-03-06
  • 2020-04-21
  • 2015-10-09
  • 2020-06-19
相关资源
最近更新 更多