2017-03-12 55 views
2

基于我的PHP知识,我不知道Laravel门面如何工作,我尝试扩展存储外观以添加一些新功能。如何扩展Laravel仓库门面?

我有这样的代码:

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.

回答

1

你的MyStorage类需要延长FilesystemManager没有存储门面类。

class MyStorage extends \Illuminate\Filesystem\FilesystemManager { 
} 
0

外观只是一个便利的类,它将静态调用Facade::method转换为resolove("binding")->method(或多或少)。您需要从Filesystem进行扩展,在IoC中进行注册,保持原有的外观,并将Facade用作静态。

class MyStorageFacade extends Facade {  
    protected static function getFacadeAccessor() 
    { 
     return 'MyStorage'; // This one is fine 
    } 
} 

class MyStorage extends Illuminate\Filesystem\FilesystemManager { 
} 

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

MyStorageFacade::disk(); //Should work. 
+0

正如我所说,我测试过,并得到这个错误:'BadMethodCallException在Macroable.php线74:方法磁盘不exist.' – Omid

+0

@Omid'disk'是没有定义是怎样的任何地方。 – apokryfos

+0

那么'\ Storage :: disk('local')'如何在这种情况下工作? @apokryfos – Omid