How to get current bundle in Symfony 2?

后端 未结 4 1008
盖世英雄少女心
盖世英雄少女心 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

    0 讨论(0)
  • 2021-02-03 11:59

    In order to get the bundle name in the controller:

    // Display "AcmeHelloBundle"
    echo $this->getRequest()->attributes->get('_template')->get('bundle');
    

    And inside a Twig template:

    {{ app.request.get('_template').get('bundle') }}
    

    In order to get the controller name in the controller:

    // Display "Default"
    echo $this->getRequest()->attributes->get('_template')->get('controller');
    

    And inside a Twig template:

    {{ app.request.get('_template').get('controller') }}
    

    In order to get the action name in the controller:

    // Displays "index"
    echo $this->getRequest()->attributes->get('_template')->get('name');
    

    And inside a Twig template:

    {{ app.request.get('_template').get('name') }}
    
    0 讨论(0)
  • 2021-02-03 12:03

    Well you can get the controller of the current route by,

    $request->attributes->get('_controller');
    

    You can parse the bundle name from it.

    0 讨论(0)
  • 2021-02-03 12:04

    You can get the bundle name in the controller simply like that:

    // Display "SybioCoreBundle"
    echo $this->getRequest()->attributes->get('_template')->get('bundle');
    

    And inside a Twig template:

    {{ app.request.get('_template').get('bundle') }}
    
    0 讨论(0)
提交回复
热议问题