门面自定义使用
- 在 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'); |
| } |