In my Sf2 parameters.yml file I've got something like this:
router.request_context.base_url: "asddassda"
website_url: "123%router.request_context.base_url%"
But when I print website_url somewhere, it only shows "123". Other variables like router.request_context.host and router.request_context.scheme are NOT visible as well. Why and how can I fix this?
I got the same problem.
Somehow parameters that name starts as router. are not visable in config.yml. Maybe it's a bug. If I get it by ParametersBag it works fine.
As a workaround I chenge name to routerX. and it works:
# app/config/parameters.ini
parameters:
routerX.request_context.base_url: "asddassda"
website_url: "123%routerX.request_context.base_url%"
And
# app/config/config.yml
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
website_url: "%website_url%"
And then in twig template
{{ website_url }}
Tried. It's working for me.
Are you sure you didn't overwrite this parameter router.request_context.base_url somewhere else in your configuration ?
Related
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}"
I want to realize the following thing: in my parameters.yml I have uploads_url: http://test.site.com. It's a URL to Docker container with my images. Next, I have an entity with names of this pictures.
I want to generate URL in twig like this:
background-image: url({{ 'URL from parameters' + 'name of picture from `entity`' }})
At present time I have the second part of the previous expression (name of the picture). How can I generate the 1st part?
app/config/config.yml
# Twig Configuration
twig:
globals:
url: "%url%"
app/config/parameters.yml
parameters:
url: 'your_url'
official doc How to Inject Variables into all Templates (i.e. global Variables)
twig concatenate How to concatenate strings in twig
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.
I am using HearsayRequireJSBundle to implement RequireJS into my Symfony2 project.
In my Twig template I initialize as follows:
{{ require_js_initialize({ 'main' : 'base/main' }) }}
The file that then is being looked for is:
http://myhost.com/app_dev.php/js/base/main.js
This however returns a 404 not found error.
My config looks like this:
hearsay_require_js:
base_directory: %kernel.root_dir%/../src/Acme/Bundle/BaseBundle/Resources/scripts
require_js_src: bundles/acmebase/assets/scripts/require-2.1.5.min.js
paths:
base: '#AcmeBaseBundle/Resources/scripts'
Did I miss anything here? Any help is appreciated.
There's no need to define the path to your base directory twice - the paths configuration node is for extra paths, with base_directory being the default. Try changing your configuration to this:
hearsay_require_js:
base_directory: %kernel.root_dir%/../src/Acme/Bundle/BaseBundle/Resources/scripts
require_js_src: bundles/acmebase/assets/scripts/require-2.1.5.min.js
and alter your template to this (will now be relative to base_directory):
{{ require_js_initialize({ 'main' : 'main' }) }}
I need to switch 'translation fallback' and 'persistDefaultLocaleTranslation' on.
I know how to change it: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md#default-locale
but don't know where should I access that listener.. Maybe in config.yml? how??
I am looking for something like this:
(config.yml)
...
translatable-extension:
translationFallback:true
persistDefaultLocaleTranslation: true
If you have the StofDoctrineExtensionsBundle installed (https://github.com/stof/StofDoctrineExtensionsBundle), you can configure the default values for this in config.yml
stof_doctrine_extensions:
default_locale: "%locale%"
translation_fallback: true
persist_default_translation: true
Haven't found this is in any docs, but it is in the configuration for the bundle https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/DependencyInjection/Configuration.php#L30
I'm not sure I fully understand what you are asking but in a typical setup you would have the translation fallback referenced in your config.yml like this.
framework:
translator: { fallback: %locale% }
In this example the locale placeholder references the locale setting in your parameters.ini/parameters.yml file.
I believe this is commented out by default in your config.yml but uncommenting this line will effectively enable translations.
You can see the full list of configuration options with along with their defaults here:
http://symfony.com/doc/current/reference/configuration/framework.html#full-default-configuration