php - Error: Call to a member function get() on a non-object -


i trying send mail swift_message when go send data not send , error of

fatalerrorexception: error: call member function get() on non-object in /vagrant/vendor/symfony/symfony/src/symfony/bundle/frameworkbundle/controller/controller.php line 252

here email controller using.

use symfony\component\finder\shell\command; use symfony\component\httpfoundation\request; use symfony\component\console\output\outputinterface; use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\dependencyinjection\containerinterface;  class emailcontroller extends controller{      public function createmessage($subject, $from, $from_name, $to, $to_name, $body){         // create message         $message = \swift_message::newinstance()              // give message subject             ->setsubject($subject)              // set address associative array             ->setfrom(array($from => $from_name))              // set addresses associative array             ->setto(array($to => $to_name))              // give body             ->setbody($body, 'text/html');          return $message;     }      public function sendemail($message, $urlalias){          $this->get('mailer')->send($message);          return $this->redirect($this->generateurl($urlalias));     } } 

i understand unable access object think part of container class can seem pull up. have tried using $this->container->get(...

but not work. missing. seems should straight forward.

i calling function different bundle using action call current controller. don't know if makes difference.


ok when looking in /vagrant/vendor/symfony/symfony/src/symfony/bundle/frameworkbundle/controller/controller.php

the line errors on

   /**      * gets service id.      *      * @param string $id service id      *      * @return object service      */     public function get($id)     {         return $this->container->get($id);     } } 

which makes me feel 'mailer; not $id used in symfony's examples , in lot of other private examples.

don't know if helps or not figured worth mentioning.

could because of swiftmailer: setting inside of config.yml file?

routing.yml file

fuel_form_homepage:     pattern:  /hello/{name}     defaults: { _controller: fuelformbundle:default:index }  referral_form:     pattern:   /form/referral/{hash}     defaults: { _controller: fuelformbundle:form:referralform }  referral_result:     pattern:  /form/referral/result     defaults: { _controller: fuelformbundle:form:referralresult }  user_form:     pattern:    /form/user     defaults: { _controller: fuelformbundle:form:userform }  home:     pattern:    /     defaults: { _controller: fuelformbundle:default:home} 

this function calls

public function userformaction(request $request){         $user = new user();         $form = $this->createform('user', $user);          $form->handlerequest($request);         if($form->isvalid()){             $user->settimecreated();             $user->settimeupdated();             $date = $user->gettimecreated();             $timestamp = $date->format("u");             $hash = $user->getfirstname() . $user->getlastname() . $timestamp ;             $user->setuserhash(md5($hash));             $em = $this->getdoctrine()->getmanager();             $em->persist($user);             $em->flush();             print_r($user);             //todo: @email: @body: make sure replace correct information.              //calls service named email_bundle_controller             $emailcontroller = $this->get('email_bundle_controller');              $fullname = $user->getfirstname() . $user->getlastname();             $body = "please visit following url start referring! <a href='http://localhost:8080/app_dev.php/form/referral/" . $user->getuserhash() . "'>your url</a>";             $message = $emailcontroller->createmessage('welcome fuel prm references', 'bsaverino@gmail.com', 'brad saverino', $user->getemail(), $fullname, $body);             $emailcontroller->sendemail($message, 'user_form');          }          return $this->render('fuelformbundle:default:mainform.html.twig', array('form' => $form->createview(),));      } 

this service allows me call on other bundle.

services:     fuel_form.form.type.referral:         class: fuel\formbundle\form\type\referraltype         tags:             - { name: form.type, alias: referral}      fuel_form.form.type.user:         class: fuel\formbundle\form\type\usertype         tags:             - { name: form.type, alias: user}      email_bundle_controller:         class: fuel\emailbundle\controller\emailcontroller 

this fuelemailbundle.php

namespace fuel\emailbundle;  use symfony\component\httpkernel\bundle\bundle; use \symfony\component\dependencyinjection\containerinterface;  class fuelemailbundle extends bundle {     private static $containerinstance = null;      public function setcontainer(containerinterface $container = null)     {         parent::setcontainer($container);         self::$containerinstance = $container;     }      public static function getcontainer()     {         return self::$containerinstance;     } } 

these changes made sendemail function

public function sendemail($message, $urlalias){          $container = fuelemailbundle::getcontainer();          $mailer = $container->get('mailer');          $mailer->send($message);          return $this->redirect($this->generateurl($urlalias));     } 

as cerad had mentioned above, getting error container not set. 1 way of fixing issue pass container instance bundle can call container anywhere in project.

edit class corresponding bundle(bundlename.php) include 2 methods setcontainer , getcontainer. see example below.

namespace venom\corebundle;  use symfony\component\httpkernel\bundle\bundle; use \symfony\component\dependencyinjection\containerinterface;  class venomcorebundle extends bundle {    private static $containerinstance = null;      public function setcontainer(containerinterface $container = null)     {      parent::setcontainer($container);      self::$containerinstance = $container;     }     public static function getcontainer()     {      return self::$containerinstance;     }  } 

use appropriate namespaces. then, use namespace bundle in classes need container. may call container by

$container = venomcorebundle::getcontainer();

then, call mailer

$mailer = $container->get('mailer');


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -