2017-10-18 1 views
0

にsymfonyのサービス注入は、以下の技術を使用してアクションで私に利用可能:アクション

http://symfony.com/doc/current/service_container.html#fetching-and-using-services

public function submitAction (Request $request, \Swift_Mailer $mailer) 
{ 
     $mailer = $this->get('mailer'); 
     var_dump($mailer); 
} 

これはエラーです:

Controller "AppBundle\Controller\QuoteController::submitAction()" requires that you provide a value for the "$mailer" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

var_dumpはこれが空ではないことを私に伝えます - 私は最近Symfony 3.3.10にアップグレードし、services.ymlを以下のように変更しました:

services: 
    _defaults: 
    autowire: true 
    autoconfigure: true 
    public: true 

答えて

2

サービスはコンストラクタに通常(自動的に)挿入されます。

それらはまた、自動的にアクションメソッドのパラメータに入れることができるが、[コントローラがそれを可能にするためにタグ付けされなければならない]。(http://symfony.com/doc/current/service_container/3.3-di-changes.html

# controllers are imported separately to make sure they're public 
# and have a tag that allows actions to type-hint services 
AppBundle\Controller\: 
    resource: '../../src/AppBundle/Controller' 
    tags: ['controller.service_arguments'] 

要求(および3.2以降ので、SessionInterface及びユーザー/ UserInterfaceなど)は、組み込みのPHP Reflection APIで検出されます。 ArgumentResolverSessionValueResolverおよびServiceValueResolverを使用)を使用して、既存のParameterConverter(アクションメソッドのパラメータのパラメータIDからエンティティを取得するために使用)を超えて、認識して自動的に追加するための、より特殊化されたサブシステムがあります。

+0

私はそれがそうであったかどうか疑問に思っていました。しかし、コントローラがタグ付けされていないとリクエストがどのように注入されるのでしょうか? –

+0

ちょうど情報のため、リクエストオブジェクトは常に特別なケースとして扱われており、新しいアクションインジェクション機能には直接関係していません。 symfonyはネストされたリクエストをサポートしており、アクションが現在のリクエストを受け取ることが重要であるため、リクエストは少しユニークです。 – Cerad