Saturday, March 11, 2017

How to extend Laravel Storage facade?

Without knowing how Laravel facades work, based on my PHP knowledge, I tried to extend Storage facade to add some new functionalities.

I have this code:

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

While booting service provider:

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

And facade is:

class MyStorage extends \Illuminate\Support\Facades\Storage{
}

When using it:

use Namespace\MyStorage\MyStorageFacade as MyStorage;
MyStorage::disk('local');

I get this error:

FatalThrowableError in Facade.php line 237: Call to undefined method Namespace\MyStorage\MyStorage::disk()

Also tried to extend MyStorage form Illuminate\Filesystem\Filesystem and got the same error in other way:

BadMethodCallException in Macroable.php line 74: Method disk does not exist.



via Omid

Advertisement