How to get current bundle in Symfony 2?

后端 未结 4 1007
盖世英雄少女心
盖世英雄少女心 2021-02-03 11:09

How can I detect in which bundle am I?

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

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-03 11:51

    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

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

    entity_management_guesser:
      class: Acme\HelloBundle\Service\EntityManagementGuesser
    

    In your controller:

    $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

提交回复
热议问题