我正在處理多租戶應用程序,並且我想要做的事情之一是能夠在組織/租戶訪問時動態切換數據庫連接該應用程序通過使用其子域來識別租戶。我正在使用多數據庫方法而不是單數據庫方法;到目前爲止,我的實現通過在組織註冊併爲其運行所有必要的遷移時爲組織創建數據庫來自動配置環境。在Laravel 5上調用成員函數connection()null null
我遇到的問題與將tenant
解出IoC容器並將數據庫連接設置爲默認值有關,因此組織/租戶可以訪問其數據。這就是我的AppServiceProvider.php
類的register()
方法是這樣的:
/**
* Register any application services.
*
* @return void
*/
public function register()
{
App::bind('setDbConnection', function($app, $db) {
Config::set("database.connections.{$db[0]}", [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => "{$db[0]}",
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]);
});
App::singleton('tenant', function() {
$server = explode('.', Request::getHost());
if (count($server) === 3 && $server !== 'www') {
return Organization::where('slug', $server[0])->firstOrFail();
}
});
// dd(App::make('tenant')->slug);
if (! App::runningInConsole()) {
App::make('setDbConnection', [App::make('tenant')->slug]);
Config::set('database.default', App::make('tenant')->slug);
}
}
對於Organization.php
模型,我明確地告訴它使用的mysql
連接,但我曾評論說出來;這是一流的樣子:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Organization extends Model
{
// protected $connection = 'mysql';
protected $fillable = ['name', 'slug'];
}
應該發生的是,如果一個組織來的應用程序,例如,samplecompany.example.com
我會使用子域名samplecompany
來查找組織中,然後切換連接在應用程序的生命週期中儘早使用該數據庫,以便租戶可以訪問其信息。如果我在應用程序的其他地方使用相同的Organization
模型,它可以很好地工作,但是當我在AppServiceProvider
類的register()
方法中使用它時,它根本無法解析連接。該測試中的域名是http://bestfinance.global.dev:8000
。
以下是完整的堆棧跟蹤:
Whoops, looks like something went wrong.
1/1 FatalThrowableError in Model.php line 3293:
Call to a member function connection() on null
1. in Model.php line 3293
2. at Model::resolveConnection(null) in Model.php line 3259
3. at Model->getConnection() in Model.php line 1880
4. at Model->newBaseQueryBuilder() in Model.php line 1853
5. at Model->newQueryWithoutScopes() in Model.php line 1823
6. at Model->newQuery() in Model.php line 3503
7. at Model->__call('where', array('slug', 'bestfinance'))
8. at call_user_func_array(array(object(Organization), 'where'), array('slug', 'bestfinance')) in Model.php line 3519
9. at Model::__callStatic('where', array('slug', 'bestfinance')) in AppServiceProvider.php line 52
10. at AppServiceProvider->App\Providers\{closure}(object(Application), array()) in Container.php line 731
11. at Container->build(object(Closure), array()) in Container.php line 629
12. at Container->make('tenant', array()) in Application.php line 697
13. at Application->make('tenant') in Facade.php line 217
14. at Facade::__callStatic('make', array('tenant')) in AppServiceProvider.php line 59
15. at AppServiceProvider->register() in Application.php line 554
16. at Application->register(object(AppServiceProvider)) in ProviderRepository.php line 74
17. at ProviderRepository- >load(array('Illuminate\Auth\AuthServiceProvider', 'Illuminate\Broadcasting\BroadcastServiceProvider', 'Illuminate\Bus\BusServiceProvider', 'Illuminate\Cache\CacheServiceProvider', 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', 'Illuminate\Cookie\CookieServiceProvider', 'Illuminate\Database\DatabaseServiceProvider', 'Illuminate\Encryption\EncryptionServiceProvider', 'Illuminate\Filesystem\FilesystemServiceProvider', 'Illuminate\Foundation\Providers\FoundationServiceProvider', 'Illuminate\Hashing\HashServiceProvider', 'Illuminate\Mail\MailServiceProvider', 'Illuminate\Pagination\PaginationServiceProvider', 'Illuminate\Pipeline\PipelineServiceProvider', 'Illuminate\Queue\QueueServiceProvider', 'Illuminate\Redis\RedisServiceProvider', 'Illuminate\Auth\Passwords\PasswordResetServiceProvider', 'Illuminate\Session\SessionServiceProvider', 'Illuminate\Translation\TranslationServiceProvider', 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'App\Providers\AppServiceProvider', 'App\Providers\AuthServiceProvider', 'App\Providers\EventServiceProvider', 'App\Providers\RouteServiceProvider')) in Application.php line 530
18. at Application->registerConfiguredProviders() in RegisterProviders.php line 17
19. at RegisterProviders->bootstrap(object(Application)) in Application.php line 203
20. at Application->bootstrapWith(array('Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders')) in Kernel.php line 232
21. at Kernel->bootstrap() in Kernel.php line 127
22. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
23. at Kernel->handle(object(Request)) in index.php line 53
24. at require_once('/Users/exampleuser/Sites/multitenant/public/index.php') in server.php line 21
不,它不會返回任何東西,我不斷收到上面突出顯示的錯誤。我只是在檢查,看看我是否真的找回了價值。 – k4r3y