我的laravel控制器中有一个用于登录网站的功能,但是我无法确定传递这两个字段(电子邮件和密码)的最佳方法
进入函数调用loginAttempt()
目前,我有:
public function login(Request $request)
{
//getting email and password form fields for validation
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
//Need to pass email and password into the loginAttempt() function
$authService = new AuthService();
$login = $authService->loginAttempt();
dd($login);
}
我知道我可以使用$login = $authService->loginAttempt($arguments);
,但是我要传递的功能需要将电子邮件和密码作为单独的变量。
如何将它们都传递给该loginAttempt()
函数调用?
答案 0 :(得分:2)
只需使用$request->input
从输入中获取值,如下所示
public function login(Request $request)
{
//getting email and password form fields for validation
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
//Need to pass email and password into the loginAttempt() function
$email = $request->input ('email');
$password = $request->input ('password');
$authService = new AuthService();
$login = $authService->loginAttempt($email, $password);
dd($login);
}
答案 1 :(得分:1)
您可以通过两种方式实现多种功能
第一种方式 将每个变量分开传递
$authService = new AuthService();
$login = $authService->loginAttempt($request->email, $request->password);
第二种方式
创建一个像这样使用的single dimension
数组
$authService = new AuthService();
$login = $authService->loginAttempt(['email' => $request->email, 'password' => $request->password]);
并在您的AuthService
中使用诸如此类的键来获取价值
$data['email'] or $data['password']