2012-04-12 6 views

답변

1

컨트롤러에서 $this->get('translator')을 수행 할 수 있습니다. 더 많은 정보 here. 컨트롤러 부분을 이해하지 못했습니다.

편집 : 서비스 구현이 될 것

샘플, 클래스에서

namespace Your\Class\Namespace; 
use Symfony\Component\Translation\TranslatorInterFace; 
use Symfony\Component\HttpFoundation\Request; 


class YourClass 
{ 

    private $translator; 
    private $request; 

    public function __construct(TranslatorInterFace $translator, Request $request) 
    { 
     $this->translator = $translator; 
     $this->request = $request; 
    } 

    public function aMethod(){ 
     $translator->trans('key'); 
     $session = $this->request->getSession(); 
    } 
    //..... 
} 

추가 다음 services.yml

my_service: 
    scope: request 
    class: FQCN\Of\YourClass 
    arguments: [@translator, @request] 

의 선 그리고 컨트롤러는 현재 C 클래스를 사용하여 액세스,

$this->get('my_service'); 
+0

$ 이것은 컨트롤러 /보기에서만 작동합니다. 나는 도서관/다른 클래스에서 컨트롤러를 얻을 필요가있다. 이 작업을 수행하는 방법? – RKI

+0

클래스를 서비스 컨테이너로 정의하고'@ translator' 서비스를 삽입해야합니다. [여기] (http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services) –

+0

내 편집 된 댓글보기 –

관련 문제