Symfony2,如何像独立类一样创建小部件 [英] Symfony2, how create a widget like a standalone class

查看:34
本文介绍了Symfony2,如何像独立类一样创建小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们从基本的东西开始,简单的例子是 Yii.它有诸如小部件之类的东西.独立的、可配置的和可从我们想要类的任何地方调用.我想知道 symfony2 是否也一样?它会是什么?控制器捆绑?动作简单(方法)?带参数的小部件(twig)?

Let's start with basic thing, simple example is Yii. It has such thing as widgets. Standalone, configurable and callable from any place we want classes. And I'm wondering can symfony2 has the same? What it will be? Controller in bundle? Action simple (method)? Widget (twig) with parameters?

在 Yii 中,我们创建类(小部件),独立的,描述它并使用(通过调用模板).它在 symfony2 中会是什么样子?

In Yii we create class (of widget), standalone, describe it and use (by calling in template). How will it look like in symfony2?

简单示例我想使用小部件创建菜单导航,它将在其中按用户角色构建 html".

Simple example 'i want create menu navigation using widget, where it will construct html by user roles'.

推荐答案

Symfony 没有提供这样的功能,但是你可以自己制作.他们的方法很少.

Symfony doesn't provide such a feature however you can make them yourself. They are few ways of doing it.

我承认我们谈论的是可以做后端工作的小部件(即从数据库获取数据、调用 API 等).

I'll just admit that we are talking about widgets that could do backend work (i.e. get data from DB, call an API, etc.).

在 Twig 中使用 渲染标签您可以从模板调用控制器操作.它是可扩展的,因为你可以使用 esi 标签和 Varnish(您也可以实现自己的缓存配置文件).作为奖励,分析器将在时间线中显示有关特定渲染调用的详细信息(它看起来像是整个请求的子集).

The use of the render tag in Twig which allows you to call a controller action from a template. It's scalable because you could use esi tags with Varnish (you can also implement your own caching profiles). As a bonus, the profiler will show details about the specific render calls in the timeline (it will look like a subset of the entire request).

包含的模板通过 Twig 函数调用收集数据.根据经验,它比第一个解决方案快一点,但不容易扩展.

The included template gathers the data through a Twig function call. By experience, it's a bit faster than the first solution however it's not easily scalable.

twig 函数将获取数据并调用 template 服务的 renderView 方法.如果您打算这样做,您可能想使用第一种方法.

The twig function will get the data and call the renderView method of the template service. If you are planning on doing this, you probably want to use the first method.

如果您有一个包含大量流量(或点击")的模块/小部件的大型网站:请使用第一种解决方案.

If you have a big website with modules/widgets that gets a lot of traffic (or "hit"): use the first solution.

如果您有一个不支持缓存的小型网站:请使用第二种解决方案.如果模块/小部件超轻,您将使用此解决方案.

If you have a small website with no support for caching: use the second solution. You'd use this solution if the module/widget is super light.

如果您正在考虑第三种解决方案……使用第一种解决方案可能是个好主意.

If you are thinking about the third solution... it's probably a good idea to use the first solution.

就我个人而言,我将始终尝试使用第一种解决方案并尝试以某种方式提高性能.自 Symfony2 的最新版本以来,Twig 中的渲染调用得到了显着改进.

Personally, I'll always try to use the first solution and try to boost the performance one way or another. The render call in Twig has been significantly improved since the last versions of Symfony2.

希望我的回答能为您提供一些指导.

Hopefully, my answer will provide you some guidelines.

这篇关于Symfony2,如何像独立类一样创建小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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