longitude-one / settings-bundle
一个用于检索应用程序设置的包。
Requires
- php: ^7.4
- doctrine/doctrine-bundle: ~2.1.2
- doctrine/orm: ~2.7.4
- symfony/http-kernel: ~5.1.8
- twig/twig: ^2.12|^3.0|^3.1
Requires (Dev)
- phpunit/phpunit: ~9.1.4
This package is auto-updated.
Last update: 2024-08-29 05:31:14 UTC
README
适用于 Symfony 5.1+ 和 PHP 7.4+ 的设置包
如何在 Symfony 5.1+ 应用程序上安装它
首先,我们需要一个 symfony 5.1 启动应用程序,比如 symfony-standard 版本使用 composer
composer create-project symfony/skeleton [项目名称]
现在让我们添加 longitude-one/settings-bundle
您可以在 Packagist 上找到 doctrine-extensions 项目: https://packagist.org.cn/packages/longitude-one/settings-bundle
将其添加到您的项目中
composer require longitude-one/settings-bundle
如何在 Symfony 5.1+ 应用程序中映射它
让我们从映射开始。您需要将此类映射信息添加到您的 doctrine.orm 配置中,编辑 config/packages/doctrine.yaml
doctrine: dbal: # your dbal config here orm: auto_generate_proxy_classes: %kernel.debug% auto_mapping: true # only these lines are added additionally mappings: settings: type: annotation alias: Settings prefix: LongitudeOne\SettingsBundle\Entity # make sure vendor library location is correct dir: "%kernel.project_dir%/vendor/longitude-one/settings-bundle/src/LongitudeOne/Entity"
之后,运行 symfony console doctrine:mapping:info
,您应该会看到输出
Found xx mapped entities:
[OK] App\Entity\... #Entities of your own entities
...
[OK] LongitudeOne\SettingsBundle\Entity\Settings
如何在您的应用程序中使用设置?
如何创建设置?
您只需创建一个对象并持久化它即可。
namespace App\Controller; use Doctrine\ORM\EntityManagerInterface;use LongitudeOne\SettingsBundle\Entity\Settings; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class FooController extends AbstractController { public function create(EntityManagerInterface $entityManager) { $bar = '42'; //$bar can be an array, an object or anything that can be serialized. $settings = new Settings(); $settings->setCode('foo'); $settings->setValue($bar); $entityManager->persist($settings); $entityManager->flush(); } }
如何在控制器中检索值
首先,您需要将此包的目录添加到 Symfony 的自动注入服务列表中。编辑 config/services.yaml
# these lines are the default one parameters: services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/' exclude: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php' - '../src/Tests/' # only the below lines are added LongitudeOne\SettingsBundle\: resource: '../vendor/longitude-one/settings-bundle/src/LongitudeOne/' exclude: - '../vendor/longitude-one/settings-bundle/src/LongitudeOne/Entity/' - '../vendor/longitude-one/settings-bundle/src/LongitudeOne/Exception/'
现在您可以在自己的控制器和自己的服务中调用设置接口
namespace App\Controller; use LongitudeOne\SettingsBundle\Service\SettingsInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class FooController extends AbstractController { public function getValue(SettingsInterface $settingsManager) { $value = $settingsManager->getValue('foo'); //... } }
如果 foo
设置不存在,则会抛出 SettingsException。
如何在服务中检索值
首先,您需要将此包的目录添加到 Symfony 的自动注入服务列表中。上一段解释了如何操作。
现在,您可以在服务中使用依赖注入功能。
namespace App\Service; use LongitudeOne\SettingsBundle\Service\SettingsInterface; class FooService { private SettingsInterface $settings; public function __construct(SettingsInterface $settings) { $this->settings=$settings; } public function someMethod() { //... $value = $this->settings->getValue('foo'); //... } }
如果 foo
设置不存在,则会抛出 SettingsException。
如何在 Twig 模板中检索值
首先,您需要将此包的目录添加到 Symfony 的自动注入服务列表中。上一段解释了如何操作。
现在,您可以使用 settings
过滤器和 settings
函数来检索您的应用程序设置。
{{ dump('foo'|settings) }} {{ dump(settings('foo')) }}