Using Laravel service provider to override connector classes

前端 未结 2 1979
南笙
南笙 2021-01-13 14:38

I\'m working in Laravel 5.2 and I\'m trying to make it work with Vertica. A couple of months ago, my colleague and I came up with this solution, but we\'re now trying to mak

相关标签:
2条回答
  • 2021-01-13 15:05

    Laravel does not resolve the Connector classes from the container, so attempting to override the connector by class name will not work.

    You can see in Illuminate/Database/Connectors/ConnectionFactory::createConnector how the connectors are resolved. Laravel just does a return new PostgresConnector (or whichever one is appropriate for the driver), so it does not look in the container for the class name.

    However, before it "new"s up a Connector, it does check the container to see if there is a connector bound to the driver using the string 'db.connector.[driver]', where [driver] is the db driver name.

    Therefore, instead of attempting to bind the class name in the container, you need to bind the string 'db.connector.your-driver-name'. So, if you created your own custom driver (e.g. vertica), you would bind your connector to 'db.connector.vertica'. Or, if you want to overwrite the built in postgres connector, you would bind your connector to 'db.connector.pgsql'.

    Based on the assumption you're trying to overwrite the postgres connector, your service provider register method would look like:

    public function register()
    {
        $this->app->bind('db.connector.pgsql', \App\Vertica\PostgresConnector::class);
    }
    
    0 讨论(0)
  • I couldn't use Illumitane query builder with Vertica fully value through the PostgresConnector connector. Thats why I make laravel-ready VerticaConnector, whos installing by 1 command:

    composer require mixartemev/dbal-vertica-driver
    
    0 讨论(0)
提交回复
热议问题