laravel重定向到路线并不总是有效

时间:2014-05-03 15:33:11

标签: php laravel laravel-4

我正在编写一个注销功能,只需在从数据库中删除所有必要信息后,我就会通过重定向清除会话和注销。我想将用户重定向到主页(登录页面),这确实在另一个登录功能中工作(成功登录后,我会重定向到主页。以下是我在代码中的操作方式:

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');

我之前已经这样做了,因为我说登录并且它有效但是在这里说:

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);
        }

请支持并让我知道为什么它可以工作一次而不是再次工作,尽管两者都是同一类中的方法并且针对相同的路线。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我不知道它是否相关 - 但你有语法错误:

$errorArray = array('errorMsg'=>'logout operation didn't succeed  ');

应该是

$errorArray = array('errorMsg'=>"logout operation didn't succeed  ");

$errorArray = array('errorMsg'=>'logout operation didn\'t succeed  ');
相关问题