Nested variables in Symfony .env - symfony

I need to use nested variables in Symfony's .env file. I tried this:
# .env
DIRECTORY=my_bucket
PREFIX_URI='cdn.example.com/%env(DIRECTORY)%'
Then I need to use PREFIX_URI in a .yaml configuration file in config/packages:
vich_uploader:
mappings:
fs_name:
uri_prefix: '%env(PREFIX_URI)%'
This doesn't seem to work however. The variables are just ignored. How should I go about it?

Your .env file is not using PHP. The correct way to reference another variable is like this:
DIRECTORY=my_bucket
PREFIX_URI="cdn.example.com/${DIRECTORY}"

Related

How to set up default manifest_json_path while having multiple configurations?

I'm working on setting up a project for multiple configurations. I know that if you want to have multiple json_manifest_paths, you need modify config.yml like this:
assets:
base_urls:
- "%cdn_url%"
packages:
project_1:
json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
projest_2:
json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
assets:
version: "%asset_version_generated%"
base_path: "/build"
And then you call your assets like this:
<img src="{{ asset("build/desktop/images/background.png", 'project_1') }}" />
But I also want to have a default json_manifest_path which will be targeted if I call my assets without the second parameter, like this:
<img src="{{ asset("build/images/background.png") }}" />
So, sometimes I'll have the project project_1 built inside /build/project_1 and sometimes I'll have project_2, and so on, you get the idea.
I need to have a third option where a project will be built directly inside /build and the way I call assets will be by not giving it a second argument. In that case, manifest.json path will be directly inside /build.
I tried with this but it's giving me an error that it doesn't exist, even though I am not calling my assets without a second argument:
assets:
json_manifest_path: '%kernel.project_dir%/web/build/manifest.json'
base_urls:
- "%cdn_url%"
packages:
project_1:
json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
project_2:
json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
assets:
version: "%asset_version_generated%"
base_path: "/build"
TL;DR
How do I set up a default json_manifest_path inside config.yml which will be targeted if I call my assets without the second parameter?
I did something similar with EventListener whitch listen for onKernelController even and adds the necessary assets package like so:
$strategy = new JsonManifestVersionStrategy(
$this->parameterBag->get('kernel.project_dir').'/public/build/'.$your_project_n.'/manifest.json'
);
$this->assetPackages->setDefaultPackage(new Package($strategy));
Then you don’t need to specify the second argument of the asset function at all.
Similarly you can dynamically add the default package and leave the rest hardcoded In the config file

Symfony using multiple translation files for single language

I was wondering if it is possible with Symfony 3.5 to use multiple translation files for a single language when using yml files.
Currently I have something like this:
AppBundle/Resources/translations/messages.en.yml
AppBundle/Resources/translations/messages.de.yml
which contains all my translations in either language. However I was wondering if it was possible to change this to the following structure:
AppBundle/Resources/translations/en/products.yml
AppBundle/Resources/translations/en/invoices.yml
AppBundle/Resources/translations/de/products.yml
AppBundle/Resources/translations/de/invoices.yml
I have been looking but I have been unable to find some kind of solution for this. I got it working for splitting up my routes.
AppBundle/Resources/config/routing.yml
appbundle_routes:
resource: '#AppBundle/Resources/config/routing'
type: directory
Inside that folder I got all my routes split like:
AppBundle/Resources/config/routing/products.yml
AppBundle/Resources/config/routing/users.yml
AppBundle/Resources/config/routing/invoices.yml
I was wondering if it was possible to achieve the same thing with translations?
Symfony's Translator requires files to by named in format domain.locale.loader. In case you have messages.en.yml:
messages is the default name of domain, you can also specify eg. invoices
en is the locale
yml is specifying YAML loader will be used
So your proposed use is not possible to achieve with standard set of configs and functionality. However, you can split your translations to different domain files. So paths would be:
AppBundle/Resources/translations/products.en.yml
AppBundle/Resources/translations/invoices.en.yml
And when you are using translator you specify the domain in which the translation should be looked for:
$translator->trans('translated.key', [], 'invoices');
Or in Twig:
{{ 'translated.key'|trans({},'invoices') }}

How to use a global config value in a private config file with symfony2

I defined some values in the define.yml in the app/config folder.
And I can access then in the PHP code.
Now I want to use the validate.yml file to check weather users give out too many data, could I use the configed valus in the config file(such as %maxInputLength%) rather than write it one more time in the
validate.yml?
for a example:
I defind a variable in define.yml:
maxLength: 10
So we can use it in the controller.
But if I want to use it in the validate.yml in my own bundle,
such as : myBandle/config/validation.yml
Now I just use the file like that:
myBundle\Entity\UserDesc:
properties:
content:
- NotBlank: ~
- MaxLength: 10
So it seems that the same value was defined at two place.In fact the 10 in the second file is just the value defined in the first file . But I don't know how to get the value from the first file.Is there any way to make that?
AFAIK you want to set value to variable in one config file and then reuse it in other .yml config file.
So you cant define some vars in e.g variables.yml file
parameters:
test_variable: "blabla"
Then in second .yml config file you can import file with defined variables:
imports:
- { resource: variables.yml }
Then you can use imported variable in this file:
property:
some_value: %test_variable%

How to access nested parameter values in Symfony2

I created a parameter in my parameters file:
parameters:
category:
var: test
I can access this in PHP by fetching it like this:
$var = $this->container->getParameter('category');
$var = $var['var'];
But how can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:
twig:
globals:
my_var: %category.var% # throws ParameterNotFoundException
(Sidequestion:
I assumed I could access it via getParamter('category.var'), but got an error there. Do you know a nicer way than my two-liner? $this->container->getParameter('category')['var'] works, but is a syntax error according to my IDE.)
$this->container->getParameter('category')['var']
..is actually a pretty good way to go. Which version of PHP is your IDE using for its syntax checking? Somebody please correct me, but I think this behavior was made valid in 5.3 or 5.4.

How to access arrays in symfony's configuration files?

Does the symfony2 can handle only flat parameters?
Say we have:
services:
manager:
class: blabla
arguments: [%app.vat%]
and in app.yml :
parameters:
app.vat: 24.5
it works, but
parameters:
app:
vat: 24.5
does not work. Is there some special syntax to access arrays or this is not possible?
This is indeed possible. You can access the values from your example in your code like this:
$config = $this->get('service_container')->getParameter('app.vat');
If this is still not working you should try to rename "app" into something else (e.g. "application"). Symfony preserves the name "app" on many places and handles it in a special way.

Resources