2

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?

2 Answers 2

1

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!
Sign up to request clarification or add additional context in comments.

Comments

1

The Facade classes are basically helper classes to provide quick, easy access to the real classes that do the work. There's plenty of debate on the merits of facades, but that is not for this question.

If you call the getFacadeRoot() method on the facade, it will give you the instance of the object to which the facade points (e.g. Mail::getFacadeRoot() == \Illuminate\Mail\Mailer).

Now that you know the actual object being used, you can look up the methods on that object. Any methods you call on the Facade are passed through to the object that is returned by getFacadeRoot(). So, when you are calling Mail::send(), you are actually calling \Illuminate\Mail\Mailer::send() (though, non statically).

1 Comment

Aka look in the api doc for mailer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.