我正在编写一个注销函数,简单地从数据库中删除所有必要的信息后,我用重定向清除会话和注销。我想将用户重定向到主页(登陆页),这确实在另一个功能,这是登录(成功登录后,我会重定向到家。在我的代码中是这样做的:
public function logout()
{
$userModel = new User;
$userToken = Session::get('userToken');
$isCleared = $userModel->clearSessionForUser($userToken);
if($isCleared == true)
{
Session::forget('userToken');
Session::flush();
return Redirect::route('/');
}else{
$errorArray = array('errorMsg'=>'logout operation didn't succeed ');
return View::make('errors.error', $errorArray);
}
}
routes.php有如下路由:
Route::get('/', 'MainController@index');
我之前在login的时候也这么做过但是这里显示的是:
InvalidArgumentException
Route [/] not defined.
下面是包含重定向的登录部分:
$userModel = new User;
$isUser = $userModel->validateDetails($email, $password);
if($isUser==true)
{
return Redirect::route('/');
}else{
$errorArray = array('errorMsg'=>'The credentials doesn''t match any user records or the account has not been activated ');
return View::make('errors.error', $errorArray);
}
请支持并让我知道为什么它的工作一次,而不是再次,虽然这两个方法在同一个类和目标相同的路由。
Redirect::route中的参数应为路由名。所以,如果你这样修改你的路由文件:
Route::get('/', array('as' => 'home', 'uses' => 'MainController@index'));
那么你可以在Redirect::route:
中使用这个路由名return Redirect::route('home');
或者,您可以简单地使用Redirect::to,重定向到URL:
return Redirect::to('/'); // This is the URL
我不知道它是否相关-但你有一个语法错误:
$errorArray = array('errorMsg'=>'logout operation didn't succeed ');
应该$errorArray = array('errorMsg'=>"logout operation didn't succeed ");
或
$errorArray = array('errorMsg'=>'logout operation didn''t succeed ');