Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

keung725's avatar

Not work to set session in route which include controller

Not work to set session in route which include controller


    Route::get('/', 'HomeController@home', function () {
        session(['last_location' => '/']);
    });

    Route::get('/123', function () {
        session(['last_location' => '/123']);
        dd((Session::get('last_location')));
        return view('123');
    });

/123 is work but / is not work

0 likes
2 replies
wells's avatar

@keung725 you'll likely want to set this session variable in your HomeController@home() method. I don't believe the closure you have included is called by anything.

One tip, you can always return redirect()->back(); or simply return back(); to go to the last visited location from a controller method.

Another tip, it is often nice to store the redirect to a page that requires authentication when sending your users back to the login route (i.e. /auth/login?redirect=/needs/authentication).

keung725's avatar

Because I am using facebook login, I think I can't use redirect back, any best way to do this?

public function redirectToFacebook()
    {
        return Socialite::with('facebook')->redirect();
    }

    public function getFacebookCallback()
    {

        $data = Socialite::with('facebook')->user();
        $user = User::where('email', $data->email)->first();

        if(!is_null($user)) {
            Auth::login($user);
            $user->facebook_id = $data->id;

            $user->save();
        } else {
            $user = User::where('facebook_id', $data->id)->first();
            if(is_null($user)){
                // Create a new user
                $user = new User();
                $user->email = $data->email;
                $user->save();
            }

            Auth::login($user);
        }

       
            return redirect()->back()->with('success', 'Successfully logged in!');
        

    }

Please or to participate in this conversation.