門面自定義使用
- 在 app/Services 目錄下建立 TestService
<?php
namespace App\Services;
class TestService
{
public function callMe($controller)
{
dd('Call Me From TestServiceProvider In '.$controller);
}
}
- 在 app 目錄下新建 Facades 目錄
- 在 app/Facades 目錄下新建 Test
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Test extends Facade
{
protected static function getFacadeAccessor(){
return 'test';
}
}
php artisan make:provider TestServiceProvider
- 在 TestServiceProvider boot方法中為例項繫結別名
public function boot()
{
$this->app->singleton('test',function(){
return new TestService();
});
}
- 在app/congfig/app.php 檔案中註冊 服務提供者、門面別名
'providers' => [
...
\App\Providers\TestServiceProvider::class,
...
],
'aliases' => [
...
'Test' => \App\Facades\Test::class,
...
]
use App\Facades\Test as TestFacade;
public function index(Request $request){
TestFacade::callMe('123');
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結