简体   繁体   English

Symfony 3和Swift Mailer:如何配置服务

[英]Symfony 3 and Swift Mailer: how to configure service

In my symfony project I try to configure an email controller without success. 在我的symfony项目中,我尝试不成功配置电子邮件控制器。

services.yml services.yml

emailController:        
    class:     AppBundle\Controller\emailController
    public: true
    arguments:            
        $mailer: '@mailer'

emailController.php emailController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\UserBundle\FOSUserEvents;
use Doctrine\ORM\EntityManagerInterface;

class emailController extends Controller
{
    protected $mailer;

function __construct(\Swift_Mailer $mailer) {       
    $this->mailer = $mailer;
}   


public function sendMail($email){

    $message = (new \Swift_Message())       
    ->setSubject('send mail')  
    ->setFrom('xx@yy.com')      
    ->setTo($email)
    ->setBody('TEST')
    ->setContentType("text/html");

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

    return 1;       
}    

}

Symfony return this message: Symfony返回此消息:

Catchable Fatal Error: Argument 1 passed to AppBundle\\Controller\\emailController::__construct() must be an instance of Swift_Mailer, none given, 可捕获的致命错误:传递给AppBundle \\ Controller \\ emailController :: __ construct()的参数1必须是Swift_Mailer的实例,未给出任何实例,

I try some configuration and option but without success 我尝试了一些配置和选项,但没有成功

I think the problem is, that you are configuring your controller as a service, but your router probably does not refer to the configured service, only to the class name. 我认为问题在于,您正在将控制器配置为服务,但是路由器可能不引用配置的服务,而仅引用类名。

You can use annotations: 您可以使用注释:

@Route(service="emailController")

or the typical yaml format to refer to your controller as a service: 或典型的yaml格式将您的控制器称为服务:

email:
path:     /email
defaults: { _controller: emailController:indexAction }

Note that both refer to the service id specified in your definition above, not the actual class name. 请注意,两者均引用上面定义中指定的服务ID,而不是实际的类名。 You can read more about the concept of controllers as services in the documentation: https://symfony.com/doc/current/controller/service.html 您可以在文档中阅读有关控制器作为服务的概念的更多信息: https : //symfony.com/doc/current/controller/service.html

edit: As a sidenote since you seem to use a new Symfony version you might want to check out injecting services directly into actions using the resolve_controller_arguments tag: https://symfony.com/doc/current/controller.html#fetching-services-as-controller-arguments 编辑:作为一个旁注,由于您似乎使用了新的Symfony版本,因此您可能希望使用resolve_controller_arguments标记将操作直接注入到服务中: https : //symfony.com/doc/current/controller.html#fetching-services-作为控制器的论点

You define a controller as a service which is not how it's intended to be but whatever; 您将控制器定义为服务,这与预期的服务无关,而是与之无关。 both controller and services are normal PHP classes. 控制器和服务都是普通的PHP类。

Anyway, the aforementioned error message says it all, your service definition needs to supply the correct arguments (none given) eg like that: 无论如何,上述错误消息说明了一切,您的服务定义需要提供正确的参数(未给出),例如:

arguments: ['@mailer']

Please try this. 请尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM