aedart / laravel-config
1.1.0
2015-06-21 17:25 UTC
Requires
- php: >=5.5.9
- aedart/laravel-detector: 1.1.*
- illuminate/config: 5.1.*
Requires (Dev)
- aedart/license: 1.*
- aedart/license-file-manager: 1.*
- aedart/testing-laravel: 1.1.*
- codeception/codeception: 2.0.*
- fzaninotto/faker: 1.5.*@dev
This package is auto-updated.
Last update: 2022-02-01 12:46:17 UTC
README
用于 Laravel 配置仓库实例的获取器和设置器包。它作为 Config
面向对象的替代品。
内容
[目录]
何时使用此包
当你的组件需要使用某种类型的配置时。或者如果你的组件需要使用 Laravel 的特定配置。
如何安装
Laravel 版本 5.0.x
#!console
composer require aedart/laravel-config 1.0.*
此包使用 composer。如果你不知道它是什么或者它是如何工作的,我建议你在尝试使用此包之前先了解一下。
快速入门
假设你有一个接口,例如用于命令,你可以扩展配置感知接口;
#!php
<?php
use Aedart\Laravel\Config\Interfaces\ConfigAware;
interface ICommand extends ConfigAware {
// ... Remaining interface implementation not shown ...
}
在你的具体实现中,你只需使用配置特性;
#!php
<?php
use Aedart\Laravel\Config\Traits\ConfigTrait;
class DoSomethingCommand implements ICommand {
use ConfigTrait;
// ... Remaining implementation not shown ...
}
默认配置仓库实例
ConfigTrait
默认将返回当前正在运行的 Laravel 配置仓库实例(如果有),前提是你的组件运行在 Laravel 应用程序内部。
然而,如果没有可用的(不在 Laravel 应用程序内部运行),则返回一个默认的空 Illuminate\Contracts\Config\Repository
实例。
默认配置仓库验证
默认情况下,ConfigTrait
将始终假设指定的配置仓库实例是有效的。但是,如果你需要确保已设置了特定的配置或某些条目可用,或者需要进行其他类型的配置特定验证,则可以通过重写 isConfigValid()
方法来实现。
#!php
<?php
use Aedart\Laravel\Config\Traits\ConfigTrait;
use Illuminate\Contracts\Config\Repository;
class DoSomethingCommand implements ICommand {
use ConfigTrait;
public function isConfigValid(Repository $repository){
// In this example, a configuration repository is only valid
// if there is a database.default entry
return $repository->has('database.default'));
}
// ... Remaining implementation not shown ...
}
致谢
Taylor Otwell et al. 为创造了最好的 PHP 框架之一。
许可
BSD-3-Clause,阅读此包中包含的 LICENSE 文件