添加数据以返回 Symfony 控制器中的所有操作 [英] Add data to return of all actions in a Symfony controller

查看:72
本文介绍了添加数据以返回 Symfony 控制器中的所有操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony 2.1 应用程序中有一个控制器,让我们在 BarBundle 中将其称为 FooController.这个控制器有很多动作 fooActionbarActionbazAction 等等.

I have a controller in a Symfony 2.1 application, let's just call it FooController in the BarBundle. This controller has a lot of actions fooAction, barAction, bazAction and a few more.

他们都有一些共同点.它们在视图中的某些部分显示相同的数据,而不是全部显示,所以我不能只使用一个动作,将类型作为参数.我想把必须传递给视图的数据集中在一个地方,否则就不会干了.

All of them have something in common. They're displaying in some part's the same data in the view, not in all, so I can't just use one action with the type as parameter. I would like to add the data that has to be passed to the view in one central place, otherwise, it just wouldn't be dry.

不好(在我看来):

public function fooAction() {
    // ...
    return $this->render('BarBundle:Foo:foo.html.twig', array('foo' => 'Foo Data', 'data' => $this->getTheDataThatIsNeededInEveryAction()));
}
public function barAction() {
    // ...
    return $this->render('BarBundle:Foo:bar.html.twig', array('bar' => 'Bar Data', 'data' => $this->getTheDataThatIsNeededInEveryAction()));
}
public function bazAction() {
    // ...
    return $this->render('BarBundle:Foo:baz.html.twig', array('baz' => 'Baz Data', 'data' => $this->getTheDataThatIsNeededInEveryAction()));
}

我现在想知道的是,什么是好"的方式?在将数据发送到视图之前调用的父控制器中是否有一个 finished 函数,我可以在那里将该数据添加到响应对象中?

What I'm wondering now, is, what would be the "Good" way? Is there like a finished function in the parent controller that is called just before sending the data to the view, where I could add that data to the response object?

另一种可能性是,创建一个事件侦听器,但我认为这会浪费资源.

Another possibility would be, to create an event listener, but I think that would be a waste of resources.

第三种选择是使用渲染函数,如 {% render url('latest_articles', { 'max': 3 }) %}我知道现在它是 {{ render(controller(..)) }} 但我在这个项目中坚持使用 Symfony 2.1.

A third option would be to use the render function like {% render url('latest_articles', { 'max': 3 }) %} I know nowadays it's {{ render(controller(..)) }} but I'm stuck with Symfony 2.1 in with this project.

推荐答案

一种选择是创建您自己的基本控制器,并让所有其他控制器对其进行扩展.您的基本控制器将覆盖 symfony 框架中 Controller 类的 render 函数

One option is to create your own base controller, and have all other controller extend it. Your base controller would override the render function of the Controller class from symfony framework

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BaseController extends Controller
{
    // override render method
    protected function render($template, $data)
    {
        $commonData = [];// get data from wherever you need
        parent::render($template, $data + $commonData);
    }
}

然后在你的其他控制器中

Then in your other controllers

class MyAnotherController extends BaseController
{
    public function fooAction() {
    // ...
    return $this->render('BarBundle:Foo:foo.html.twig', array('foo' => 'Foo Data'));
    }
}

这篇关于添加数据以返回 Symfony 控制器中的所有操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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