Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Difference between Facades and Aliases

Good day!

The more I read, the more I get confused about this. What is the difference between a Facade and Aliases?

I have this Class:

/app/libraries/Project/Data.php

namespace PJ;

class Data {

    // It is much like a data container, with static methods and properties for saving info

}

And the corresponding facade, so I can access by using just PJD:: .

According to some webpage around:

... Laravel Facades are proxies. They wrap around and call functions on the underlying true implementation of the code. Further, in the context of a Laravel application, these Facades are accessed by assigning them to aliases. This use of the Dependency Injection container allow you to reference something like Illuminate\Support\Facades\Filesystem by simply calling File. (http://ryantablada.com/post/proxies-service-locators-alias-facades-and-war)

But, I've also found and successfully tested that adding something like:

__app/config/app.php__

'aliases' => array(
    //....,
    'PJD'             => 'PJ\Data',
),

I can also access my class the same way.

So, what's the difference?

Thanks

EDIT #01

I have created a class named Data in /app/libraries/Project/Data.php

namespace PJ;

class Data {
    // It is much like a data container, with static methods and properties for saving info
}

I have a Facade Class for this Class Data /app/libraries/Project/DataFacade.php

use Illuminate\Support\Facades\Facade;   
class PJD extends Facade {
    protected static function getFacadeAccessor() { 
        return 'PJData';
    } 
}

And I have a Service Provider for them: /app/libraries/Project/DataServiceProvider.php

use Illuminate\Support\ServiceProvider;

class DataServiceProvider extends ServiceProvider {
    public function register() {
        $this->app->singleton('PJData', function() {
            return new PJ\Data;
        });
    }
}

I also have added to /app/config/app.php:

'providers' => array(
    // ....
    'DataServiceProvider',
),

and in composer.json I've added a psr-4 line to direct PJ namespace to /app/libraries/Project

"psr-4": {
     "PJ\\": "app/libraries/Project"
},

By doing all this, I can access my class from anywhere in the project just by PJD:: instead of PJ\Data::.

However, I've also noticed that just by adding to /app/config/app.php

'aliases' => array(
    //....,
    'PJD'             => 'PJ\Data',
),

I get exactly the same result without all that facades and ServiceProviders. So, what's the point of one or another?

Thanks, and sorry for the large post.

like image 505
Marco Madueño Mejía Avatar asked Nov 12 '14 02:11

Marco Madueño Mejía


People also ask

What is aliases in Laravel?

Bash aliases are shortcuts added to a file that allows you to reference another command through more memorable words, abbreviations, or characters.

What are Laravel facades?

A Laravel facade is a class which provides a static-like interface to services inside the service container. They serve as a proxy for accessing the underlying implementation of the laravel's services.

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.

What is a facade in PHP?

Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework. While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.


1 Answers

Facade and Alias are two totally different concepts.

you can not access PJ\Data\ by PJD:: unless you have setup alias in the service provider while binding.

If you are accessing it, without defining it in config/app.php, then you have set it up in the service provider file itself.

Definition of alias,

used to indicate that a named person is also known or more familiar under another specified name.

It simply means you are giving a different name to the class so that it will be easier to call.

e.g.

if you have a class like this: Foo\Bar\AVeryLongNamespaceClassName\Data, you can just give an alias, (e.g. PJD) and access its methods and properties by this alias.

Note:

Unit testing is an important aspect of why facades work the way that they do. In fact, testability is the primary reason for facades to even exist.

like image 67
itachi Avatar answered Sep 23 '22 22:09

itachi