如何扩展 Laravel 存储门面?
How to extend Laravel Storage facade?
在不知道 Laravel facades 如何工作的情况下,根据我的 PHP 知识,我尝试扩展 Storage facade 以添加一些新功能。
我有这个代码:
class MyStorageFacade extends Facade {
/**
* Get the binding in the IoC container
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'MyStorage'; // the IoC binding.
}
}
启动服务提供商时:
$this->app->bind('MyStorage',function($app){
return new MyStorage($app);
});
门面是:
class MyStorage extends \Illuminate\Support\Facades\Storage{
}
使用时:
use Namespace\MyStorage\MyStorageFacade as MyStorage;
MyStorage::disk('local');
我收到这个错误:
FatalThrowableError in Facade.php line 237: Call to undefined method Namespace\MyStorage\MyStorage::disk()
也 尝试扩展 MyStorage
形式 Illuminate\Filesystem\Filesystem
并以其他方式得到相同的错误:
BadMethodCallException in Macroable.php line 74: Method disk does not exist.
外观只是一种便利 class,它将静态调用 Facade::method
转换为 resolove("binding")->method
(或多或少)。您需要从文件系统扩展,在 IoC 中注册它,保持外观不变,并将外观用作静态。
门面:
class MyStorageFacade extends Facade {
protected static function getFacadeAccessor()
{
return 'MyStorage'; // This one is fine
}
}
您的自定义存储空间class:
class MyStorage extends Illuminate\Filesystem\FilesystemManager {
}
在任何服务提供商中(例如 AppServiceProvider
)
$this->app->bind('MyStorage',function($app){
return new MyStorage($app);
});
然后当你需要使用它的时候把它当作:
MyStorageFacade::disk(); //Should work.
您的 MyStorage
class 需要扩展 FilesystemManager
而不是存储外观 class。
class MyStorage extends \Illuminate\Filesystem\FilesystemManager {
....
}
在不知道 Laravel facades 如何工作的情况下,根据我的 PHP 知识,我尝试扩展 Storage facade 以添加一些新功能。
我有这个代码:
class MyStorageFacade extends Facade {
/**
* Get the binding in the IoC container
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'MyStorage'; // the IoC binding.
}
}
启动服务提供商时:
$this->app->bind('MyStorage',function($app){
return new MyStorage($app);
});
门面是:
class MyStorage extends \Illuminate\Support\Facades\Storage{
}
使用时:
use Namespace\MyStorage\MyStorageFacade as MyStorage;
MyStorage::disk('local');
我收到这个错误:
FatalThrowableError in Facade.php line 237: Call to undefined method Namespace\MyStorage\MyStorage::disk()
也 尝试扩展 MyStorage
形式 Illuminate\Filesystem\Filesystem
并以其他方式得到相同的错误:
BadMethodCallException in Macroable.php line 74: Method disk does not exist.
外观只是一种便利 class,它将静态调用 Facade::method
转换为 resolove("binding")->method
(或多或少)。您需要从文件系统扩展,在 IoC 中注册它,保持外观不变,并将外观用作静态。
门面:
class MyStorageFacade extends Facade {
protected static function getFacadeAccessor()
{
return 'MyStorage'; // This one is fine
}
}
您的自定义存储空间class:
class MyStorage extends Illuminate\Filesystem\FilesystemManager {
}
在任何服务提供商中(例如 AppServiceProvider
)
$this->app->bind('MyStorage',function($app){
return new MyStorage($app);
});
然后当你需要使用它的时候把它当作:
MyStorageFacade::disk(); //Should work.
您的 MyStorage
class 需要扩展 FilesystemManager
而不是存储外观 class。
class MyStorage extends \Illuminate\Filesystem\FilesystemManager {
....
}