1.创建自己的工具类: \App\Utils\MyUtil::class
2.服务提供者把工具类添加到容器
1) php artisan make:provider MyProvider 创建服务
2)服务中添加到容器
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
$this->app->singleton("MyUtil",\App\Utils\MyUtil::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
3)app.php中添加服务
'providers' => [
App\Providers\MyProvider::class,
]
3.创建门面
<?php
/**
* Created by PhpStorm.
* User: xuefutao
* Date: 2019-07-13
* Time: 19:38
*/
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class MyFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'MyUtil';//返回前面服务注册名
}
}
app.php加入门面,然后进行访问
'aliases' => [
'MyUtil'=>App\Facades\MyFacade::class,
]
访问: MyUtil::getxxx();getxxx为工具中的方法