New Get started on Amezmo with a $60, 12-month credit for new users. Create free account →

How To Find All Available Methods To Laravel Facades

Avatar
Ryan McCullagh on
A picture of a brick Facade

Laravel, the most popular PHP framework, stands out with its Facades pattern. Notably, the documentation encourages the Facade pattern over the DI Pattern. However, you don’t have to use Facades, if Dependency Injection is more your style.

In this post, we dive into the Laravel Internals. More specifically, we learn how to find every method available to use on any facade.

What’s a Facade?

A Laravel facade is a class where we statically call methods rather than having to new up objects to call a method. Of course, the underlying implementations are not part of a static class.

Laravel facades, powered by service providers, typically register a class implementation to the Laravel IoC container. In particular, facades are easy to write and provide an expressive way of writing PHP code.

However, facades are kind of a black box sometimes, because the IDE, in most cases, can’t infer all possible methods on the facade class.

Finding The Real Implementation Of The Mail class

We all know the Mail class. It makes sending an email painless. Of course, we don’t have to use the facade pattern to invoke the send method on the Mail class, but if we do, we can find all the methods by searching the source code.

The Laravel framework binds builtin services to the DI container upon booting your application. This magic happens with method registerCoreContainerAliases in Application.php.

A screenshot of Mail.php in the Laravel source framework codebase.

In particular, the Mail facade class is bound to the mail.manager service. Considering this fact, we can search Application.php for mail.manager and uncover the true fully qualified class that calls to your favorite Mail::<Method X> method map to.

Further, we can see that methods invoked on our Mail facade are instances of real class Illuminate\Mail\Mailer::class Knowing this, we still have further ground to dig up.

The MailManager class is actually a factory class that invokes methods on via the PHP magic method __call on the Mailer class. The default driver for mail in Laravel is smtp.

Of course, the driver doesn’t matter because Laravel provides a common interface. That is, the driver’s particular methods aren’t exposed. So, what methods are available on our Mail::<Insert method Here>() class?

Every invocation of Mail is actually an invocation of the class Mailer.php. Specifically, this class is defined at \Illuminate\Mail\Mailer.php. Therefore, as of this writing, these are the list of methods available.

namespace Illuminate\Mail;

class Mailer implements MailerContract, MailQueueContract
{
    /**
     * Set the global from address and name.
     *
     * @param  string  $address
     * @param  string|null  $name
     * @return void
     */
    public function alwaysFrom($address, $name = null);
    /**
     * Set the global reply-to address and name.
     *
     * @param  string  $address
     * @param  string|null  $name
     * @return void
     */
    public function alwaysReplyTo($address, $name = null);

    /**
     * Set the global return path address.
     *
     * @param  string  $address
     * @return void
     */
    public function alwaysReturnPath($address);
    /**
     * Set the global to address and name.
     *
     * @param  string  $address
     * @param  string|null  $name
     * @return void
     */
    public function alwaysTo($address, $name = null);
    /**
     * Begin the process of mailing a mailable class instance.
     *
     * @param  mixed  $users
     * @return \Illuminate\Mail\PendingMail
     */
    public function to($users);
    /**
     * Begin the process of mailing a mailable class instance.
     *
     * @param  mixed  $users
     * @return \Illuminate\Mail\PendingMail
     */
    public function cc($users);
    /**
     * Begin the process of mailing a mailable class instance.
     *
     * @param  mixed  $users
     * @return \Illuminate\Mail\PendingMail
     */
    public function bcc($users);
    /**
     * Send a new message with only an HTML part.
     *
     * @param  string  $html
     * @param  mixed  $callback
     * @return void
     */
    public function html($html, $callback);
    /**
     * Send a new message with only a raw text part.
     *
     * @param  string  $text
     * @param  mixed  $callback
     * @return void
     */
    public function raw($text, $callback)
    {
        return $this->send(['raw' => $text], [], $callback);
    }
    /**
     * Send a new message with only a plain part.
     *
     * @param  string  $view
     * @param  array  $data
     * @param  mixed  $callback
     * @return void
     */
    public function plain($view, array $data, $callback);
    /**
     * Render the given message as a view.
     *
     * @param  string|array  $view
     * @param  array  $data
     * @return string
     */
    public function render($view, array $data = []);
    /**
     * Send a new message using a view.
     *
     * @param  \Illuminate\Contracts\Mail\Mailable|string|array  $view
     * @param  array  $data
     * @param  \Closure|string|null  $callback
     * @return void
     */
    public function send($view, array $data = [], $callback = null);

    /**
     * Queue a new e-mail message for sending.
     *
     * @param  \Illuminate\Contracts\Mail\Mailable  $view
     * @param  string|null  $queue
     * @return mixed
     *
     * @throws \InvalidArgumentException
     */
    public function queue($view, $queue = null);

    /**
     * Queue a new e-mail message for sending on the given queue.
     *
     * @param  string  $queue
     * @param  \Illuminate\Contracts\Mail\Mailable  $view
     * @return mixed
     */
    public function onQueue($queue, $view);

    /**
     * Queue a new e-mail message for sending on the given queue.
     *
     * This method didn't match rest of framework's "onQueue" phrasing. Added "onQueue".
     *
     * @param  string  $queue
     * @param  \Illuminate\Contracts\Mail\Mailable  $view
     * @return mixed
     */
    public function queueOn($queue, $view)
    {
        return $this->onQueue($queue, $view);
    }
    /**
     * Queue a new e-mail message for sending after (n) seconds.
     *
     * @param  \DateTimeInterface|\DateInterval|int  $delay
     * @param  \Illuminate\Contracts\Mail\Mailable  $view
     * @param  string|null  $queue
     * @return mixed
     *
     * @throws \InvalidArgumentException
     */
    public function later($delay, $view, $queue = null);
    /**
     * Queue a new e-mail message for sending after (n) seconds on the given queue.
     *
     * @param  string  $queue
     * @param  \DateTimeInterface|\DateInterval|int  $delay
     * @param  \Illuminate\Contracts\Mail\Mailable  $view
     * @return mixed
     */
    public function laterOn($queue, $delay, $view);
    /**
     * Get the array of failed recipients.
     *
     * @return array
     */
    public function failures();

    /**
     * Get the Swift Mailer instance.
     *
     * @return \Swift_Mailer
     */
    public function getSwiftMailer();

    /**
     * Get the view factory instance.
     *
     * @return \Illuminate\Contracts\View\Factory
     */
    public function getViewFactory();

    /**
     * Set the Swift Mailer instance.
     *
     * @param  \Swift_Mailer  $swift
     * @return void
     */
    public function setSwiftMailer($swift);

    /**
     * Set the queue manager instance.
     *
     * @param  \Illuminate\Contracts\Queue\Factory  $queue
     * @return $this
     */
    public function setQueue(QueueContract $queue);
}

Wrapping Things Up

Laravel Facades provide a bit of magic, but it’s no secret how they work. In this post, we went over a technique we use here at Amezmo.

Of course, finding the implementations of Facade services in Laravel is a bit of work, but in the end, it’s worth it.

Hundreds of businesses and developers use Amezmo for completely managed Laravel hosting. We hope you found this content useful. Of course, we would love to hear from you in our comment section.