承接 effiana/config-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

effiana/config-bundle

Composer 安装命令:

composer require effiana/config-bundle

包简介

Database-stored settings made available via a service for your Symfony project.

README 文档

README

Build Status Coverage Status

ConfigBundle manages configuration settings stored in the database and makes them accessible via a service in your Symfony project. These settings are similar to those defined in parameters.yml but can be modified at runtime, e.g. by an admin user.

Installation

Get the bundle

Let Composer download and install the bundle by running

php composer.phar require effiana/config-bundle:~2.2

in a shell.

Enable the bundle

// in app/AppKernel.php
public function registerBundles() {
	$bundles = [
		// ...
		new Effiana\ConfigBundle\EffianaConfigBundle(),
	];
	// ...
}

Create the table

Preferably you do this by calling

# in a shell
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

or

# in a shell
php bin/console doctrine:schema:update

or however you like.

Add the route to manage settings (optional)

You can either import the default routing configuration

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

...or add your own to have full control over the URL pattern.

# in app/config/routing.yml
effiana_config_settings_modify:
  path: /settings/modify
  defaults:
    _controller: Effiana\ConfigBundle\Controller\SettingsController::modifyAction

Some CSS is needed to render the form correctly. Install the assets in your project:

# in a shell
php bin/console assets:install --symlink web

Usage

Defining settings

This bundle does not provide functionality to create new settings because this would make no sense at runtime. Those settings will be used in your application and thus code needs to be written for that. This means that you have to create new settings in the database table effiana_config_setting yourself, e.g. using a migration.

Managing settings' values

If you added the route described above you can manage the values of all defined settings in a simple form. By default, you can access that form by browsing to app_dev.php/settings/modify. But you probably want to limit access to this form in your security configuration.

Reading settings

The bundle provides a service called effiana_config. Inside of a controller you can call

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

to retrieve the value of the setting name-of-a-setting. Furthermore, you can call

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

to get an associative array of all defined settings and their values.

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

will fetch only settings with the specified section (or those without a section if explicitly passing null for the name).

Writing settings

With the same service you can set new values of settings:

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

Keep in mind that the setting has to be present, or an exception will be thrown.

Usage in Twig templates

The Twig extension in this bundle supports reading settings directly in your template.

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

Enable caching (optional)

To reduce the number of database queries, you can set up a cache for settings. First, you have to choose which cache implementation you'd like to use. Currently, there are adapters available for:

Refer to the documentation of each implementation for details and read on in the corresponding section below. When done, ConfigBundle will automatically cache settings (using the built-in effiana_config_cache_adapter service).

Keep in mind to clear the cache (if needed) after modifying settings outside of your app (e.g. by Doctrine migrations):

# in a shell
php bin/console doctrine:cache:clear effiana_config_cache

Cache implementation: DoctrineCacheBundle

Set the parameter effiana_config.cache_adapter.class appropriately and configure a so-called cache provider with the alias effiana_config_cache_provider:

# in app/config/config.yml
parameters:
  effiana_config.cache_adapter.class: Effiana\ConfigBundle\CacheAdapter\DoctrineCacheBundleAdapter

doctrine_cache:
  providers:
    effiana_config_cache:
      apc: ~
      namespace: effiana_config
      aliases:
        - effiana_config_cache_provider

Cache implementation: Symfony Cache component

Set the parameter effiana_config.cache_adapter.class appropriately and configure a so-called cache pool with the service id effiana_config_cache_provider:

# in app/config/config.yml
parameters:
  effiana_config.cache_adapter.class: Effiana\ConfigBundle\CacheAdapter\SymfonyCacheComponentAdapter

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

Customization

Redirect to a different page after submitting the built-in form

If you've enabled the build-in form, you can define where to redirect on successfully saving the changes by setting the target route name:

# in app/config/parameters.yml
parameters:
  effiana_config.redirectRouteAfterModify: effiana_config_settings_modify

Rendering of settings in sections

If you want to render settings in a group (called section here), you'll have to assign those settings a common section name (in the database). Optionally, you can influence the order of these sections:

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

Settings without a section will be rendered at first. Sections without explicit ordering are rendered at last.

Translation

You can add translations for all settings (and sections) to be shown in the form by adding them to translation files with the ConfigBundle domain, e.g.

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

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

Using a custom entity for settings

The custom entity has to provide a mapping for the field value. The class BaseSetting defines this field, but no mapping for it. This allows easy overriding, including the data type. In the following example, the value field will be mapped to a text column, which will in turn render the built-in form fields as textarea.

So create the entity and its appropriate mapping:

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

/**
 * @ORM\Entity(repositoryClass="Effiana\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;
	}

}

And make the bundle aware of it:

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

effiana/config-bundle 适用场景与选型建议

effiana/config-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 239 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 effiana/config-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 effiana/config-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 effiana/config-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 239
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-16