laravel 5 : Extend a Facade

╄→尐↘猪︶ㄣ 提交于 2019-12-05 09:07:48

Let's say that you define your custom facade in app/Facades/MyDBFacade.php

<?php

namespace App\Facades;

use Illuminate\Support\Facades\DB;

class MyDBFacade extends DB
{
    // ...
}

You just need to change single line in config/app.php, from

'DB' => Illuminate\Support\Facades\DB::class,

to

'DB' => App\Facades\MyDBFacade::class,

And it all should work now.

You can create / extend your Facade like this:

<?php namespace YourNameSpace\Facades;

class MyDBFacade extends Illuminate\Support\Facades\DB {

        /**
         * Create your custom methods here...
         */
        public static function anyMethod($active)
        {
            /// do what you have to do
        }

}

And then replace (or add it as a new one) to your app/config/app.php:

'aliases' => array(
  'MyDBFacade'   =>  'YourNameSpace\Facades\MyEventFacade::class',
),

Remember to execute composer dump-autoload at the end.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!