[英]Laravel: Update .env file by publishing a third-party package
I have a library which I want to wrap into Laravel package. 我有一个要包装到Laravel包中的库。
My library uses two configs: the public config.php
for some nonsensitive settings and .env
private config for sensitive credentials. 我的库使用两个配置:用于一些非敏感设置的public config.php
和用于敏感凭据的.env
private配置。
I'd like to vendor:publish
my configs for end user. 我想vendor:publish
为最终用户vendor:publish
我的配置。 And it seems fine for public config.php
but how to publish my .env
in Laravel-friendly way? 对于公共config.php
来说似乎很好,但是如何以Laravel友好的方式发布我的.env
? I failed to find any way to do it by vendor:publish
. 我找不到任何方法可以通过vendor:publish
。
The application's .env
file is primarily there for keeping sensitive information out of configuration files and other resources that often get committed to source control. 应用程序的.env
文件主要用于将敏感信息保留在配置文件和其他通常致力于源代码控制的资源中。 It's not intended to be published in any way. 它不打算以任何方式发布。
What you can do is create a separate configuration file with some sensible defaults preset, while at the same time allowing the user to override them. 您可以做的是创建一个单独的配置文件,并预先设置一些合理的默认值,同时允许用户覆盖它们。 For example: 例如:
// my-config.php
return [
'user_model => env('MY_USER_MODEL', App\User::class),
...
];
If needed, they can implement their own User model or omit setting it within their own env file and defaulting to the implementation you've specified. 如果需要,他们可以实现自己的用户模型,也可以在自己的env文件中省略设置它,并默认使用您指定的实现。
You'll see this approach quite often in packages providing roles, permissions, tenancy, etc. 您会在提供角色,权限,租赁等信息的软件包中经常看到这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.