错误:调用非对象上的成员函数 get()

2024-02-11

我正在尝试使用 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 文件中设置的吗?

路由.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', '[email protected] /cdn-cgi/l/email-protection', '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));
    }

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

编辑与您的包 (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->get('mailer');

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:调用非对象上的成员函数 get() 的相关文章

  • 正则表达式检查确切的字符串是否存在,包括#

    新问题正如 Asaph 在上一个问题中所建议的 正则表达式检查确切的字符串是否存在 https stackoverflow com questions 2824291 regex to check if exact string exist
  • 表单请求中的 Laravel 数组验证

    我无法验证 Form Request 类中包含数组元素的字段 规则方法 public function rules return state gt required state 0 gt required state gt required
  • 有没有办法在 Windows 上全局安装 Composer?

    我读过全局安装文档 http getcomposer org doc 00 intro md globally对于 Composer 但仅适用于 nix 系统 curl s https getcomposer org installer p
  • 此集合实例 Laravel 关系中不存在属性 [X]

    我在 Laravel 5 6 中使用了很多 Realtions 当我添加 phonebooks 时 我看到所有关系都工作正常 一切都很好 但是当我尝试在视图中显示它们时 我得到了属性在此集合上不存在的错误 这是关系代码 public fun
  • 从 Symfony2 中的服务重定向

    我有一项查找页面数据的服务 但如果找不到该数据 则应重定向到主页 对于我的一生 我不知道如何在 Sf2 中做到这一点 有很多不同的方法可以使用服务和路由器 但似乎都不起作用 namespace Acme SomeBundle Service
  • 回显 HTML 并内置 PHP

    请帮助我使用 echo 与 HTML 和 PHP 使用数组范围将其转换为动态
  • Codeigniter - 检查用户是否已登录并存在(它是真实用户)

    我正在尝试在用户登录我的网站时为他们设置会话数据 因此 如果用户存在于数据库中 我将设置一个会话数据 例如 this gt session gt set userdata user exists 1 现在 每次我想检查用户是否存在并已登录时
  • 如何正确转义反斜杠以匹配单引号和双引号 PHP 正则表达式模式中的文字反斜杠

    为了匹配字面上的反斜杠 很多人和PHP 手册 http www php net manual en regexp reference escape php说 总是三重转义吧 就像这样 Note 单引号和双引号 PHP 字符串具有反斜杠的特殊
  • PHP 特性 - 定义通用常量

    定义可由命名空间内的多个类使用的常量的最佳方法是什么 我试图避免过多的继承 因此扩展基类不是理想的解决方案 并且我正在努力寻找使用特征的良好解决方案 这在 PHP 5 4 中是否可行 或者应该采取不同的方法 我有以下情况 trait Bas
  • PHP preg_match_all 100 MB 文件

    我读到 preg match all 不是为解析大文件而设计的 但我需要这样做 我增加了 pcre backtrack limit 1000000000 pcre recursion limit 1000000000 我的 PHP memo
  • 自动安排并执行 PHP 脚本

    我编写了一个 PHP 脚本 它生成一个包含数据库中所有表的 SQL 文件 我想要做的是每天或每 n 天执行这个脚本 我读过有关 cron 作业的内容 但我使用的是 Windows 如何在服务器上自动执行脚本 您需要添加计划任务来调用 URL
  • 使用composer create-project安装特定的laravel 5版本

    今天我尝试安装特定的 laravel 版本composer create project laravel laravel 5 1 8 your project name prefer dist 因为有些插件在5 1 9及以上版本有问题 但是
  • 根据相同的 XML 模式 (XSD) 加速一批 XML 文件的 XML 模式验证

    我想加快根据同一个 XML 模式 XSD 验证一批 XML 文件的过程 唯一的限制是我处于 PHP 环境中 我当前的问题是 我想要验证的模式包括 2755 行的相当复杂的 xhtml 模式 http www w3 org 2002 08 x
  • PHP 中的 GOTO 命令?

    我听说 PHP 计划引入 goto 命令的传言 它应该做什么 我尝试搜索了一下 但没有找到任何具有描述性的内容 我明白这不会是 GOTO 10 类似命令 They are not adding a real GOTO but extendi
  • 如何在 PHP 中修剪定界文档(长字符串)中的每一行

    我正在创建一个 PHP 函数 可以修剪长字符串中的每一行 例如
  • PHP:检测USB设备

    我正在尝试使用 PHP 将用户名和密码存储到 USB 拇指驱动器上的文本文件中 因此 当用户返回使用 USB 密钥登录时 应该会打开一个弹出窗口 并提示输入用户名和密码 所以我的问题是如何使用 PHP 检测 USB 拇指驱动器 所以客户端或
  • 使用 PHP 比较两个字符串的相似度

    嘿伙计们 我想寻求一些解决方案 现在我有字典了单词 txt 这里有一些例子 happy laugh sad 我有俚语字符串 hppy 我想要搜索和匹配那个俚语字符串我的字典这意味着它将返回 happy 因为这些字符串参考 快乐 in 字典
  • 在 php 中回显 JSON 数据

    我正在尝试回显一些 JSON 数据 问题是数据包含变量 但我的代码没有将变量放入字符串中 这是我的代码 status row Status priority row Priority echo status status priority
  • html 下钻下拉所选值未插入 MYSQL

    我有两个下拉列表 首先从数据库下拉填充 根据第一个下拉列表的选定值从数据库填充第二个下拉列表 document ready function c change function var c1 c selected text if c1 aj
  • 是否需要使用fetch_object或fetch_array?

    我最近发现我可以打印数据库中的结果而不使用mysqli fetch object功能 例如 假设我们有一个简单的 sql select 语句 可以使用如下所示的语句来执行 conn mysqli connect localhost root

随机推荐