2017-03-12 47 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