My goal is to add to a new Symfony 4.4 project an extra config file to define some behavior of the system. It could be anything, like, pancakes.yaml:
pancakes:
enablePancakes: false
I wish to know how can I load that config file. find a way to read its parameters and values to change some custom behavior the system might have but honestly I think I'm not smart enough to understand what the documentation says.
For now it could be anything, like printing the configuration file values, for now I only need to know how to load it.
you can update the following file :
# config/service.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
parameters:
locale: 'en'
chat_update_interval: 10000
and use service decoration in your new application to override your parameters.
Related
I'm a long time Zend Framework user (now renamed Laminas). But I decided to give a try to last Symfony version. So I just installed it in 5.1.2.
I'm facing a question regarding the multiple environments deployments. In my compay, we have :
Local environment which is developer pc.
Development.
Staging.
Production.
In ZF-Laminas, we have a global.php file which is located in config directory.
For those of you who are not familiar with this framework, you can override key set in global.php file by creating local.php file.
In this global file, I use to put standard configuration for my application.
For example (prod) :
'open_id' => [
'client_id' => 1234
]
Then, I have development and staging files which car override those values for every environmenet. During the deployment, the file corresponding to the environment is copied to local.php.
Let's say staging.local.php.dist becomes local.php with :
'open_id' => [
'client_id' => 5678
]
Which is fine because value is overriding the one from global file.
I would like the same behavior in Symfony but I don't see something similar in Symfony 5.
So far, I only found two possibilities
Create a bundle which will allow me to have a <bundle_name>.yaml file in config/packages directory. According to the documentation (https://symfony.com/doc/current/configuration.html#configuration-files), I will be able to have dev, prod and staging overrides. But it forces me to create a bundle to handle just some standard configurations, which is huge.
Use .env files. But .env files only allow string data, not complex data like arrays.
What do I miss ? Or is it my "zend" way of doing things that is wrong ?
Thanks.
You can also create services_%env%.yaml (services_dev.yaml, services_test.yaml) files for each environment. It will allow you to define different parameters and override/define services for each environment.
Example:
config/services_dev.yaml
parameters:
hello: 'world'
From what I understand from your post, your goal is to have different config values based on the server you are on. If this is the case, you can use environment variables (in the .env file or .env.local for server specific config). You can then use these values in your applications by binding the env var to a parameter. This parameter will then be available within the configuration by using %parameter_name% as value or within the container. You can also pas parameters to services (service definitions are handled the same way as any other config). For more information you can checkout these sources:
https://symfony.com/doc/current/configuration/env_var_processors.html
https://symfony.com/doc/current/configuration.html
I am using LexikTranslationBundle for translate contents in my project. I have been reading carefully the documentation for the bundle and I have setup my project as follow:
# LexikTranslationBundle Configuration
lexik_translation:
fallback_locale: [en]
managed_locales: [en, es]
storage:
type: mongodb
object_manager: default
Docs says:
The bundle overrides the translator service and provides a
DatabaseLoader. Database translations content is loaded last so it
overrides content from xliff, yml and php translations files. You can
also export translations from the database into files in case you need
to get translations files with the same content as the database.
Maybe this mean I depends on .xliff files and can't load translations directly from DB which sucks.
As a side note I have already loaded all the translations I had in .xliff files into the DB so I've removed the .xliff files from the project.
I want to manage the translations directly from DB and get rid of the files and I've tried but it doesn't work.
Did I miss something here? How do I load the translations directly from DB?
You do not have to remove your .xliff files.
The translator.loader service will decorated when the container is built, and will try to load use the database value: https://github.com/lexik/LexikTranslationBundle/blob/master/DependencyInjection/Compiler/TranslatorPass.php
So, the problem might has to do with clearing your cache. If you test whether:
You are able to make translations via the edit page
Manually clear the cache, and play with the cache settings a bit
lexik_translation:
auto_cache_clean: false
auto_cache_clean_interval: 600
see configuration
I have a base config of my ZF2 application, which is in the following structure of my ZF2 application:
/frontend
/config
/autoload -> (here I have config.php file - which is base config)
/brand -> (here I have config files for my vhosts)
When I need things from my vhosts config I simply do like this:
$this->config()['SomethingGoesHere']
My question here is, when I'm located in my vhost (www.sitename1.com). I would like to access the base config within my vhost, how can I do that guys??
If you want to set different configuration for different virtual-hosts I would not recommend to do it like this. You can set more advanced configuration differently, for example by using environmental variables. You can read more on this here in the documentation chapter: Advanced Configuration Tricks.
The correct solution also totally depends on what variables you are setting and what you want to use them for and where you want to use them. If you provide more details on what you are trying to do (what is in your "base config"?) it would be possible to give a more suitable answer for your needs.
I want to access the base_path (base_url registered) of a specific Asset component directory from a controller in order to store my reports to a specific path preconfigured in config.yml.
I started changing my configuration, after upgrading to Symfony 2.7, like the following:
app/config/config.yml
framework:
assets:
version: 'v5'
version_format: '%%s?version=%%s'
base_path: ~
base_urls: ['http://cdn.example.com', 'https://secure.example.com']
packages:
reports:
base_path: bundles/mybundle
So, when I request a specific route, with the correct request parameters my controller generate the HTML from a particular Twig template and, at the end, it will be converted to PDF using KnpSnappyBundle.
At last, my purpose is to build a list of generated PDF reports accessible from a public assets directory.
$kernel->locateResource()
However, I can access the complete path using a workaround like the following:
$this->container->get('kernel')->locateResource('#MyBundle/Resources/public/reports')
Using parameters.yml
I have also asked for some hints and it seems legit to use the parameters.yml in order to manage the Asset component configuration. So, from the controller, they would be accessed using $this->getParameter() and, at the same time, as a configuration value for Asset.
The simplest way to deal with that is to define it as a parameter in parameters.yml, as you suggested yourself.
It's really easy to get it and it totally makes sense.
Update
I wanted to provide a bit more reasoning for my answer, so I will cite http://symfony.com/doc/current/best_practices/configuration.html as a reference.
Reading there, it seems that you should put into "parameters.yml" all infrastructure parameters which do not really change your application behaviours. I think this applies to your case as well: your application does not change its behaviour according to assets paths, it only needs to know where they are.
So, again, I'd say that putting them in parameters.yml not only provides you an easy solution but also it's a "good practice".
When configuring one bundle in Symfony2, I needed to set up a static path to CSS file from web folder, i.e. a line from config.yml:
content_css: "%path_to_web%/bundles/mybundle/css/styles.css"
%kernel.root_dir% returns the absolute root server path, but what in this case is the way of getting a virtual path to web folder? Is there any special variable for that or do I need to hard code that path?
You need no extra variable. The web root is defined with your webserver config. That's the way you configure the content_css option.
If you can reach your app.php (or app_dev.php) simple via http://www.example.com/app.php, then all assets are simply with the path reachable
content_css: "/bundles/mybundle/css/styles.css"
If you have exposed the whole symfony directory (strictly not recommended) and your app.php is reachable under the http://www.example.com/web/app.php, then simply prefix the path with /web.
content_css: "/web/bundles/mybundle/css/styles.css"
EDIT: Or you use a parameter in the parameters.yml. If you read this and you store your source in git or other (strongly recommended), then you have a paramaters.yml.dist with defaults, and every system (every developer or production server) has his own parameters.yml. Then add a parameter to yours and to the prods (and also to the .dist with some default):
parameters:
# [...] some other parameters
my_web_root: "/myproject/web"
the option looks like
content_css: "%my_web_root%/bundles/mybundle/css/styles.css"
As the directory structure of Symfony is defined in the Standard Edition and not the Symfony2 framework, there is no special parameter to use.
%kernel.root_dir% is defined by using __DIR__ in the AppKernel class. That's the one that should be used as the base path, you can do something like: %kernel.root_dir%/../web/