Yada
Yada

Reputation: 31225

Change the name of DB Facade

I'm using Lumen with an existing PHP application.

There's a conflict in the global namespace. The existing app also have a DB class in the global namespace which is conflicting with the Lumen's DB class.

// in vendor/laravel/lument-framework/src/Appliction.php

public function withFacades()
{
    Facade::setFacadeApplication($this);

    if (! static::$aliasesRegistered) {
        static::$aliasesRegistered = true;

        class_alias('Illuminate\Support\Facades\App', 'App');
        class_alias('Illuminate\Support\Facades\Auth', 'Auth');
        class_alias('Illuminate\Support\Facades\Bus', 'Bus');
        class_alias('Illuminate\Support\Facades\DB', 'DB');
        ...
    }
}

If I change class_alias('Illuminate\Support\Facades\DB', 'LumenDB'); solves the problem for me, but I don't want to edit code in the vendor folder.

Is there anyway I can change it programmatically?

Upvotes: 1

Views: 764

Answers (2)

Ap Koponen
Ap Koponen

Reputation: 475

The correct method in newer versions of Laravel/Lumen is to pass an array of "user aliases" as the second argument to the $app->withFacades() method.

$app->withFacades(
    true, // $aliases parameter set to true (default)
    [
    'Illuminate\Support\Facades\DB' => 'LumenDB',
    ] // array of $userAliases
);

Upvotes: 1

Yada
Yada

Reputation: 31225

Alright. I think I have a workaround for now.

In the bootstrap/app.php uncomment or remove this lines

// $app->withFacades();

and replace with.

class_alias('Illuminate\Support\Facades\App', 'App');
class_alias('Illuminate\Support\Facades\Auth', 'Auth');
class_alias('Illuminate\Support\Facades\Bus', 'Bus');
class_alias('Illuminate\Support\Facades\DB', 'LumenDB');
...
...
class_alias('Illuminate\Support\Facades\Validator', 'Validator');

So we will register the facades manually instead of calling the withFacades() function.

Upvotes: 1

Related Questions