Php Laravel-注销重定向到主页,而不是注销
我使用的是Laravel5.1。我在Php Laravel-注销重定向到主页,而不是注销,php,authentication,redirect,laravel-5,laravel-5.1,Php,Authentication,Redirect,Laravel 5,Laravel 5.1,我使用的是Laravel5.1。我在AuthController.php中有一个自定义注销函数,我在上面添加的只是计算用户的总登录时间,并更新一个特定的表。我的问题是,当我单击“注销”时,它只会将我重定向到/home,现在,这种情况并不总是发生,但很容易重现。当我单击注销时,它只是重定向到/home,然后我再次单击,同样的事情。有时,我花了4次时间单击注销按钮,然后它注销并将我发送到登录页面。这里发生了什么?这是我的密码: public function getLogout() { i
AuthController.php
中有一个自定义注销函数,我在上面添加的只是计算用户的总登录时间,并更新一个特定的表。我的问题是,当我单击“注销”时,它只会将我重定向到/home
,现在,这种情况并不总是发生,但很容易重现。当我单击注销时,它只是重定向到/home
,然后我再次单击,同样的事情。有时,我花了4次时间单击注销按钮,然后它注销并将我发送到登录页面。这里发生了什么?这是我的密码:
public function getLogout()
{
if (Auth::check())
{
$userid = Auth::user()->id;
date_default_timezone_set('Asia/Taipei');
$today = date('Y-m-d');
$logHour = new LoginHour();
$checkLogin = $logHour->checkLoginHoursOut(intval($userid), $today);
if($checkLogin != null)
{
$loginhours = '';
$timestamp = date('Y-m-d h:i:s');
$timestamp2 = strtotime($timestamp);
$userLastLogin = $checkLogin[0]->timestamp;
$userLastLogin2 = strtotime($userLastLogin);
// Get difference in hours
$diffHours = round(($timestamp2 - $userLastLogin2) / 3600, 2);
LoginHour::where('date', '=', $today)->
where('user_id', '=', $userid)->
update(['loginhours' => $checkLogin[0]->loginhours + $diffHours, 'status' => 0, 'timestamp' => $timestamp]);
}
Auth::logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/auth/login');
}
else
{
return Redirect::to('/auth/login');
}
}
现在您可能会注意到我有一个Auth::check()
,我需要它,因为我不知道为什么在$userid=Auth::user()->id代码>但现在没事了,问题是我被重定向到/home
我还声明了protected$redirectAfterLogout='/auth/login'代码>在我的控制器顶部。问题在于句柄重定向身份验证中间件。
protected $redirectLogoutTo = '/login';
它检查请求并重定向到{domain}/home。只需卸下它并使用控制器进行处理 在Laravel 5.3/5.4中测试
指定它应该重定向的URL。在LoginController中
protected $redirectLogoutTo = '/login';
在LoginController中,重写AuthenticatesUsers的注销方法
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect($this->redirectLogoutTo); // only line that changes
}
在这个方法中,您还可以根据自己的喜好执行其他操作。您是否可以尝试删除与您的问题无关的代码?这将大大改善这个问题:-)@edi9999这是我的全部注销功能:)我把它们全部放在这里的原因是让其他人发现其他部分可能有什么问题。我相信我已经很好地解释了这个问题,代码也没有那么长时间被混淆:)可能是您没有成功注销。你检查过了吗?我想它会因为你的else语句而把你送回主页。这意味着你仍然在登录。试着用可能!Auth::guest()而不是Auth check.。要检查您是否是guest。@JLPuro尝试了您的建议,但也无效,我尝试了5次才成功注销:(