使用自定义FormRequest时,请求未正确发送,但使用默认值时发送

时间:2018-10-22 13:03:53

标签: php laravel http laravel-5

这是我用于使用Laravel Passport登录用户的代码:

public function login(LoginRequest $request)
{
    $email = $request->email;
    $password = $request->password;

    $request->request->add([
        'username' => $email,
        'password' => $password,
        'grant_type' => 'password',
        'client_id' => env('PASSWORD_GRANT_CLIENT_ID'),
        'client_secret' => env('PASSWORD_GRANT_CLIENT_SECRET'),
        'scope' => '*',
    ]);

    $tokenRequest = Request::create(
        env('APP_URL') . '/oauth/token',
        'post'
    );

    $response = Route::dispatch($tokenRequest);

    return $response;
}

LoginRequest类:

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'email' => 'required|string|email|max:255',
        'password' => 'required|min:6',
    ];
}

当我尝试使用Insomnia客户端登录时,返回以下信息:

{
    "error": "unsupported_grant_type",
    "message": "The authorization grant type is not supported by the 
    authorization server.",
    "hint": "Check that all required parameters have been provided"
}

我尝试从登录函数而不是$request返回$response,但是返回的却是该函数确切发送的内容,所以我知道不是那样的。

更令人困惑的是,如果我将登录方法内部的LoginRequest注入替换为默认的Request,则该功能可以正常工作。

有人知道如何解决吗?

0 个答案:

没有答案