本文介绍了如何在 Symfony 2 中获取当前包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测我在哪个包中?

How can I detect in which bundle am I?

例如,当我在 web.com/participants/list 时,我想阅读参与者".

for exemple, when I'm in web.com/participants/list, I want to read "participants".

推荐答案

AFAIK 目前还不可能(至少在一个简单的方式中).你应该使用反射.我写了一个快速而肮脏的服务来根据我的约定来获取包名称和猜测实体/存储库/表单名称.可能有问题,看看:http://pastebin.com/BzeXAduH

AFAIK it's not yet possible (at least in a easy way). You should use reflection. I wrote a quick and dirty service to do get bundle name ang guess entity/repository/form names based on my conventions. Can be buggy, take a look at: http://pastebin.com/BzeXAduH

它仅在您传递从 Controller (Symfony2) 继承的类时才有效.用法:

It works only when you pass a class that inherits from Controller (Symfony2). Usage:

entity_management_guesser:
  class: Acme\HelloBundle\Service\EntityManagementGuesser

在您的控制器中:

$guesser = $this->get('entity_management_guesser')->inizialize($this);

$bundleName  = $guesser->getBundleName();      // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle

另一种可能性是使用内核获取所有包:获取包名称来自实体

Another possibility would be using kernel to get all bundles: Get a bundle name from an entity

这篇关于如何在 Symfony 2 中获取当前包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:24