简体   繁体   English

如何获得 object 是 Symfony 中的每个响应?

[英]How can I get an object is every response in Symfony?

Context语境

I have an entity User and an entity Cart in my project.我的项目中有一个实体User和一个实体Cart There is a ManyToOne relation between both entities:两个实体之间存在ManyToOne关系:

/* /src/Entity/User.php */

/**
* @ORM\OneToMany(targetEntity="App\Entity\Cart", mappedBy="user", orphanRemoval=true)
*/
private $carts;
/* /src/Entity/Cart.php */

/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="carts")
* @ORM\JoinColumn(nullable=false)
*/
private $user;

I also have a method in my CartRepository that returns the current cart of the user.我的CartRepository中还有一个方法,它返回用户的当前购物车。

/*src/Repository/CartRepository.php */

/**
* @param User $user
* @return mixed
* @throws NonUniqueResultException If the user somehow has several non-validated
* carts bidden to their account.
*/
public function findCurrentUserCart(User $user)
{
    /* ... */
}

My Problem我的问题

What I would like to do is to be able to access easily the current count of cart items in the current user cart, so I can display it in my navbar.我想做的是能够轻松访问当前用户购物车中的当前购物车项目数,以便我可以在导航栏中显示它。 To do so, I need to be able to access the user's current cart easily.为此,我需要能够轻松访问用户当前的购物车。

I thought about getting it through the controller, but it seems quite annoying because, as the information is requested on all the pages of the site (because it is in the navbar) then I would need to retrieve the count from every single action of the controllers.我想过通过 controller 来获取它,但这似乎很烦人,因为在网站的所有页面上都需要该信息(因为它在导航栏中),所以我需要从每个操作中检索计数控制器。

I then thought about having a simple function in my User entity like getCartTotalCount() but this would imply using the CartRepository directly from my User entity which, IMHO, feels wrong (I mean, my entity is a plain object and I don't feel like getting a repository is a good way to go, is it?)然后我考虑在我的User实体中使用简单的 function,例如getCartTotalCount()但这意味着直接从我的User实体使用CartRepository ,恕我直言,感觉不对(我的意思是,我的实体是一个普通的 object,我不觉得就像获取存储库是 go 的好方法一样,是吗?)

So my question is: Is there a way to globally get the current cart object, so I can have it in all my views, without needing to passing it in any single render() method in my controllers?所以我的问题是:有没有办法全局获取当前的购物车 object,所以我可以在我的所有视图中拥有它,而无需在我的控制器中的任何单个render()方法中传递它? Or maybe is there another way to go?或者也许有另一种方法来 go?

Presuming you're using twig to render your templates, you have to create an extension.假设您使用 twig 来呈现您的模板,您必须创建一个扩展。 Declaring a twig extension as a service you can inject the entity manager or the repository directly in the service constructor.将 twig 扩展声明为服务,您可以直接在服务构造函数中注入实体管理器或存储库。

Here's the documentation on symfony docs on how to create a twig extension: https://symfony.com/doc/current/templating/twig_extension.html Here's the documentation on symfony docs on how to create a twig extension: https://symfony.com/doc/current/templating/twig_extension.html

In this case you need to create a custom twig function.在这种情况下,您需要创建自定义 twig function。

Even if the Twig extension looks like a feasible solution, I'd do it differently: using an embedded controller, you have more control.即使 Twig 扩展看起来像是一个可行的解决方案,我也会采取不同的做法:使用嵌入式 controller,您拥有更多控制权。 Place the cart template snippet in a distinct file, create a controller that renders this tiny piece, and call it using将购物车模板片段放在一个不同的文件中,创建一个 controller 来渲染这个小片段,然后使用

{{ render(controller(
    'App\\Controller\\CartController::cartNavbar'
)) }}

If the template to display this information grows (for example, as you want to use an icon for an empty cart, and another one if something is placed in the cart), you will see that this helps to seperate concerns better than using an extension如果显示此信息的模板增加(例如,当您想为空购物车使用一个图标,如果购物车中有东西则使用另一个图标),您会发现这比使用扩展更有助于分离关注点

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

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