5
votes

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.

1
without facades and service providers, how you are accessing PJD class and its methods and properties? can you edit and give example?itachi
I have this class Data under namespace PJ. So I access PJ\Data::. Adding an alias to app.php in /app/config (By adding that line into the aliases array) I can access PJ\Data:: as PJD::. All without creating Facades and ServiceProviders. In composer.json I add that psr-4 section and also the "app/libraries" directory to the autoload classmap section.Marco Madueño Mejía
without instantiating the class?... i mean object?itachi
Should I? It works just by doing what I said. I just created another class named Test under namespace PJ\ with just one property public static $testvar = 'Hi';. I added to the aliases array in app.php 'TestClass' => 'PJ\Test'. And that's all. I can access from anywhere TestClass::$testvar. Am I missing something?`Marco Madueño Mejía
check the NOTE in my answer as to why facade is needed. and Facades are mainly for non static methods. NOT static ones.itachi

1 Answers

5
votes

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.