【问题标题】:Laravel 5.1 Documentation for Mail - confusing?Laravel 5.1 邮件文档 - 令人困惑?
【发布时间】:2016-01-18 09:15:28
【问题描述】:

我还是 Laravel 5.1 的新手,但我发现文档非常奇怪和混乱。

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

到目前为止,一切都很好。当我去 Laravel API 并找到 Illuminate Support Facades Mail 时,这种方法不存在吗? https://laravel.com/api/5.1/Illuminate/Support/Facades/Mail.html

我如何了解此方法采用哪些参数以及成功/失败时返回什么?

【问题讨论】:

    标签: php email laravel


    【解决方案1】:

    那是因为它使用了 Facade 模式。

    在您的app.php 配置文件中有一个名为“别名”的部分。该部分中有一行:'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;
    });
    

    如您所见,service provider 中返回了 \Illuminate\Mail\Mailer 类,这是您使用 Facade 时使用的类,称为 Mail

    快速发现 Facade 背后的类:

    也可以通过dump类名快速找到类名:dd( get_class( Mail::getFacadeRoot() ) );

    更多信息

    • 更多关于服务容器的信息:Click!
    • 更多关于 Laravel 5 中 Facades 的信息:Click!
    • 更多关于外观模式的信息:Click!

    【讨论】:

      【解决方案2】:

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

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

      现在您知道了实际使用的对象,您可以查找该对象的方法。您在 Facade 上调用的任何方法都会传递给 getFacadeRoot() 返回的对象。所以,当你调用Mail::send() 时,你实际上是在调用\Illuminate\Mail\Mailer::send()(虽然,不是静态的)。

      【讨论】:

      • 又名在 api 文档中查找 mailer
      猜你喜欢
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多