nti/config-bundle

通过服务在您的Symfony项目中提供数据库存储的设置。

安装: 13

依赖项: 0

建议者: 0

安全: 0

星星: 0

关注者: 1

分支: 35

类型:symfony-bundle

2.1.0 2018-01-02 19:53 UTC

This package is auto-updated.

Last update: 2024-09-16 19:25:58 UTC


README

Build Status Coverage Status

CraueConfigBundle管理存储在数据库中的配置设置,并通过服务在您的Symfony项目中提供访问。这些设置类似于在parameters.yml中定义的设置,但在运行时可以修改,例如由管理员用户修改。

安装

获取包

在shell中运行以下命令,让Composer下载并安装包:

php composer.phar require craue/config-bundle:~2.1

启用包

// in app/AppKernel.php
public function registerBundles() {
	$bundles = array(
		// ...
		new Craue\ConfigBundle\CraueConfigBundle(),
	);
	// ...
}

创建表

最好通过调用以下方法来完成此操作:

# in a shell (run `bin/console` instead of `app/console` if your project is based on Symfony 3)
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate

# in a shell (run `bin/console` instead of `app/console` if your project is based on Symfony 3)
php app/console doctrine:schema:update

或您喜欢的任何方式。

添加管理设置的路线(可选)

您可以选择导入默认的路由配置

# in app/config/routing.yml
craue_config_settings:
  resource: "@CraueConfigBundle/Resources/config/routing/settings.xml"
  prefix: /settings

...或者添加您自己的以完全控制URL模式。

# in app/config/routing.yml
craue_config_settings_modify:
  path: /settings/modify
  defaults:
    _controller: CraueConfigBundle:Settings:modify

用法

定义设置

此包不提供创建新设置的功能,因为这没有意义。这些设置将用于您的应用程序,因此需要编写代码。这意味着您必须自己创建数据库表craue_config_setting中的新设置,例如使用迁移。

管理设置值

如果您添加了上述路线,您可以通过简单的表单管理所有定义的设置值。默认情况下,您可以通过浏览到app_dev.php/settings/modify来访问该表单。但您可能希望限制在安全配置中对该表单的访问。

读取设置

该包提供了一个名为craue_config的服务。在控制器内部,您可以调用以下代码

$this->get('craue_config')->get('name-of-a-setting')

来检索设置name-of-a-setting的值。此外,您可以调用以下代码

$this->get('craue_config')->all()

来获取所有定义的设置及其值的关联数组。

$this->get('craue_config')->getBySection('name-of-a-section')

将仅获取具有指定部分的设置(或显式传递null作为名称时没有部分的设置)。

写入设置

使用相同的服务,您可以设置设置的新的值

$this->get('craue_config')->set('name-of-a-setting', 'new value');
$this->get('craue_config')->setMultiple(array('setting-1' => 'foo', 'setting-2' => 'bar'));

请注意,设置必须存在,否则将抛出异常。

在Twig模板中使用

此包中的Twig扩展支持在模板中直接读取设置。

{{ craue_setting('name-of-a-setting') }}

启用缓存(可选)

为了减少数据库查询次数,您可以设置设置缓存。首先,您必须选择您想使用的缓存实现。目前,有可用于以下内容的适配器:

请参阅每个实现的文档以获取详细信息,并在下面的相应部分继续阅读。完成后,CraueConfigBundle将自动缓存设置(使用内置的craue_config_cache_adapter服务)。

请注意,在修改应用程序之外(例如,通过Doctrine迁移)修改设置后,请清除缓存(如果需要)

# in a shell (run `bin/console` instead of `app/console` if your project is based on Symfony 3)
php app/console doctrine:cache:clear craue_config_cache

缓存实现:DoctrineCacheBundle

设置参数craue_config.cache_adapter.class并配置一个名为craue_config_cache_provider的缓存提供者

# in app/config/config.yml
parameters:
  craue_config.cache_adapter.class: Craue\ConfigBundle\CacheAdapter\DoctrineCacheBundleAdapter

doctrine_cache:
  providers:
    craue_config_cache:
      apc: ~
      namespace: craue_config
      aliases:
        - craue_config_cache_provider

缓存实现:Symfony缓存组件

设置参数craue_config.cache_adapter.class并配置一个名为craue_config_cache_provider的缓存池

# in app/config/config.yml
parameters:
  craue_config.cache_adapter.class: Craue\ConfigBundle\CacheAdapter\SymfonyCacheComponentAdapter

services:
  craue_config_cache_provider:
    class: Symfony\Component\Cache\Adapter\FilesystemAdapter
    public: false
    arguments:
      - 'craue_config'
      - 0
      - '%kernel.cache_dir%'

自定义

在提交内置表单后重定向到不同的页面

如果您已启用内置表单,可以通过设置目标路由名称来定义成功保存更改后重定向的位置。

# in app/config/parameters.yml
parameters:
  craue_config.redirectRouteAfterModify: craue_config_settings_modify

部分设置渲染

如果您想在组(此处称为部分)中渲染设置,您必须将这些设置分配一个共同的名称(在数据库中)。可选地,您可以影响这些部分的顺序。

# in app/config/parameters.yml
parameters:
  craue_config.configTemplate.sectionOrder: [section1, section2, section3]

没有部分的设置将首先渲染。没有明确排序的部分将最后渲染。

翻译

您可以通过将它们添加到具有 CraueConfigBundle 域的翻译文件中来为要显示在表单中的所有设置(和部分)添加翻译,例如:

# in app/Resources/CraueConfigBundle/translations/CraueConfigBundle.en.yml
name-of-a-setting: name of the setting

# in app/Resources/CraueConfigBundle/translations/CraueConfigBundle.de.yml
name-of-a-setting: Name der Einstellung

使用自定义实体设置

自定义实体必须提供对 value 字段的映射。类 BaseSetting 定义了此字段,但没有提供对它的映射。这允许轻松覆盖,包括数据类型。在下面的示例中,value 字段将被映射到一个 text 列,这将反过来将内置表单字段渲染为 textarea

因此,创建实体及其相应的映射

// src/MyCompany/MyBundle/Entity/MySetting.php
use Craue\ConfigBundle\Entity\BaseSetting;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Craue\ConfigBundle\Repository\SettingRepository")
 * @ORM\Table(name="my_setting")
 */
class MySetting extends BaseSetting {

	/**
	 * @var string|null
	 * @ORM\Column(name="value", type="text", nullable=true)
	 */
	protected $value;

	/**
	 * @var string|null
	 * @ORM\Column(name="comment", type="string", nullable=true)
	 */
	protected $comment;

	public function setComment($comment) {
		$this->comment = $comment;
	}

	public function getComment() {
		return $this->comment;
	}

}

并且使包知道它

# in app/config/config.yml
craue_config:
  entity_name: MyCompany\MyBundle\Entity\MySetting