Laravel 5.8 添加Facades
记录下自己创建的Facades
1、创建一个类
<?php
namespace App\Wubuze\Plugs;
class Common
{
public function logInfo($msg)
{
file_put_contents(storage_path('logs/wbz-log'), $msg);
}
}
2、 创建一个Facade
namespace App\Wubuze\Facades;
/**
* @method static void info(string $message, array $context = [])
* @package App\Wubuze\Facades
*
*
* @see \App\Wubuze\Plugs\Common
*/
class Log extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return 'wbz-plugs';
}
}
3、 在 AppServiceProvider 中 register
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Wubuze\Plugs\Common;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('wbz-plugs', function() {
return new Common;
});
}
}
4、 使用
App\Wubuze\Facades\Log::logInfo('Hello Facades');
本作品采用《CC 协议》,转载必须注明作者和本文链接