自分がどのバンドルに含まれているかをどのように検出できますか?
たとえば、私がweb.com/participants/listにいるときは、「参加者」を読みたいと思います。
自分がどのバンドルに含まれているかをどのように検出できますか?
たとえば、私がweb.com/participants/listにいるときは、「参加者」を読みたいと思います。
コントローラでバンドル名を取得するには、次のようにします。
// 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') }}
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つの可能性は、カーネルを使用してすべてのバンドルを取得することです。エンティティからバンドル名を取得します。
さて、あなたは現在のルートのコントローラーを取得することができます、
$request->attributes->get('_controller');
そこからバンドル名を解析できます。
コントローラでバンドル名を取得するには、次のようにします。
// Display "SybioCoreBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
そしてTwigテンプレートの内部:
{{ app.request.get('_template').get('bundle') }}