Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the underlying class behind a Laravel facade?

Tags:

php

laravel

For example - this function uses a facade:

File::get('path/to/file.txt');

It turns out the underlying class that actually supplies File::get is Illuminate\Filesystem\Filesystem

I looked at the Laravel 4.2 documentation - thats the version Im using - and also the api reference but I couldn't find anything that would explain how to someone who didn't know in advance how to find the "real" class to a facade.

this tutorial on Laravel facades gives a method of doing it which involves

  • finding the File class
  • looking to see that it extends class Facade
  • following the code through the Facade#__callstatic() method
  • tracing the behaviour of __callstatic(), resolveFacadeInstance() when getFacadeAccessor() returns string files
  • etc, etc
  • ... too long / many steps to post

This is a good demonstration of whats going on, but I wouldn't want to do this regularly.

My question is, knowing that the "facaded classes" you use in your app dont necessarily have a the same name or some convention to help you search the filesystem, how can someone - who doesn't know in advance what the underlying class is - find the underlying class for a laravel facade?

like image 402
the_velour_fog Avatar asked Apr 19 '15 22:04

the_velour_fog


People also ask

What is facade class in Laravel?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

Can you explain which design pattern does Laravel facades follows?

A facade in Laravel is a wrapper around a non-static function that turns it into a static function. The word "wrapper" can also be used when describing design patterns. Wrapping an object to provide a simplified interface to it is often described as the "facade" pattern. So in short, the wrapper is the facade.

Where is facade folder in Laravel?

Since laravel largely uses facades rather every non static function has its equivalent facade available in service container, we must also follow the same way in our custom application. All of Laravel's facades are defined in the Illuminate\Support\Facades namespace.

What does Laravel use for seeding & facades?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


2 Answers

This is a nice resource: https://laravel.com/docs/facades#facade-class-reference other than that make sure to install some kind of intellisense plugin for whatever editor you happen to be using. Most of them allow you to Ctrl+Right-Click on a class/method and go to the definition.

like image 100
scrubmx Avatar answered Sep 28 '22 05:09

scrubmx


It seems that you can use getFacadeRoot(). For example, to find out what's behind the Mail facade:

get_class(Mail::getFacadeRoot());
// in my case returns 'Illuminate\Mail\Mailer'
like image 34
Daniel Howard Avatar answered Sep 28 '22 06:09

Daniel Howard