damian972 / options-bundle
v1.1
2021-09-21 18:14 UTC
Requires
- php: >=7.4
- doctrine/orm: ^2.9
- symfony/dependency-injection: ^5.3
- symfony/framework-bundle: ^5.3
- symfony/yaml: ^5.3
README
此包的主要目标是能够从数据库中恢复存储的选项(键/值)。
安装
composer require damian972/options-bundle
使用
// HelloController.php use Damian972\OptionsBundle\Contracts\OptionsInterface; use Damian972\OptionsBundle\Entity\Option; ... // note: this example is tested with PHP 8 public function greeting(OptionsInterface $options): Response { $options->set(Option::make('greeting', 'World')); // define a parent if you have multiple key with the same name // will create an new entry with parent field "global" $options->set(Option::make('greeting', 'World', 'global')); // if you want to use a custom name $options->set(Option::make('greeting', 'World')->setName('Greeting to the world')); $greeting = 'Hello'; $greetingOpt = $options->get('greeting'); // or $options->get('greeting', 'global') if you want the entry with the parent "global" if (null !== $greetingOpt) { $greeting .= " {$greetingOpt}"; // or $greetingOpt->getValue(); } else { $greeting .= ' John'; } ... }
配置
# config/packages/options.yaml options: # when set to true every call to get an "option" will trigger an SQL query unless it is already called # else, it will make one query to get all the "options" in the database when the get method is called for the first time # and cache others "options" in an associative array lazy: false # optionnal # entity class that represents an "option" object and implements the Damian972\OptionsBundle\Contracts\OptionInterface target_entity: Damian972\OptionsBundle\Entity\Option # optionnal