spacemonkey
spacemonkey

Reputation: 2550

laravel redirection to route doesn't always work

I am writing a logout function and simply after deleting all necessary information from database, I clear session and logout with a redirect. I would like to redirect the user to the home page (landing page) and this did work in another function which is the login (upon successful login, I would redirect to home. Here is how it is done in my code:

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

the routes.php has the following route:

Route::get('/', 'MainController@index');

and I've done this before as I said with login and it worked but here it is saying:

InvalidArgumentException
Route [/] not defined.

Here is how the login part containing the redirect looks like:

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

Please support and let me know why it works once and not again although both are methods in the same class and targeting the same route.

Upvotes: 0

Views: 993

Answers (2)

seeARMS
seeARMS

Reputation: 1690

The parameter in Redirect::route should be the route name. So, if you modified your routes file as such:

Route::get('/', array('as' => 'home', 'uses' => 'MainController@index'));

Then you can utilize that route name in Redirect::route:

return Redirect::route('home');

Alternatively, you can simply use Redirect::to, to redirect to a URL:

return Redirect::to('/'); // This is the URL

Upvotes: 2

Laurence
Laurence

Reputation: 60038

I dont know if it is related - but you have a syntax error:

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

should be

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

or

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

Upvotes: 0

Related Questions