Omid Omid - 9 months ago 23
PHP Question

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.

Answer Source

Your MyStorage class needs to extend FilesystemManager not the Storage facade class.

class MyStorage extends \Illuminate\Filesystem\FilesystemManager {
}