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.
Related
I am using several Symfony expressions in my TypoScript checking for query parameters such as this:
[request.getQueryParams()['tx_news_pi1']['news'] > 0]
do something
[END]
This is working well – as long as the query parameter exists. If it doesn’t, the following error message is written into the log file:
Unable to get an item on a non-array.
In PHP I would use isset() to check whether the query parameter exists – but I could not find a similar way for Symfony expressions in TypoScript. I have tried
[request.getQueryParams()['tx_news_pi1']['news']]
which works the same, meaning: it does what it’s supposed to do, but logs an error message if the query parameter does not exist.
Is there anything like isset() for the Symfony Expression Language in TYPO3?
The is_defined() or isset() I was looking for will be returned by the condition
[request.getQueryParams()['tx_news_pi1']]
instead of
[request.getQueryParams()['tx_news_pi1']['news']]
In my use case this would even be enough. If you need to be more precise (e.g. to differentiate between different query parameters within the same plugin), go for
[request.getQueryParams()['tx_news_pi1'] && request.getQueryParams()['tx_news_pi1']['news'] > 0]
The solution was provided as a reply to a bug report on forge.typo3.org
Try this:
[request.getQueryParams()['tx_news_pi1']['news'] = ]
do something
[END]
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') }}
Preface: in ez4 i remember there was a tpl function to read ini settings, we used to use this to pass specific locations or id's with which we could then render certain content.
In ezplatform I am now doing the same thing but by using the PreContentViewListener (in the PreContentViewListener read a yml file and pass into the view as params), but this doesn't feel like the correct way as the PreContentViewListener doesn't always get triggered, in custom controllers for example.
Question
Is there a native way to read yaml files from within twig templates? After searching the docs and available packagists i cannot find anything :/
If your needs are simple (i.e. reading container parameters), you can also use eZ Publish config resolver component which is available in any Twig template with ezpublish.configResolver.
You can specify a siteaccess aware parameter in format <namespace>.<scope>.<param_name>, like this:
parameters:
app.default.param.name: 'Default param value'
app.eng.param.name: 'English param value'
app.cro.param.name: 'Croatian param value'
where default, eng and cro are different eZ Publish scopes.
You can then use the config resolver to fetch the parameter in current scope with:
{{ ezpublish.configResolver.parameter('param.name', 'app') }}
If you have Legacy Bridge installed, this even falls back to legacy INI settings if no Symfony container parameter exists:
{{ ezpublish.configResolver.parameter('SiteSettings.SiteName', 'site') }}
Disclaimer: Some say that using config resolver is bad practice, but for simpler usecases it is okay, IMO.
Have a look to our CjwPublishToolsBundle.
https://github.com/cjw-network/CjwPublishToolsBundle
https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php
Here we have 2 wrapper twig functions
{{cjw_config_resolver_get_parameter ( 'yamlvariablename', 'namespace default ezsettings') }}
=> ezpublish siteaccessmatching
{{cjw_config_get_parameter( 'mailer_transport' )}}
=> core symfony yaml reader without siteaccess
You could do a lot of things in eZ 4 and not always really good for your application design. ezini was able to read the configuration from the template but now in eZ Platform and by extension Symfony you need to respect more common patterns. IMO the view should not be that smart.
Then injecting variables to the view from a listener (PreContentViewListener or your own) is not a bad idea.
You can also use the Twig Globals that could allow you to do 2 global things:
inject variables (1)
inject a service (2)
Look here: https://symfony.com/doc/current/templating/global_variables.html
(2): please don't inject the service container globally it is bad
(1): I don't remember if the Twig Globals are Site Access aware, if not injecting your own service (2) to manage access to the config might be better.
And finally, I think that the use case is not a common one:
we used to use this to pass specific locations or id's with which we could then render certain content.
Most of the time it is a bad idea to pass ids coming from the configuration to render something, it is much better to organize the content structure to let you pull the location you want using the PHP API. (no id in configuration no hassle with dev, stage, preprod and prod architecture)
We are transforming PHP Application to Symfony2 Application.
Most of the pages we are completely writing new but some pages we decided to keep it as it is. i.e I want to use the same php without any major change.
In the php page we used GET['prospect_id'], GET['executive_id'] and many other arguments. Both GET and Post methods. When I view the page in Symfony1.4 there is no error or warning.
But when I view in Symfony 2 I am getting undefined index error.
How can I solve the issue?
EDIT: if GET['prospect_id'] is null there is no error in Symfony 1.4 but i'm getting undefined index notice in Symfony2. There are many variables like that. Is it necessary to define variable before use it. How to avoid this notice message.
What i want is if i am using $_GET['xxx']. symfony2 should not show any notice or error. i want to escape from that.
Use (in Symfony2) the controllers request-object, to get those params:
$this->request->get('prospect_id');
$this->request->get('executive_id');
You can also set default values, if there is no value given. Take a look at this documentation.
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.