Laravel has many useful features which makes web application development easy and fast. Laravel also provides default login system. But there is one thing we need to do is to redirect back to the previous page after successful login.

In this article we will show you how to change redirect back to previous url after success login. This is useful when login is required like feedback or comment or like in page. Therefore it is important that, user redirect back to that page.

In that case, there are two ways we can do it. One is Laravel provides inbuilt intended() method to redirect back to previous url.

/**
 * Show the application loginprocess.
 *
 * @return \Illuminate\Http\Response
 */
public function loginPost()
{
    if ($this->auth->attempt([
        'email' => Input::get('email'),
        'password' => Input::get('password')
    ])) {
        return Redirect::intended();
    } else {
        return back();
    }
}

The above method sometimes don't work properly. So the other method is we can do it by using Laravel Session. This will work perfect in all way.

/**
 * Show the application login page.
 *
 */
public function login()
{
    Session::put('previous_url', URL::previous());

    return view('login');
}

/**
 * Show the application loginprocess.
 *
 * @return \Illuminate\Http\Response
 */
public function loginPost()
{
    if ($this->auth->attempt([
        'email' => Input::get('email'),
        'password' => Input::get('password')
    ])) {
        return Redirect::to(Session::get('previous_url'));
    } else {
        return back();
    }
}

This way you can also set previous url and redirect back using session.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments