2016-02-28 40 views

回答

2

看來你controller is not defined as a service,這樣你就可以在控制器中使用的服務爲:

/** 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function sendAction(Request $request) 
    { 
     $notification = new Notification(); 
     $user = $this->get('security.token_storage')->getToken()->getUser(); 
     $notification->setBody($request->request->get('subject'), $user->getUsername()); 
     $notification->setSubject($request->request->get('body')); 
     try { 
      $this->get('emailsender')->send($notification); 
     } catch (\Exception $ex) { 
      $this->render('MailerBundle:Email:error.html.twig', [ 
       'message' => $ex->getMessage() 
      ]); 
     } 
     $this->render('MailerBundle:Email:success.html.twig'); 
    } 

希望這個幫助

+0

還做我的項目是如何在菜譜 – user3058157

+0

寫現在我有500 HTTP錯誤 – user3058157

+0

做這樣的: $容器 - > setDefinition( 'app.hello_controller',新的定義( '的appbundle \控制器\ HelloController中', 陣列(新參考( '模板')) )); – user3058157