简体   繁体   English

控制器中的Symfony 3.4 $ _SESSION

[英]Symfony 3.4 $_SESSION in controller

I have an app that starts in a .php file. 我有一个以.php文件开头的应用程序。 In this file I do some work, but at the end I do two things, I generate a value and save in $_SESSION and redirect to other route: 在此文件中,我做了一些工作,但最后我做了两件事,生成一个值并保存在$_SESSION然后重定向到其他路由:

This php file is out of Symfony folder: 这个php文件不在Symfony文件夹中:

<?php
session_start();

...Do some work

$_SESSION['token'] = 'some value';
header ("Location: new direction");
?>

The redirection,redirects to symfony folder,and starts the symfony part going to my src/AppBundle/Controller/MainController . 重定向,重定向到symfony文件夹,并开始将symfony部分转到我的src/AppBundle/Controller/MainController

And in the first action that is processed in this controller I need to access the value of $_SESSION['token'] to do some work with this value in the controller action. 在此控制器中处理的第一个动作中,我需要访问$_SESSION['token']值,以对该控制器动作中的该值进行一些处理。 This is the controller: 这是控制器:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends Controller
{
    public function homepageAction(Request $request)
    {
        dump($_SESSION);
        die;

        ...Some work
    }
}
?>

The dump($_SESSION) shows this: dump($_SESSION)显示以下内容:

在此处输入图片说明

But I can´t find my $_SESSION['token'] 但是我找不到我的$_SESSION['token']

Do I need to use some Symfony component to access the PHP $_SESSION values inside a controller? 我是否需要使用一些Symfony组件来访问控制器内的PHP $ _SESSION值?

Or How can I access $_SESSION['token'] in the controller? 或如何在控制器中访问$_SESSION['token']

Best way is to create route that accept token param, to stuff like saving token param in session, and redirect user. 最好的方法是创建接受令牌参数的路由,进行将令牌参数保存在会话中的操作,并重定向用户。

If you need to duplicate this behavior for all your routes, use a listener on request event. 如果需要对所有路由重复此行为,请在请求事件中使用侦听器。

Edit : More informations about my answer 编辑:有关我的答案的更多信息

Step 1 : Set your token value 第1步:设置令牌值

Step 2 : Redirect to Symfony route that accept token param 第2步:重定向到接受令牌参数的Symfony路由

Step 3 : Apply your own logic based on token value 步骤3:根据令牌值应用自己的逻辑

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;


/**
 * @Route("/{token}", defaults={"token"=null})
 */
class MainController extends Controller
{

    public function homepageAction(Request $request, SessionInterface $session, ?string $token)
    {
        if ($token) {
            $session->set('token', $token);
        }

        ...Some work
    }
}

I tryed all the things all of you suggest,and nothing convice me, so reading a post in a forum I fount this line: 我尝试了所有人提出的所有建议,但没有任何建议可以帮助我,因此在论坛上阅读我的帖子时,请注意以下内容:

php_value session.auto_start 0

I put that in my .htaccess file and Symfony doesn´t overwrite the $_SESSION variable anymore and I can access to it everywhere in the controllers. 我将其放在.htaccess文件中,Symfony不再覆盖$_SESSION变量,并且可以在控制器中的任何位置访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM