user1094128
user1094128

Reputation: 461

I just can't get my facade to work in laravel 4

Please can some one tell me why my first home made facade doesn't work. I'm spending hours on this and I might be going mad.

composer.js contains

    "classmap": [
        "app/commands",
        "app/controllers",
        "app/facades",
        "app/libraries",
        ....

/app/config/app.php contains

'providers' => array(
    ....
    'CloudsServiceProvider',

/libraries/Clouds.php

class Clouds {

    protected $_current = null;

    public function current() 
    {

        if($this->_current)
            return $this->_current;

        $this->_current = Cloud::find(1); // Cloud in a Model 

        return $this->_current;
    }

}

/libraries/CloudsServiceProvider.php

use Illuminate\Support\ServiceProvider;

class CloudsServiceProvider extends ServiceProvider 
{

    public function register()
    {

        $this->app->bind('Clouds', function()
        {
            return new Clouds();
        });
    }
} 

/facades/CloudsFacade.php

use Illuminate\Support\Facades\Facade;

class Clouds extends Facade 
{

    protected static function getFacadeAccessor() 
    { 
        return 'Clouds'; 
    }

}

in the Terminal i do:

composer dump

When I call $cloud = Clouds::current(); I get:

Non-static method Clouds::current() should not be called statically, assuming $this from incompatible context

App::make('Clouds')->current(); seems to work though.

I have followed a few tutorials but I just keep coming back to this problem.

Upvotes: 1

Views: 1195

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87739

You facade is using the same name of your Clouds class, you shoud change to:

class CloudsFacade extends Facade 
{
   ...
}

A tip: open your vendor/composer/autoload_classmap.php, all 3 files must be listed there.

Also, you will have to namespace all 3 files:

<?php namespace App\Clouds;

Add to your providers:

'App\Clouds\CloudsServiceProvider',

And aliases:

'Clouds' => 'App\Clouds\CloudsFacade',

Then you

composer du

And you should be good to go.

Upvotes: 1

Related Questions