Laravel 5:如何将数据库值加载到config/services.php文件中? [英] Laravel 5: How can I load database values into the config/services.php file?

查看:77
本文介绍了Laravel 5:如何将数据库值加载到config/services.php文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的多租户应用程序,并且在添加社交名流包时,我尝试从数据库中加载特定网站的自定义facebook client_id和client_secret.我不能真正使用env变量,因为每个站点都有自己的自定义facebook密钥.

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.

似乎您不能真正在config/services.php文件上调用模型的方法,因为它可能尚未加载.我尝试遍历请求生命周期文档以解决此问题.

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.

我还尝试创建一个服务提供者,以从我的业务模型的方法中获取值并将其设置为常量,但直到在应用程序中可用时,仍然已加载config/services.php文件

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',
],

错误:

致命错误:在null上调用成员函数connection()

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

推荐答案

您真的不应该这样做.从模型初始化数据库连接需要加载所有配置,并且您打算使用这些连接来定义您的配置.您发现自己遇到了循环依赖问题.

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.

这篇关于Laravel 5:如何将数据库值加载到config/services.php文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆