Kenny
Kenny

Reputation: 2144

Symfony2 Session - Proper Start and Retrieval

I'm using Symfony2 to talk to an API then start a session. I have my OauthCallbackController.php that finishes the authorization process then starts a sessions and sets some variables. I then want to be able to access those variables from my other controllers, to ensure that I have proper access to the API.

There are so many different (and confusing) ways I've read about in Symfony2 sessions, right now I'm using below:

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RedirectResponse;

class OauthCallbackController extends Controller
{
public function callbackAction(Request $request)
{

    $session = new Session();
    $session->start();


    // Oauth Code here
    $json_response = curl_exec($curl);
    $response = json_decode($json_response, true);
    $access_token = $response['access_token'];
    $instance_url = $response['instance_url'];


    $session->set('access_token', $access_token);
    $session->set('instance_url', $instance_url);

    return new RedirectResponse("rootpage.com");

}
}

This is how I perceive starting a session, but I cannot figure out how to use/access this session in my other controllers. So if anyone can help me with that, or if I'm doing the session creation wrong, please provide feedback.

Upvotes: 0

Views: 432

Answers (1)

Federkun
Federkun

Reputation: 36934

You don't need start yourself a session (Symfony does that for you), nor you need to create your own Session object.

In your controllers you can get the session object that Symfony provides from the Request object.

$session = $request->getSession();

That's all. You can read the documentation about it here.

Upvotes: 1

Related Questions