2017 © Pedro Peláez
 

symfony-bundle config-bundle

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

image

craue/config-bundle

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

  • Thursday, January 4, 2018
  • by craue
  • Repository
  • 15 Watchers
  • 110 Stars
  • 123,440 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 27 Forks
  • 11 Open issues
  • 18 Versions
  • 8 % Grown

The README.md

Information

Tests Coverage Status, (*1)

CraueConfigBundle 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., (*2)

Installation

Get the bundle

Let Composer download and install the bundle by running, (*3)

composer require craue/config-bundle

in a shell., (*4)

Enable the bundle

If you don't use Symfony Flex, register the bundle manually:, (*5)

// in config/bundles.php
return [
    // ...
    Craue\ConfigBundle\CraueConfigBundle::class => ['all' => true],
];

Create the table

Preferably you do this by calling, (*6)

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

or, (*7)

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

or however you like., (*8)

Add the route to manage settings (optional)

You can either import the default routing configuration, (*9)

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

...or add your own to have full control over the URL pattern., (*10)

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

Some CSS is needed to render the form correctly. Install the assets in your project:, (*11)

# 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 craue_config_setting yourself, e.g. using a migration., (*12)

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 /settings/modify. But you probably want to limit access to this form in your security configuration., (*13)

Reading and writing settings

For accessing settings, the bundle provides the service Craue\ConfigBundle\Util\Config. To use it directly in a controller, either add an autowired type-hinted argument to the action..., (*14)

// in src/Controller/MyController.php
use Craue\ConfigBundle\Util\Config;

public function indexAction(Config $config) {
    // use $config
}

...or let your controller extend Symfony\Bundle\FrameworkBundle\Controller\AbstractController and make the service alias craue_config available by defining getSubscribedServices:, (*15)

// in src/Controller/MyController.php
use Craue\ConfigBundle\Util\Config;

public function indexAction() {
    // use $this->get('craue_config')
}

public static function getSubscribedServices() {
    return array_merge(parent::getSubscribedServices(), [
        'craue_config' => Config::class,
    ]);
}

The service defines the following methods:, (*16)

  • all() - get an associative array of all defined settings and their values
  • get($name) - get the value of the specified setting
  • getBySection($section) - like all(), but get only settings within the specified section (or those without a section if explicitly passing null)
  • set($name, $value) - set the value of the specified setting
  • setMultiple([$name1 => $value1, $name2 => $value2]) - set values for multiple settings at once

Keep in mind that each setting has to be present, or an exception will be thrown., (*17)

Usage in Twig templates

The Twig extension in this bundle supports reading settings directly in your template., (*18)

{{ craue_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: - DoctrineCacheBundle - Symfony Cache component, (*19)

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

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

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

Cache implementation: DoctrineCacheBundle
, (*22)

Set the parameter craue_config.cache_adapter.class appropriately and configure a so-called cache provider with the alias craue_config_cache_provider:, (*23)

```yaml # in app/config/config.yml parameters: craue_config.cache_adapter.class: Craue\ConfigBundle\CacheAdapter\DoctrineCacheBundleAdapter, (*24)

doctrine_cache: providers: craue_config_cache: apc: ~ namespace: craue_config aliases: - craue_config_cache_provider ``` , (*25)

Cache implementation: Symfony Cache component
, (*26)

Set the parameter craue_config.cache_adapter.class appropriately and configure a so-called cache pool with the service id craue_config_cache_provider:, (*27)

```yaml # in app/config/config.yml parameters: craue_config.cache_adapter.class: Craue\ConfigBundle\CacheAdapter\SymfonyCacheComponentAdapter, (*28)

services: craue_config_cache_provider: class: Symfony\Component\Cache\Adapter\FilesystemAdapter public: false arguments: - 'craue_config' - 0 - '%kernel.cache_dir%' ``` , (*29)

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:, (*30)

# in app/config/parameters.yml
parameters:
  craue_config.redirectRouteAfterModify: craue_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:, (*31)

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

Settings without a section will be rendered at first. Sections without explicit ordering are rendered at last., (*32)

Translation

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

# 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

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., (*34)

So create the entity and its appropriate mapping:, (*35)

// 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;
    }

}

And make the bundle aware of it:, (*36)

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

The Versions

04/01 2018

dev-master

9999999-dev https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2 symfony3 symfony4

02/01 2018

2.1.0

2.1.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2 symfony3 symfony4

28/12 2017

dev-test

dev-test https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2 symfony3 symfony4

27/03 2017

1.4.2

1.4.2.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2 symfony3

29/12 2015

1.4.1

1.4.1.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2 symfony3

30/11 2015

1.4.0

1.4.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2

30/11 2015

1.3.2

1.3.2.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2

23/04 2015

dev-doctrine-mapping-xml

dev-doctrine-mapping-xml https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2

27/02 2015

1.3.1

1.3.1.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2

04/11 2014

1.3.0

1.3.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

config symfony2

22/05 2014

1.2.0

1.2.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

08/12 2013

1.1.4

1.1.4.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

21/11 2013

1.1.3

1.1.3.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

25/09 2013

1.1.2

1.1.2.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

02/05 2013

1.1.1

1.1.1.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

28/02 2013

1.1.0

1.1.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2

26/05 2012

1.0.0

1.0.0.0 https://github.com/craue/CraueConfigBundle

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

  Sources   Download

MIT

The Requires

 

by Christian Raue

config symfony2