错误:调用非对象上的成员函数get() [英] Error: Call to a member function get() on a non-object

查看:1192
本文介绍了错误:调用非对象上的成员函数get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Swift_Message发送邮件,但是当我发送数据时,它不会发送,我收到了错误

I am trying to send mail with Swift_Message however when I go to send the data it will not send and I get an error of


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

FatalErrorException: Error: Call to a member function get() on a non-object in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 252

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

Here is the Email Controller that I am 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 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(...

不工作,我缺少什么,这似乎应该是很直接。

but that also does not work. What am I missing. This seems like it should be really straight forward.

我从一个不同的包调用这个函数使用一个动作来调用当前控制器

I am calling this function from a different bundle using an action to call the current controller. I don't know if that makes a difference.

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

Ok so when looking in /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);
    }
}

这使我感觉像'mailer; $ id,但它用于Symfony的例子和许多其他私人示例。

Which makes me feel like 'mailer; is not a good $id but it is used in Symfony's examples and in a lot of other private examples.

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

Don't know if this helps or not but figured it was worth mentioning.

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

Could this be because of the swiftmailer: setting inside of my config.yml file?

routes.yml文件

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}






这是调用

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(),)); }

This is the service that allows me to call on the 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 is the FuelEmailBundle.php






这是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 are the changes that were made to the sendEmail function 

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

public function sendEmail($message, $urlAlias){ $container = FuelEmailBundle::getContainer(); $mailer = $container->get('mailer'); $mailer->send($message); return $this->redirect($this->generateUrl($urlAlias)); }



推荐答案

您收到错误,因为容器未设置。解决这个问题的一种方法是将一个容器实例传递给你的包,以便你可以从项目中的任何地方调用该容器。

Edit the class corresponding to your bundle(BundleName.php) to include two methods setContainer and getContainer. See the example below.

编辑与你的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; } }

Use the appropriate namespaces.
Then, use the namespace for the bundle in classes where you need the container. 
You may call the container by

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

$container = VenomCoreBundle::getContainer();

$ container = VenomCoreBundle :: getContainer(); p>

然后,调用mailer

Then, call the mailer

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

这篇关于错误:调用非对象上的成员函数get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆