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 协议》,转载必须注明作者和本文链接
wubuze
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!