简体   繁体   English

symfony3树枝和表演会话数组

[英]symfony3 twig and show session array

I have array in session, but I can't show them. 我在会话中有数组,但无法显示。 My code: Controller: 我的代码:控制器:

  $game = $request->query->get('game');
  $type = $request->query->get('type');
  $odd = $request->query->get('odd');

  $kupon = array(
                 'game' => $game,
                 'type' => $type,
                 'odd' => $odd,
               );

   $this->get('session')->set('kupon', $kupon);

Where 'game' is for example: 'Arsenal - Chelsea', 'type' is number like 1, and odd is float number like '2.2'. 例如,“游戏”为:“阿森纳-切尔西”,“类型”为数字,如1,奇数为浮点数,如“ 2.2”。

Twig file: 树枝文件:

{% if app.session.get('kupon') is not null %}
<table>
{% for kupon in session %}
    <tr>
      <td>{{ kupon.game }}</td>
      <td>{{ kupon.type }}</td>
      <td> </td>
    </tr>
{% endfor %}
</table>

And everything ok, but when I try log in and show data session, I have this error: 一切正常,但是当我尝试登录并显示数据会话时,出现此错误:

Impossible to access an attribute ("game") on a string variable ("PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574") in baw\\kupon.html.twig at line 13. 在第13行的baw \\ kupon.html.twig中,无法访问字符串变量(“ PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574”)上的属性(“游戏”)。

This variable is from session after log in, I checked dump information about it: 此变量来自登录后的会话,我检查了有关它的转储信息:

array(3) { 
["_csrf/login"]=> string(43) "PNdjNUeuZ_d5uJlm1VG7zPZhp2Vb4CY3nDf93vAQ574" ["login"]=> string(4) "test" 
["kupon"]=> array(3) { ["game"]=> string(31) "Arsenal Londyn - Chelsea Londyn" ["type"]=> string(1) "1" ["odd"]=> string(3) "2.2" } }

And now I haven't any idea to fix this. 现在我还不知道要解决此问题。

{% for kupon in session %} will not loop over app.session.get('kupon') {% for kupon in session %}将不会遍历app.session.get('kupon')

what you want to do is: 您想要做的是:

{% for kupon in app.session.get('kupon') %}

But looking at you dumped data, app.session.get('kupon') is just a single dataset, so you cant even loop that (with desired results)... 但是查看您转储的数据, app.session.get('kupon')仅仅是一个数据集,因此您甚至无法循环播放(具有所需的结果)...

it will be just: 只是:

{{ app.session.get('kupon').game }}

extra explanations about your data: you have this in you session: 有关数据的其他说明:您的会话中有以下内容:

"kupon" => [
    "game" => ...
    "type" => ...
    ...
]

to be able to loop over these, you need to make a collection of your data types: 为了能够遍历这些,您需要收集数据类型:

"kupon" => [
    [
        "game" => ...
        "type" => ...
        ...
    ],
    [
        "game" => ...
        "type" => ...
        ...
    ],
    ....
]

Try below 试试下面

{% if app.session.get('kupon') is not null %}
  {% set kupon = app.session.get('kupon') %}
  <table>
      <tr>
        <td>{{ kupon.game }}</td>
        <td>{{ kupon.type }}</td>
        <td> </td>
      </tr>
  </table>
{% endif %}

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

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