Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

How to create a custom Facade - It's hard to find the right info

Hi,

Can someone be so kind and show me step by step how to create a custom Facade in Laravel 5? Or direct me to where I can find info.

Thanks

0 likes
16 replies
RachidLaasri's avatar

This is the basic code to create a Facade in Laravel :

<?php 

namespace Vendor\PackageName\Facades;

use Illuminate\Support\Facades\Facade;

class FacadeName extends Facade {
    /**
     * Get the binding in the IoC container
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'Classname'; // the IoC binding.
    }
}

IT still pretty much the same from 4.2 http://laravel.com/docs/4.2/facades#creating-facades

2 likes
fsdolphin's avatar

@RachidLaasri

Well, when I said step-by-step I meant more than the code in the Facade class, sorry about the confusion. What I would like to see is more like...

  1. Go to the app folder and create a folder, call it Classes.
    app/Classes

  2. Create a class inside the Classes folder, call it Dog.php.
    app/Classes/Dog.php

      <?php namespace Custom\Classes;
     class Dog {
         public function bark()
         {
             echo 'Dog is Barking!';
         }
     }
    
  3. Go to the folder [ what folder? ] and create the Facade class.

     <?php namespace Custom\Classes\Facades;  
     use Illuminate\Support\Facades\Facade;  
     class Dog extends Facade 
     {
         protected static function getFacadeAccessor() { return 'Dog';     }
     }
     
    
  4. Bind your Dog class, where and how do I do this?

  5. Create a service provider, I have no idea how and where to do this?

  6. Open the app.php class and register your service provider and add an alias.
    app/config/app.php

  7. Using the bark() method in another class.
    Dog::bar();

  8. Done.

Here is the reason I don't fully understand a concept as simple as Facades. The documentation for 4.2 show one way, the 5.1 show a different way and the there is this tutorial that I couldn't follow since it doesnt tell you ecxacltry where to add you files.

4.2 Documentation
5.1 Documentation
Tutorial

Thanks

7 likes
fsdolphin's avatar

I already tried following this tutorial but I keep getting an error that's why I was looking for a step-by-step thinking that since the video is based on Laravel 4 I needed to do something different.

This is the code I have from following the tutorial you suggested.

File Structure

app/
+---Acme/
+-------Facades/
|------------Search.php
+-------Search/
|------------Search.php
|------------SearchServiceProvider.php

CODE:

Routes.php

Route::get('foo', function()
{
    return Acme\Facades\Search::acme();
});

File: app/Facades/Search.php

<?php  namespace Acme\Facades;
use Illuminate\Support\Facades\Facade;
class Search extends Facade{
    protected static function getFacadeAccessor()
    {
        return 'search';
    }
}

File: app/Search/Search.php

<?php namespace Acme\Search;
use Illuminate\Support\Collection;

class Search{
    public function acme()
    {
        echo 'Message from Acme';
    }
}

File: app/Search/SearchServiceProvider.php

<?php namespace Acme\Search;
use Illuminate\Support\ServiceProvider;
class SearchServiceProvider extends ServiceProvider{
    public function register()
    {
        $this->app->bind('search', 'Acme\Search\Search' );
    }
}

File: config/app.php

    ...
    'Illuminate\View\ViewServiceProvider',
    'Acme\Search\SearchServiceProvider',

ERROR:
Class 'Acme\Search\SearchServiceProvider' not found

Any Suggestion? What am I doing wrong?

lancebutler2's avatar

If you're adding these classes into a new namespace, like Acme\Search & Acme\Facades, make sure you run the 'composer dumpautoload' command to generate a new autoload file that includes those namespaces and classes. If they still aren't found by composer, you may need to add a PSR-4 entry in composer.json for your Acme namespace.

2 likes
fsdolphin's avatar

@lancebutler2 Thanks a lot, it now works.

I ran composer dumpautoload but it didn't work until I added the PSR-4 entry in the composer file.

    "psr-4": {
        "App\\": "app/",
        "Acme\\": "app/Acme"
    }
fsdolphin's avatar

I'm honestly a little frustrated about finding the right information in the documentation, can someone really create a custom Facade just by following the documentation, can someone go and read the docs and tell me if I'm the only one or It is really missing information?

Sorry but it looks like the documentation is for advance users.

I Also re-watched the video Custom Facades and I don't see where @JeffreyWay modifies the composer file nor runs the composer dumpautoload. Jeff, don't forget how it feels when learning new topics, if this video was for Custom Facades we don't really need a complete application just a simple call to a method to demonstrate Facades. In my opinion this could be broken into two videos. One from minute 000 to 8:00 and the other from minute 8:00 to 13.17.

MarkRedeman's avatar

@fsdolphin before you learn how to use and create facades, it's a good idea to understand how autoloading in combination with psr4 works.

So your composer.json file contains,

"psr-4": {
    "App\\": "app/",
    "Acme\\": "app/Acme"
}

If you do a composer dumpautoload then this tells composer that any time it needs to find a class named App\Something\MyClass then it will include the file located at app/Something/MyClass.php and if your app needs a class named Acme\Facades\Search it will include the file located at app/Acme/Facades/Search.php.

The files you showed in your previous post are in the Acme namespace, however if we take the SearchServiceProvider as an example then you said that it is located at app/Search/SearchServiceProvider.php, while its FQN (Fully Qualified Name) is Acme\Search\SearchServiceProvider, hence Laravel (composer) will try to find the file at app/Acme/Search/SearchServiceProvider.

This is why you get the error, Class 'Acme\Search\SearchServiceProvider' not found.

1 like
bovisp's avatar

@klaaz That article was very helpful to me too. Got a Facade up and running in just a few mins with that one. Thanks!

alexeydemin's avatar

Starting Laravel 5.4 you can use the approach below.

Assume you have a class

namespace App\States;

class Kansas
{
    public function weather()
    {
        echo "tornado";
    }
}

To get a real-time Facade you just have to reference your class by its fully qualified name as belonging to the Facades namespace.

// SomeController.php or whatever
use Facades\App\States\Kansas;

Kansas::weather();
// tornado

7 likes
jeff-h's avatar

@alexeydemin — sounds interesting, but can you tell me more? Or a link to somewhere to read more?

ChristophHarms's avatar

@alexeydemin That's a really cool feature! How did you hear of it? Can't find it anywhere in the docs. :-/

EDIT: I see there is a laracasts video about it, but it doesn't show up in the official documentation, right? Or is it just me who can't find it?

Please or to participate in this conversation.