doubiao9775 2016-01-25 21:20
浏览 38
已采纳

Symfony2会话 - 正确启动和检索

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.

  • 写回答

1条回答 默认 最新

  • douyu0725 2016-01-25 21:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染