10

自分がどのバンドルに含まれているかをどのように検出できますか?

たとえば、私がweb.com/participants/listにいるときは、「参加者」を読みたいと思います。

4

4 に答える 4

15

コントローラでバンドル名を取得するには、次のようにします。

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

Twigテンプレート内:

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

コントローラでコントローラ名を取得するには、次のようにします。

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

Twigテンプレート内:

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

コントローラでアクション名を取得するには、次のようにします。

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

Twigテンプレート内:

{{ app.request.get('_template').get('name') }}
于 2013-01-23T08:42:45.153 に答える
7

AFAIKそれはまだ可能ではありません(少なくとも簡単な方法で)。リフレクションを使用する必要があります。私は、慣例に基づいてバンドル名と推測エンティティ/リポジトリ/フォーム名を取得するための迅速で汚いサービスを作成しました。バグがある可能性があります。http://pastebin.com/BzeXAduHをご覧ください。

これは、Controller(Symfony2)から継承するクラスを渡す場合にのみ機能します。使用法:

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

もう1つの可能性は、カーネルを使用してすべてのバンドルを取得することです。エンティティからバンドル名を取得します。

于 2012-05-11T09:07:02.310 に答える
6

さて、あなたは現在のルートのコントローラーを取得することができます、

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

そこからバンドル名を解析できます。

于 2012-05-11T11:45:07.690 に答える
3

コントローラでバンドル名を取得するには、次のようにします。

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

そしてTwigテンプレートの内部:

{{ app.request.get('_template').get('bundle') }}
于 2013-01-10T20:42:36.503 に答える