How to access arrays in symfony's configuration files? - symfony

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.

Related

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') }}

symfony services.yml difference between/utility of name and resource

I'm new to symfony, I noticed that in services.yml we usually define each entry by a name (for example App\Controller) and then add the path to the file/folder in the resource attribute, for example :
App\Controller :
resources: ../../Controllers/*
isn't this some kind of repetitive ? what is the use of the name we give at the beginning ?
Thank you

Symfony2 : Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given

I'm trying to implement OAuth2 in my actual symfony2 project using the FOSOAuthServerBundle.
I've been following this Article to implement it.
Since i don't use FOS User bundle i had to create a UserProvider.
I'm also using A User Repository as he did in his Article.
I've been stuck with this error :
ContextErrorException: Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in /app/cache/dev/appDevDebugProjectContainer.php on line 3527 and defined in /app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php line 67
I've followed the given code for services.yml:
And converted it to Yaml, here my file (services.yml) :
imports:
- { resource: parameters.yml }
parameters:
# wca_transition.example.class: WCA\TransitionBundle\Example
collection.import.from_email: "%import_from_email%"
collection.import.to_email: "%import_to_email%"
platform.entity.user.class: WCA\ServerBundle\Entity\Profile
platform.user.provider.class: WCA\ServerBundle\Provider\ProfileProvider
services:
# wca_betosee_transition.example:
# class: %wca_transition.example.class%
# arguments: [#service_id, "plain_value", %parameter%]
collectionImportCommand:
class: WCA\ServerBundle\Command\CollectionImporterCommand
calls:
- [setContainer, ["#service_container"] ]
platform.user.manager:
class: Doctrine\ORM\EntityManager
factory-service: doctrine
factory-method: getManagerForClass
arguments: ["%platform.entity.user.class%"]
platform.user.repository:
class: WCA\ServerBundle\Model\ProfileRepository
factory-service: platform.user.manager
factory-method: getRepository
arguments: ["%platform.entity.user.class%"]
platform.user.provider:
class: "%platform.user.provider.class%"
# arguments: ["#platform.user.repository"]
platform.grant_type.api_key:
class: WCA\ServerBundle\OAuth\ApiKeyGrantExtension
arguments: ["#platform.user.repository"]
tags:
- { name: fos_oauth_server.grant_extension, uri: http://mywebsite.local/grants/api_key }
I suppose the given error is coming from my configuration but i don't see what could be wrong since i followed what was given. Either the services created don't get the given arguments ("none given") or i don't pass the correct ones.
Any ideas or hints?
PS : If you need any other files, don't hesitate to ask, i'll update the post.
I doubt if it will help but try changing factory-service,factory-method to factory_service and factory_method to match the Symfony documentation.
Sounds like it is the only article that describes very well how to implement FOS OAuth2 bundle, even better than their own documentation.
But for some reason, services are defined in xml which I find very weird and ugly.
Anyway, as #Cerad mentioned, if you translate the xml service to the yaml format, then you'll have to replace the - by _ and it will work fine.

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.

Set the default propel connection for entire php process

Propel 1.6 and Symfony 1.4
I'm looking for a way to programmatically set the default propel connection for the length of an entire php process. The issue is that I'm using an alternative db for testing purposes and I have a good deal of code that doesn't pass the PropelPDO object currently.
Can this be done? Any tips? Thanks.
Why not use environments in your databases.yml?
dev:
propel:
class: sfPropelDatabase
param:
classname: DebugPDO
etc, etc
stage:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
etc, etc
prod:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
etc, etc
So, the solution to this was to use the following, pretty clean and sweet:
//override the "default" "propel" dsn and set it to our testing db!
\Propel::setConnection(
"propel",
Propel::getConnection(SqliteSetup::$databaseName)
);

Resources