如何将控制器变量传递给 Symfony2 中的事件监听器?

发布于 2024-12-17 04:00:02 字数 741 浏览 2 评论 0原文

我厌倦了在 Symfony2 中的每个操作的末尾编写此内容:

return $this->render('Project:Bundle:view.twig', array(
                                     'foo' => 1,
                                     'bar' => 2
                                 ));

因此,我尝试在操作运行后立即插入请求生命周期,以节省自己的打字时间。我希望能够在我的控制器中执行类似的操作:

$this->params = array(
    'foo' => 1,
    'bar' => 2
);

然后侦听器会将参数传递给渲染器,并使用操作名称自动检测模板。我意识到我需要使用事件监听器来实现这一点,但我似乎无法在正确的时间挂钩生命周期...

  • kernel.controller 很好,因为我可以访问控制器,但这是在操作运行之前,因此不会设置 $this->params
  • kernel.response 是在操作运行之后,但我似乎无法访问控制器仅供

参考 - 我有Zend 背景,这是(obv)我第一次使用 Symfony2...如果我以完全错误的方式解决这个问题,请大喊!

I'm bored of writing this at the end of every action in Symfony2:

return $this->render('Project:Bundle:view.twig', array(
                                     'foo' => 1,
                                     'bar' => 2
                                 ));

So I've attempted to hook into the request lifecycle just after an action has been run, in order to save myself some typing. I want to be able to do something similar to this in my controller instead:

$this->params = array(
    'foo' => 1,
    'bar' => 2
);

A listener would then pass the params to the render, and auto-detect the template using the action name. I realise I need to use Event Listeners to achieve this, but I can't seem to hook into the lifecycle at the right time...

  • kernel.controller is good, because I can get at the controller, but it's before the action has been run, so $this->params won't be set
  • kernel.response is after the action has run, but I can't seem to get at the controller itself from here

FYI - I've got a Zend background, and this is (obv) my first time using Symfony2... If I'm approaching this problem in entirely the wrong manner, shout!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-12-24 04:00:02

如果您使用的是 SensioFrameworkExtraBundle,则可以使用 @Template() 注解并返回一个数组:

<?php

namespace Acme\FooBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class BarController
{
    /**
     * @Template()
     */
    public function bazAction()
    {
        return array(
            'some_value' => $someValue;
        );
    }
}

该注解告诉它根据包、控制器和操作名称在默认位置查找视图(在本例中,AcmeFooBundle:Bar:baz.html.twig)。

If you're using the SensioFrameworkExtraBundle, you can use the @Template() annotation and return an array:

<?php

namespace Acme\FooBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class BarController
{
    /**
     * @Template()
     */
    public function bazAction()
    {
        return array(
            'some_value' => $someValue;
        );
    }
}

The annotation tells it to look for the view in the default location based on bundle, controller and action name (in this case, AcmeFooBundle:Bar:baz.html.twig).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文