2

Swift_Message でメールを送信しようとしていますが、データを送信しようとすると送信されず、エラーが発生します

FatalErrorException: エラー: /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 行 252 の非オブジェクトに対するメンバー関数 get() の呼び出し

これが私が使用しているメールコントローラーです。

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 の例や他の多くのプライベートな例で使用されています。

これが役立つかどうかはわかりませんが、言及する価値があると考えました.

これは、 config.yml ファイル内の swiftmailer: 設定が原因でしょうか?

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

1 に答える 1