5

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.

Marco Madueño Mejía
  • 1,105
  • 2
  • 9
  • 7
  • without facades and service providers, how you are accessing `PJD` class and its methods and properties? can you edit and give example? – itachi Nov 12 '14 at 03:06
  • 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 Nov 12 '14 at 03:12
  • without instantiating the class?... i mean object? – itachi Nov 12 '14 at 03:13
  • 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 Nov 12 '14 at 03:20
  • check the __NOTE__ in my answer as to why facade is needed. and Facades are mainly for non static methods. NOT static ones. – itachi Nov 12 '14 at 03:24
  • So, creating an alias for any class just by adding it to app.php isn't correct? If you speak about testability (I don't know ANYthing about it) as the reason for using Facades, then it means I'm completely lost, and I should start reading a lot to understand your point. I suposse that, even it works, it is not correct doing it that easy way, right? – Marco Madueño Mejía Nov 12 '14 at 03:54
  • read [**this**](http://stackoverflow.com/questions/1185605/when-to-use-static-vs-instantiated-classes) as to why static properties and methods should be avoided at many times. – itachi Nov 12 '14 at 03:56
  • I think this is starting to go offtopic. I don't catch the point yet. You say I must not use statics, but rather non static methods and facades. At the end, one way or another I will be accessing my class the same way: `PJD::$var`; and `PJD::set(...);` for example. I need a lot of researching about to get it clear, though I don't know where to start... – Marco Madueño Mejía Nov 12 '14 at 04:13
  • `Facades provide a "static" interface to classes that are available in the application's IoC container.` those are NOT static methods. read from the doc. it is explained very nicely into detail. http://laravel.com/docs/4.2/facades – itachi Nov 12 '14 at 04:17

1 Answers1

5

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.

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
itachi
  • 6,323
  • 3
  • 30
  • 40