Ad

Laravel 5.1 Documentation For Mail - Confusing?

- 1 answer

I am still new to Laravel 5.1, but I found the documentation very strange and confusing.

For example - according to the Laravel docs I can use send() method from the Mail facade to send email.

So far, so good. When I go to Laravel API and find Illuminate Support Facades Mail such method doesn't exist? https://laravel.com/api/5.1/Illuminate/Support/Facades/Mail.html

How can I understand what parameters this method takes and what it is returning on success/failure?

Ad

Answer

That's because it is using the Facade pattern.

In your app.php config file there is a section called 'aliases'. There is a line in that section: 'Mail' => Illuminate\Support\Facades\Mail::class, which points to the Facade, which returns the key of the bind in the service container (IoC) which returns the class/object to work with.

So you need to find the place where the bind is created. Binds are created by the methods App::bind('foo', .. ), App::singleton('foo', .. )or App::instance('foo', .. ).

I search for 'mailer' and found the file lluminate\Mail\MailServiceProvider where the bind is created:

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

As you can see, the class \Illuminate\Mail\Mailer is returned in the service provider and that is the class used when you use the Facade called Mail.

Quick way to discover the class behind the Facade:

You can also find the name of the class quickly by dumping the class name: dd( get_class( Mail::getFacadeRoot() ) );

More information

  • More information about the service container: Click!
  • More information about Facades in Laravel 5: Click!
  • More information about the Facade pattern: Click!
Ad
source: stackoverflow.com
Ad