首页 文章

Laravel 5.1邮件文档 - 令人困惑?

提问于
浏览
1

我仍然是Laravel 5.1的新手,但我发现文档非常奇怪且令人困惑 .

例如 - 根据Laravel文档,我可以使用Mail Facade中的send()方法发送电子邮件 .

到现在为止还挺好 . 当我去Laravel API并找到Illuminate Support Facades Mail这样的方法不存在? https://laravel.com/api/5.1/Illuminate/Support/Facades/Mail.html

如何理解此方法采用的参数以及成功/失败时返回的参数?

2 回答

  • 1

    那是因为它正在使用Facade模式 .

    app.php 配置文件中有一个名为'aliases'的部分 . 该部分中有一行: 'Mail' => Illuminate\Support\Facades\Mail::class, 指向Facade,它返回 service container (IoC) 中绑定的 key ,返回要使用的类/对象 .

    因此,您需要找到创建绑定的位置 . 绑定由方法 App::bind('foo', .. )App::singleton('foo', .. )App::instance('foo', .. ) 创建 .

    我搜索 'mailer' 并找到创建绑定的文件 lluminate\Mail\MailServiceProvider

    $this->app->singleton('mailer', function($app) {
        ...
    
        // this is the class resolved by the IoC.
        $mailer = new Mailer(
            $app['view'], $app['swift.mailer'], $app['events']
        );
    
        ...
    
        return $mailer;
    });
    

    如您所见,类 \Illuminate\Mail\Mailerservice provider 中返回,这是您使用名为 MailFacade 时使用的类 .

    快速发现Facade背后的课程:

    您还可以通过转储类名来快速找到类的名称: dd( get_class( Mail::getFacadeRoot() ) );

    更多信息

    • 有关服务容器的更多信息:Click!

    • 关于Laravel 5外墙的更多信息:Click!

    • 有关Facade模式的更多信息:Click!

  • 1

    Facade类基本上是帮助类,可以快速,轻松地访问执行工作的真正类 . 关于外墙的优点有很多争论,但这不是针对这个问题 .

    如果在外观上调用 getFacadeRoot() 方法,它将为您提供外观指向的对象的实例(例如 Mail::getFacadeRoot() == \Illuminate\Mail\Mailer ) .

    现在您已了解正在使用的实际对象,您可以在该对象上查找方法 . 您在Facade上调用的任何方法都将传递给 getFacadeRoot() 返回的对象 . 因此,当您调用 Mail::send() 时,您实际上正在调用 \Illuminate\Mail\Mailer::send() (但非静态地) .

相关问题