Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

php - laravel 5 : Extend a Facade

I need to handle different types of DB depending on the client.

I created a Facade called MyDBFacade where I can call my own functions.

For example:

MyDBFacade::createDBUser("MyUser"); // will create a DB user whatever I'm using Postgres or SQL Server

Is there a possibility to extends the framework Facade DB:: in a way I could add my own functions and then call DB::createUser("MyUser") ?

Any clue or idea would be appreciate.

Thanks in advance, have a nice day.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

<?php

namespace AppFacades;

use IlluminateSupportFacadesDB;

class MyDBFacade extends DB
{
    // ...
}

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

'DB' => IlluminateSupportFacadesDB::class,

to

'DB' => AppFacadesMyDBFacade::class,

And it all should work now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...