缩短控制器逻辑 Laravel
Shorten the Controller Logic Laravel
我正在尝试使用 Laravel 使用不同守卫的登录身份验证,但我发现登录控制器中的存储方法中存在一些重复(唯一的区别是使用的守卫,所有其他逻辑都是相同的)。我只是找不到一种方法将它们缩短为其他一些逻辑,例如可以重用的方法(函数)。所以如果有可能的方法,请帮助我。
public function store(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
if (Auth::guard('instructor')->attempt(['email' => $request->username, 'password' => $request->password])) {
if (auth('instructor')->user()->status === Instructor::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('instructor')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route('instructor.dashboard'));
}
if (Auth::guard('student')->attempt(['email' => $request->username, 'password' => $request->password])) {
if (auth('student')->user()->status === Student::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('student')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route('student.dashboard'));
}
return back()->with('error', 'Credentials provided do not match any record.');
}
您可以创建一个单独的函数,然后调用该函数。
public static function deleteSession($gaurd)
{
$request->session()->flush();
Auth::guard($gaurd)->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
然后在您的存储函数中静态调用 deleteSession
。
if (auth('instructor')->user()->status === Instructor::HAS_DEACTIVATED)
{
self::deleteSession('instructor'); //change gaurd according to your need
}
这可能不是解决此问题的最佳方法,我相信有更好的解决方案,@Aqib Javaed 似乎就是这样,但这里有一种方法可以稍微缩短代码我可以管理。它并不完美,但完成了工作。
public function store(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
if (Auth::guard('instructor')->attempt(['email' => $request->username, 'password' => $request->password])) {
$userType = 'instructor';
} elseif (Auth::guard('student')->attempt(['email' => $request->username, 'password' => $request->password])) {
$userType = 'student';
} else {
return back()->with('error', 'Credentials provided do not match any record.');
}
$modelName = ucwords($userType); //turn the user type to its corresponding model name
if (auth('$userType')->user()->status === $modelName::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('$userType')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route("$userType.dashboard"));
}
我正在尝试使用 Laravel 使用不同守卫的登录身份验证,但我发现登录控制器中的存储方法中存在一些重复(唯一的区别是使用的守卫,所有其他逻辑都是相同的)。我只是找不到一种方法将它们缩短为其他一些逻辑,例如可以重用的方法(函数)。所以如果有可能的方法,请帮助我。
public function store(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
if (Auth::guard('instructor')->attempt(['email' => $request->username, 'password' => $request->password])) {
if (auth('instructor')->user()->status === Instructor::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('instructor')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route('instructor.dashboard'));
}
if (Auth::guard('student')->attempt(['email' => $request->username, 'password' => $request->password])) {
if (auth('student')->user()->status === Student::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('student')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route('student.dashboard'));
}
return back()->with('error', 'Credentials provided do not match any record.');
}
您可以创建一个单独的函数,然后调用该函数。
public static function deleteSession($gaurd)
{
$request->session()->flush();
Auth::guard($gaurd)->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
然后在您的存储函数中静态调用 deleteSession
。
if (auth('instructor')->user()->status === Instructor::HAS_DEACTIVATED)
{
self::deleteSession('instructor'); //change gaurd according to your need
}
这可能不是解决此问题的最佳方法,我相信有更好的解决方案,@Aqib Javaed 似乎就是这样,但这里有一种方法可以稍微缩短代码我可以管理。它并不完美,但完成了工作。
public function store(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
if (Auth::guard('instructor')->attempt(['email' => $request->username, 'password' => $request->password])) {
$userType = 'instructor';
} elseif (Auth::guard('student')->attempt(['email' => $request->username, 'password' => $request->password])) {
$userType = 'student';
} else {
return back()->with('error', 'Credentials provided do not match any record.');
}
$modelName = ucwords($userType); //turn the user type to its corresponding model name
if (auth('$userType')->user()->status === $modelName::HAS_DEACTIVATED) {
$request->session()->flush();
Auth::guard('$userType')->logout();
return redirect('login')->with(
'error',
'Your Account has being deactivated . Please Contact your Administrator!');
}
return redirect(route("$userType.dashboard"));
}