rmunate / laravel-config-runtime
`LaravelRuntime`库使您能够在运行时修改Laravel配置值。需要注意的是,这些更改不会影响`.env`文件中的值;它们只会应用于您的脚本运行期间。
Requires
- php: ^7.4|^8.0
- illuminate/collections: ^9.0|^10.0
- illuminate/contracts: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
This package is auto-updated.
Last update: 2024-01-07 19:27:35 UTC
README
《code>LaravelRuntime库使您能够在运行时修改Laravel配置值。需要注意的是,这些更改不会影响.env
文件中的值;它们只会应用于执行脚本时。
⚙️此库与Laravel 8.0及更高版本兼容⚙️
📖 西班牙语文档 📖
以下我们将展示各种可能的示例,以展示其广泛的可用性。
目录
安装
要使用Composer安装该包,请执行以下命令
composer require rmunate/laravel-config-runtime
可用方法
在这里,我们将展示库的可能用途。它简单且非常灵活,让您能够轻松掌握Laravel配置。
获取所有配置数据
如果您想以关联数组的形式获取所有当前配置数据,可以轻松地按照以下方式操作
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // Main method LaravelRuntime::config()->all(); // Alias of the previous method LaravelRuntime::config()->get();
获取特定文件的配置
请记住,Laravel有一个名为"config"的文件夹,其中存储了各种配置文件。此包允许您定义要咨询哪些配置文件。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // Get the complete configuration status of the defined file LaravelRuntime::config()->file('app')->get();
检索值的方法
现在,如果您想在运行时访问当前Laravel配置中的特定值,可以使用以下任何一种方法
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // Define the file and then retrieve the value LaravelRuntime::config()->file('app')->get('name'); // Without defining the file, use only the get method indicating the full path LaravelRuntime::config()->get('app.name');
当无法检索值时抛出异常
如果需要,您可以在尝试检索框架配置中不存在的值时抛出异常。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // Define the file and then retrieve the value LaravelRuntime::config()->file('app')->getOrFail('names'); // Without defining the file, use only the get method indicating the full path LaravelRuntime::config()->getOrFail('name2');
获取多个值
考虑不要总是使用GET方法来获取多个值,使用这种查询方法,您可以同时发送您想要检索的值。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // You can send an array or simply the values you require separated by commas. LaravelRuntime::config()->getMany('app.name', 'cache.default'); LaravelRuntime::config()->getMany(['app.name', 'cache.default']);
验证配置值的存在
如果检查配置值是否存在是必要的,那么这个方法会非常简单。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // You can send the full route of the value to search for. LaravelRuntime::config()->has('app.name'); // You can also define the file to search in. LaravelRuntime::config()->file('app')->has('name');
验证多个配置值的存在
如果您想验证多个配置值,那么这可能是您需要的函数。注意,您在这里不能定义要查询的文件,因为使用值的完整路径可以允许您同时验证多个文件。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // You can send an array or simply the full paths separated by commas. LaravelRuntime::config()->hasMany('app.name', 'cache.default'); LaravelRuntime::config()->hasMany(['app.name', 'cache.default']);
更改配置值
这非常有用;您可以通过简单地定义您需要的内容来在运行时更改任何配置值。需要更改数据库连接?想使用自定义电子邮件进行通知?在这种情况下,这就是解决方案。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // You can send an array or simply the full paths separated by commas. LaravelRuntime::config()->file('mail')->set('mailers.smtp.username', 'xxxx@xxxx.com'); LaravelRuntime::config()->file('mail')->set('mailers.smtp.password', 'xxxxxxx'); // You can also change values without defining the file to intervene. LaravelRuntime::config()->set('app.name', 'CodeMaestro');
删除配置值
如果您需要,这是只有您知道的。如果是这样,那么它就在这里,快速且简单。
use Rmunate\LaravelConfigRuntime\LaravelRuntime; // You can define the file to use if you wish; this property will be set to null while the script finishes. LaravelRuntime::config()->file('app')->unset('name'); LaravelRuntime::config()->unset('app.name');
创建者
- 🇨🇴 Raúl Mauricio Uñate Castro
- 电子邮件: raulmauriciounate@gmail.com
许可证
本项目受MIT许可证保护。
🌟 支持我的项目!🚀
做出您认为合适的贡献;代码完全属于您。我们可以一起做一些惊人的事情,并改善开发世界。您的支持是无价的。✨
如果您有想法、建议或只是想要合作,我们开放接受一切!加入我们的社区,成为我们成功旅程的一部分!🌐👩💻👨💻