简体   繁体   中英

Laravel: Update .env file by publishing a third-party package

I have a library which I want to wrap into Laravel package.

My library uses two configs: the public config.php for some nonsensitive settings and .env private config for sensitive credentials.

I'd like to vendor:publish my configs for end user. And it seems fine for public config.php but how to publish my .env in Laravel-friendly way? I failed to find any way to do it by 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. 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.

You'll see this approach quite often in packages providing roles, permissions, tenancy, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM