目录
1、app/Services 目录下创建TestService
2、app目录下创建Facades目录,并创建类Test
3、创建服务提供者
4、? TestServiceProvider.php中的boot方法中为实例绑定别名
5、config/app.php文件中注册服务提供者、门面别名
6、控制器中调用
?7、结果显示
1、app/Services 目录下创建TestService
app目录下新增Services 目录,并在此目录下新增clss类 TestServices
TsetServices.php
<?php
namespace App\Services;
class TestServices
{
public function callMe($controller)
{
dd('call Me From TestSrvicesProvider In'.$controller);
}
}
2、app目录下创建Facades目录,并创建类Test
Test.php
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Test extends Facade
{
protected static function getFacadeAccessor()
{
return 'test';
}
}
3、创建服务提供者
php artisan make:provider TestServiceProvider
4、? TestServiceProvider.php中的boot方法中为实例绑定别名
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//使用singleton 绑定单例
$this->app->singleton('test',function (){
return new TestServices();
});
}
5、config/app.php文件中注册服务提供者、门面别名
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
//新注册的服务提供者
App\Providers\TestServiceProvider::class,
],
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
// 'Redis' => Illuminate\Support\Facades\Redis::class,
'Request' => Illuminate\Support\Facades\Request::class,
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,
'Schema' => Illuminate\Support\Facades\Schema::class,
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'Str' => Illuminate\Support\Str::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
//新注册的门面别名Test
'Test'=>\App\Facades\Test::class,
],
6、控制器中调用
(1)新建TestController
php artisan make:controller TestController
(2)控制器中调用门面
<?php
namespace App\Http\Controllers;
use App\Facades\Test as TestFacade;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index(Request $request)
{
TestFacade::callMe('123');
}
public function test()
{
$strNew ="欢迎来到Laravel的世界。" ;
return $strNew;
}
}
(3)定义路由:web.php
Route::get('/test',[\App\Http\Controllers\TestController::class,'index']);
?7、结果显示
至此Laravel的自定义门面就结束了,谢谢大家的观看。
|