如何使用自定义 Twig 扩展在 Symfony 中使用 Session [英] How to Use Session in Symfony using Custom Twig Extension

查看:19
本文介绍了如何使用自定义 Twig 扩展在 Symfony 中使用 Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么

  • 我只想在 symfony 中实现会话.

这是我尝试过的

  • /src/Acme/bundlename/Twig

  • /src/Acme/bundlename/Twig

Acmeextension.php

Acmeextension.php

public function getFunctions()
{
  return array(
'count'  => new Twig_Function_Method($this, 'count'),
 );
 }   

public function count()
{
    session_start();
    if(isset($_SESSION["count"]))
    {
        $accesses = $_SESSION["count"] + 1;
    }
    else
    {
        $accesses = 1;
    }
    $_SESSION["count"] = $accesses;
    return $accesses;
}

这里是twig的代码:

here is the code of twig:

function callback()
{
    var page = {{ count}};
    if (page >4)
    {
        alert("limit exceeded");            
    }
    else
    {
      alert("ok");                 
    }
}
callback();

calling in twig 
{{ count }}

  • 我只想计算浏览量.
  • 我在 twig 中使用了脚本,因此如果页面浏览量超过 4 次,则显示警报消息.
  • 我使用了 symfony 自定义函数,以便在 symfony 中使用 $SESSION.
  • 我已经在核心 PHP 中实现了这段代码,它工作正常.
  • 我参考了链接 将会话传递给 TWIG 模板.

    自定义 Twig 扩展 http://symfony.com/doc/current/cookbook/template/twig_extension.html

    custom Twig Extension http://symfony.com/doc/current/cookbook/templating/twig_extension.html

    问题

    • 当我重新加载页面时,我不会收到任何警报.
    • 请告诉我我哪里错了,欢迎提出任何建议.

    推荐答案

    如果您希望将超出范围含义的会话注入自定义扩展.

    If you are looking to inject sessions outside of the scope meaning into your custom extension.

    我会这样做.

         //AppKernel +add
          protected function initializeContainer() {
              parent::initializeContainer();
              if (PHP_SAPI == 'cli') {
                  $this->getContainer()->enterScope('request');
                  $this->getContainer()->set('request', new SymfonyComponentHttpFoundationRequest(), 'request');
              }
          }
    

    然后在你的服务容器中

          <!-- Custom Twig Extensions -->
          <service id="yourid" class="yourclasspath">
            <argument type="service" id="service_container" />
            <tag name="twig.extension" />
        </service>
    

    然后在你的 twig.php 中

    Then in your twig.php

         class Twig extends Twig_extension {
             private $request;
    
             public function __construct(Container $container) {
                  $this->request = $container->get('request');
             }
    
             public function getFunctions() {
               return array(
                'count'  => new Twig_Function_Method($this, 'count'),
               );
             }    
    
             public function count() {
             $session = $this->request->getSession();
    
             if(session->has('count')) {
                 $session->set('count') += 1;
             } else {
                 $session->set('count') = 1;
             }
    
             return $session->get('count');
          }
       }
    

    然后在你的树枝上也一样

    Then the same in your twig

    这篇关于如何使用自定义 Twig 扩展在 Symfony 中使用 Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆