All Projects → craue → Craueconfigbundle

craue / Craueconfigbundle

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

Projects that are alternatives of or similar to Craueconfigbundle

Webpack Bundle
Bundle to Integrate Webpack into Symfony
Stars: ✭ 124 (-19.48%)
Mutual labels:  bundle, symfony, symfony-bundle
Sonatanewsbundle
Symfony SonataNewsBundle
Stars: ✭ 153 (-0.65%)
Mutual labels:  bundle, symfony, symfony-bundle
Sonataseobundle
Symfony SonataSeoBundle
Stars: ✭ 106 (-31.17%)
Mutual labels:  bundle, symfony, symfony-bundle
Filemanagerbundle
FileManager is a simple Multilingual File Manager Bundle for Symfony
Stars: ✭ 105 (-31.82%)
Mutual labels:  bundle, symfony, symfony-bundle
Nelmiocorsbundle
The NelmioCorsBundle allows you to send Cross-Origin Resource Sharing headers with ACL-style per-URL configuration.
Stars: ✭ 1,615 (+948.7%)
Mutual labels:  bundle, symfony, symfony-bundle
Notification Bundle
A simple Symfony bundle to notify user
Stars: ✭ 103 (-33.12%)
Mutual labels:  bundle, symfony, symfony-bundle
Swiftmailer Bundle
Symfony Swiftmailer Bundle
Stars: ✭ 1,558 (+911.69%)
Mutual labels:  bundle, symfony, symfony-bundle
Web Server Bundle
WebServerBundle provides commands for running applications using the PHP built-in web server. It simplifies your local development setup because you don't have to configure a proper web server such as Apache or Nginx to run your application.
Stars: ✭ 1,281 (+731.82%)
Mutual labels:  bundle, symfony, symfony-bundle
Web Profiler Bundle
The WebProfilerBundle provides detailed technical information about each request execution and displays it in both the web debug toolbar and the profiler.
Stars: ✭ 1,905 (+1137.01%)
Mutual labels:  bundle, symfony, symfony-bundle
Crauegeobundle
Doctrine functions for calculating geographical distances in your Symfony project.
Stars: ✭ 112 (-27.27%)
Mutual labels:  bundle, symfony, symfony-bundle
Easy Doc Bundle
Symfony application documentation generator
Stars: ✭ 99 (-35.71%)
Mutual labels:  bundle, symfony, symfony-bundle
Sonatanotificationbundle
Symfony SonataNotificationBundle
Stars: ✭ 136 (-11.69%)
Mutual labels:  bundle, symfony, symfony-bundle
Easy Security Bundle
EasySecurityBundle
Stars: ✭ 95 (-38.31%)
Mutual labels:  bundle, symfony, symfony-bundle
Passwordstrengthbundle
Symfony Password strength and blacklisting validator bundle
Stars: ✭ 123 (-20.13%)
Mutual labels:  bundle, symfony, symfony-bundle
Swarrotbundle
A symfony bundle for swarrot integration
Stars: ✭ 89 (-42.21%)
Mutual labels:  bundle, symfony, symfony-bundle
Liipcachecontrolbundle
DEPRECATED! This bundle is superseded by FOSHttpCacheBundle. A migration guide is in the README of LiipCacheControlBundle
Stars: ✭ 108 (-29.87%)
Mutual labels:  bundle, symfony, symfony-bundle
Knpmenubundle
Object Oriented menus for your Symfony project.
Stars: ✭ 1,242 (+706.49%)
Mutual labels:  bundle, symfony, symfony-bundle
Pugxautocompleterbundle
Add an autocomplete field to your Symfony forms
Stars: ✭ 83 (-46.1%)
Mutual labels:  bundle, symfony, symfony-bundle
Liipimaginebundle
Symfony Bundle to assist in imagine manipulation using the imagine library
Stars: ✭ 1,516 (+884.42%)
Mutual labels:  bundle, symfony, symfony-bundle
Vichuploaderbundle
A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
Stars: ✭ 1,613 (+947.4%)
Mutual labels:  bundle, symfony, symfony-bundle

Information

Build Status Coverage Status

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.

Installation

Get the bundle

Let Composer download and install the bundle by running

composer require craue/config-bundle

in a shell.

Enable the bundle

If you don't use Symfony Flex, register the bundle manually:

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

Or, for Symfony 3.4:

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

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
craue_config_settings:
  resource: "@CraueConfigBundle/Resources/config/routing/settings.xml"
  prefix: /settings

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

# 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:

# 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.

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.

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...

// 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:

// 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:

  • 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.

Usage in Twig templates

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

{{ 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:

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).

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 craue_config_cache
Cache implementation: DoctrineCacheBundle

Set the parameter craue_config.cache_adapter.class appropriately and configure a so-called cache provider with the alias 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
Cache implementation: Symfony Cache component

Set the parameter craue_config.cache_adapter.class appropriately and configure a so-called cache pool with the service id 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%'

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:
  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:

# 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.

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.

# 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.

So create the entity and its appropriate mapping:

// 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:

# in app/config/config.yml
craue_config:
  entity_name: MyCompany\MyBundle\Entity\MySetting
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].