如何在Symfony 2中获取当前捆绑包?

Ber*_*nat 9 symfony

我怎样才能发现我是哪一捆?

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

小智 15

为了在控制器中获取包名称:

// Display "AcmeHelloBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
Run Code Online (Sandbox Code Playgroud)

在Twig模板中:

{{ app.request.get('_template').get('bundle') }}
Run Code Online (Sandbox Code Playgroud)

为了在控制器中获取控制器名称:

// Display "Default"
echo $this->getRequest()->attributes->get('_template')->get('controller');
Run Code Online (Sandbox Code Playgroud)

在Twig模板中:

{{ app.request.get('_template').get('controller') }}
Run Code Online (Sandbox Code Playgroud)

要在控制器中获取操作名称:

// Displays "index"
echo $this->getRequest()->attributes->get('_template')->get('name');
Run Code Online (Sandbox Code Playgroud)

在Twig模板中:

{{ app.request.get('_template').get('name') }}
Run Code Online (Sandbox Code Playgroud)

  • 非常肯定` - > attributes-> get('_ template')`如果你不使用@template注释就不可用. (6认同)

gre*_*emo 7

AFAIK还不可能(至少以一种简单的方式).你应该使用反射.我写了一个快速而又脏的服务来根据我的约定获得bundle name ang guess实体/存储库/表单名称.可以马车,看看:http://pastebin.com/BzeXAduH

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

entity_management_guesser:
  class: Acme\HelloBundle\Service\EntityManagementGuesser
Run Code Online (Sandbox Code Playgroud)

在你的控制器中:

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

$bundleName  = $guesser->getBundleName();      // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle
Run Code Online (Sandbox Code Playgroud)

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


Mun*_*Das 5

那么你可以得到当前路线的控制器,

$request->attributes->get('_controller');
Run Code Online (Sandbox Code Playgroud)

您可以从中解析包名称.