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

Nospoon's avatar

Laravel 5.1 - Can't get facade to work

So, I've looked through every possible documentation and forum thread on service providers and facades and still can't get this to work...

I have a service and a provider:

namespace App\Providers;

use App\Services\AddressFinder;
use Illuminate\Support\ServiceProvider;

class AddressFinderServiceProvider extends ServiceProvider

{
    protected $defer = true;

    public function register()
    {
        $this->app->bind('address_finder', function()
        {
            return new AddressFinder('idealpostcodes');
        });
    }
}

And a facade for it:

namespace App\Services\Facades;

use Illuminate\Support\Facades\Facade;

class AddressFinderFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'address_finder';
    }
}

Now when I try to use AddressFinder::find($postcode); I'm getting

ReflectionException in Container.php line 736:
Class address_finder does not exist

Both the service provider and facade are properly registered in app.php.

If I pass the full class name in getFacadeAccessor() only then it works. As if the facade doesn't even try to resolve it from the service container.

0 likes
13 replies
Nospoon's avatar

I've also seen different ways of binding the service, not sure which one is the correct one so I tried them all:

$this->app->singleton('address_finder', function () {
            return new AddressFinder('idealpostcodes');
        });

and

$this->app['address_finder'] = $this->app->share(function($app) {
            return new AddressFinder('idealpostcodes');
        });

as well as

$this->app->bind('address_finder', function() {
            return new AddressFinder('idealpostcodes');
        });
davidfaux's avatar

Aren't you meant to bind to a class or interface, I'm guessing App\Services\AddressFinder in your case?

Nospoon's avatar

Well that's kind of the part which I don't exactly understand, how do I make the binding correctly so that it's available by address_finder handle?

usman's avatar

@Nospoon did you add the service provider into the providers array of config/app.php file?

Update: sorry missed that part.

Nospoon's avatar

Yes I did:

App\Providers\AddressFinderServiceProvider::class,

And the facade is:

'AddressFinder' => App\Services\Facades\AddressFinderFacade::class,
usman's avatar

@Nospoon do you have a compiled.php file inside the bootstrap/cache/ directory? In case it exists try running the artisan clear-compiled command.

Nospoon's avatar

Ok so the only way I've managed to get this working is by: in register() method of service provider referencing full class name:

$this->app->singleton('App\Services\AddressFinder', function () {
            return new AddressFinder('idealpostcodes');
        });

in facade

protected static function getFacadeAccessor()
    {
        return 'App\Services\AddressFinder';
    }

However this is not how it's done with all other service providers...

Nospoon's avatar

@usman clear-compiled didn't help as well. Still getting Class address_finder does not exist.

agustinuswidodo's avatar

@Nospoon: Try to remove files, if any, except .gitignore, inside bootstrap/cache/ folder. It will auto-generate again services.json file. I hope that helps.

2 likes

Please or to participate in this conversation.