Download the PHP package sherlockode/configuration-bundle without Composer
On this page you can find all versions of the php package sherlockode/configuration-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sherlockode/configuration-bundle
More information about sherlockode/configuration-bundle
Files in sherlockode/configuration-bundle
Package configuration-bundle
Short Description Symfony bundle for user-defined configuration values
License MIT
Informations about the package configuration-bundle
Sherlockode ConfigurationBundle
This bundle is built to allow users to define configuration options, useful in admin panels.
Get rid of the business logic variables in your ENV
or parameters.yml
file !
Available features:
- Define multiple configuration entries available for the users
- Retrieve configuration values anywhere in your code for business logic
- Use or create custom parameter types for a better user experience
Installation
The best way to install this bundle is to rely on Composer:
Setup
Enable the bundle in the kernel
You will need a Parameter
entity in order to store the configuration values in the database.
Configuration
The entity class you just created must be set in the bundle's configuration:
Now you are free to define any configuration entry you'd like by using the parameters
key:
Translations
By default parameters labels are not translated in the form provided by the bundle.
If you want to use translations you can define a translation_domain
key globally and/or for each parameter
and use your translation key as the label.
Options
Each type of field may accept a various range of options that can be defined under the options
key.
Every field may have a required
option to define if the input field will be required (it defaults to true).
The other options are up to the field and its needs. For instance, the choice field allow to define
multiple
and choices
options in order to customize the form.
Every field may also have a constraints
option to define validation constraints.
You can use Symfony built-in constraints or add your own:
Usage
Default controller
All configured parameters can be edited with the provided controller.
You may import the routing file in order to access it, the listing will be available at URI /parameters
.
You can also modify the default template used by this controller with the templates
configuration entry point:
Custom code for the Form
If you have more specific usages, you can build your own form using the ParametersType
,
a FormType
dedicated to editing the parameters.
The model data for the form is an associative array of the paths and existing values.
You can get the existing parameters from the DB using the service sherlockode_configuration.parameter_manager
:
Access configuration values
You are now able to retrieve any configuration value by using the get
method from the parameter manager.
It is possible to provide a default value to return if the entry has not been set yet.
Import / export from the browser
You can export or import parameters in a yaml file. You have two routes for these operations:
sherlockode_configuration.export
sherlockode_configuration.import
You also can customize the import form template by defining your own in the configuration:
Import / export from CLI
You can export or import your parameters with these two Symfony commands:
sherlockode:configuration:export
sherlockode:configuration:import
These commands rely on the Symfony secrets mechanism. So be sure to generate your credentials with the following command:
Events
When parameters are saved in database, multiple events are dispatched
Sherlockode\ConfigurationBundle\Event\PreSaveEvent
is dispatch before saving parameters;Sherlockode\ConfigurationBundle\Event\SaveEvent
is dispatch just after saving new parameters values in database;Sherlockode\ConfigurationBundle\Event\PostSaveEvent
is dispatch just after the SaveEvent. It can be useful for customizing redirection or adding flash message for example;
Field types
Default types
Here are the field types provided in the bundle, located in the namespace Sherlockode\ConfigurationBundle\FieldType
:
- simple
- checkbox
- choice
- datetime
- entity
- image
- password
Custom Field types
In order to add custom field types, you should create a service implementing the FieldTypeInterface
interface
and tag it with sherlockode_configuration.field
(or use autoconfiguration).
The getName()
return value is the alias of the field type to use in the configuration (like simple
or choice
).
Using transformers
Due to the format of the Parameter entity in the database (the value is stored as a string, whatever the parameter type), complex values cannot be stored directly. For instance, we can serialize an array to fit the string type, or we may store the ID of a database entity. The process may vary depending on your needs and the value to store, but the application needs to be aware of the process to transform the PHP data into a string and the opposite process. This is done through transformers.
A transformer is an object implementing the Sherlockode\ConfigurationBundle\Transformer\TransformerInterface
.
The interface has two methods transform
and reverseTransform
, similarly to the transformers used by Symfony in the Form Component.
The transform
method takes the string representation and returns the PHP value,
when the reverseTransform
takes your PHP value and returns back the corresponding scalar value.
In order to be used, an instance of the transformer should be returned by the getModelTransformer
method of the corresponding field type. If this method returns null
, the bundle considers that no transformation is needed.
The bundle also provides a CallbackTransformer
that can be used for faster implementations.
For instance handling an array can be done like this :
License
This bundle is under the MIT license. Check the details in the dedicated file
All versions of configuration-bundle with dependencies
symfony/framework-bundle Version ^6.0 || ^7.0
symfony/validator Version ^6.0 || ^7.0
twig/twig Version ^3.0