我像这样创建了一个类和Facade:
Invoice.php(Facades / Invoice.php)
<?php
namespace app\Facades\;
use Illuminate\Support\Facades\Facade;
class Invoice extends Facade
{
protected static function getFacadeAccessor()
{
//NEVER CALLED
dd("TEST");
return 'invoice';
}
}
?>
InvoiceFacadesServiceProviders.php
<?php
namespace App\Providers;
use App;
use Illuminate\Support\ServiceProvider;
class InvoiceFacadesServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
App::bind('invoice',function()
{
return new \App\Invoice\Invoice;
});
}
}
?>
最后是类本身:
<?php
namespace App\Invoice;
class Invoice
{
public function testfunc()
{
return "This is a test";
}
}
?>
在控制器中,我尝试通过使用如下类来对其进行测试:
use App\Invoice\Invoice;
并调用testfunc方法:
dd(Invoice::testfunc());
在app.php
中,我添加了以下内容(提供者):App\Providers\InvoiceFacadesServiceProvider::class,
和(别名):'invoice' => App\Facades\Invoice::class,
但是我只能得到:
Non-static method App\Invoice\Invoice::testfunc() should not be called statically.
我还注意到,getFacadeAccessor()
方法从未触发过,因为它没有命中我放在那里的dd
。
我在这里想念什么?
感谢您的帮助和指导!
答案 0 :(得分:2)
您将类称为Invoice
,而不是Facace Invoice
。这就是警告发生的原因。
尝试调用\App\Facades\Invoice::testfunc();
,然后通过Facade调用该方法。