【问题标题】:Error: Call to a member function get() on a non-object错误:在非对象上调用成员函数 get()
【发布时间】:2013-09-24 20:54:42
【问题描述】:

我正在尝试使用 Swift_Message 发送邮件,但是当我发送数据时它不会发送并且我收到一个错误

FatalErrorException: 错误: 调用成员函数 get() 非对象在 /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 第 252 行

这是我正在使用的电子邮件控制器。

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 the message
        $message = \Swift_Message::newInstance()

            // Give the message a subject
            ->setSubject($subject)

            // Set the From address with an associative array
            ->setFrom(array($from => $from_name))

            // Set the To addresses with an associative array
            ->setTo(array($to => $to_name))

            // Give it a body
            ->setBody($body, 'text/html');

        return $message;
    }

    public function sendEmail($message, $urlAlias){

        $this->get('mailer')->send($message);

        return $this->redirect($this->generateUrl($urlAlias));
    }
}

我知道它无法访问我认为是容器类的一部分的对象,但我似乎可以让它拉起来。我试过使用$this->container->get(...

但这也行不通。我错过了什么。这似乎应该是非常直截了当的。

我正在使用调用当前控制器的操作从不同的包中调用此函数。我不知道这是否有区别。


好的,当查看 /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 时

错误所在的行是

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

这让我觉得自己像个“邮寄者”;不是一个好的 $id 但它在 Symfony 的示例和许多其他私有示例中使用。

不知道这是否有帮助,但认为值得一提。

这可能是因为 swiftmailer: 设置在我的 config.yml 文件中吗?

routing.yml 文件

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}

这是调用的函数

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 to replace with correct information.

            //Calls a service named email_bundle_controller
            $emailController = $this->get('email_bundle_controller');

            $fullName = $user->getFirstName() . $user->getLastName();
            $body = "please visit the following url to start referring! <a href='http://localhost:8080/app_dev.php/form/referral/" . $user->getUserHash() . "'>Your URL</a>";
            $message = $emailController->createMessage('Welcome to 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(),));

    }

这是允许我调用另一个捆绑包的服务。

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

这是 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;
    }
}

这些是对 sendEmail 函数所做的更改

public function sendEmail($message, $urlAlias){

        $container = FuelEmailBundle::getContainer();

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

        $mailer->send($message);

        return $this->redirect($this->generateUrl($urlAlias));
    }

【问题讨论】:

  • 如果我把所有东西都放在同一个控制器中,我可以让它正常工作。
  • 你能发布你的路线定义吗?
  • 是routing.yml文件吗?刚刚添加。
  • 好的,所以当sendMail 被调用时,它是来自事件挂钩还是您实际上是在向该控制器生成请求?
  • 错误是因为没有设置容器。您正在尝试新的电子邮件控制器,这就是未设置容器的原因。发布一些代码,展示如何调用 createMessage。

标签: php symfony controller


【解决方案1】:

正如 Cerad 上面提到的,由于未设置容器,您会收到错误消息。解决此问题的一种方法是将容器实例传递给您的包,以便您可以从项目中的任何位置调用该容器。

编辑与你的bundle(BundleName.php) 对应的类以包含两个方法setContainer 和getContainer。请参阅下面的示例。

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; 
   }
 }

使用适当的命名空间。 然后,在需要容器的类中使用包的命名空间。 您可以通过

调用容器

$container = VenomCoreBundle::getContainer();

然后,打电话给邮件

$mailer = $container-&gt;get('mailer');

【讨论】:

  • 这不起作用。我仍然遇到同样的错误。我将上面的代码输入到我的 php 包中。我将它包含在我的包中,我设置了 $container 并使用它,但我仍然得到同样的错误。当所有东西都在同一个包和控制器中时,我可以让它工作,但是一旦我将它们移动到单独的包中,它们就不起作用了。我是否需要在调用函数的包中执行此操作,而不是在它自己的实际函数中执行此操作。我不知道我会如何做到这一点。
  • 但是,如果我使用 $container = FuelFormBundle::getContainer(); 并像 $container-&gt;sendEmail($message, 'user_form'); 这样调用函数,则会收到错误 FatalErrorException: Error: Call to undefined method appDevDebugProjectContainer::sendEmail() in /vagrant/src/Fuel /FormBundle/Controller/FormController.php 第 76 行。这是有道理的,因为现在它无法找到该函数。我错过了一些东西。
  • 对于其他搜索此问题的人,如果您尝试通过 $this->forward( '..newcontroller' ) 在控制器中执行子请求,我发现容器未传递给子控制器.使用 Praveesh 的上述解决方案可以解决这个问题,我认为这是 symfony 中的一个错误。
  • @BRad 你最后是怎么解决的?
猜你喜欢
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多