drcj64241 2017-07-29 05:56
浏览 260
已采纳

Laravel 5:如何将数据库值加载到config / services.php文件中?

I have a multi-tenant app I'm working on and while adding the socialite package, I tried to load the custom facebook client_id and client_secret for the specific website from the database. I can't really use the env variables because each site will have it's own custom facebook keys.

It seems you can't really call a model's method on the config/services.php file because it might not have been loaded yet. I've tried going through the request lifecycle docs to resolve this to no avail.

I've also tried to create a service provider to get the value from my Business model's method and set it as a constant but still, by the time it's available in the app, the config/services.php file has been loaded.

Here's where I want the database value available:

config/services.php

'facebook' => [
  'client_id' => \App\Business::getAppKeys()->fb_client_id,
  'client_secret' => 'your‐fb‐app‐secret',
  'redirect' => 'http://your‐callback‐url',
],

Error:

Fatal error: Call to a member function connection() on null

  • 写回答

2条回答 默认 最新

  • doudoulb1234 2017-08-25 21:32
    关注

    You really should not want to do this. Initialising DB connections from a model requires all config to be loaded, and you intend to use these connections to define your config. You've found yourself having a circular dependency problem.

    I'd like to suggest having a look at the socialite package you're trying to use. If no facilities exist in the service to set/override credentials at runtime, see if you're able to extend the service with your own implementation that does allow for that. I think that will be the only way to accomplish what you're trying to do.

    Besides all that, config files are supposed to be composed of only scalar values and arrays, so that they can be cached.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable