Confused on Symfony2 YAML imports - symfony

I'm new to Symfony and a little confused by the imports key found at the top of config.yml. I am trying to import /our_stuff/admin/version.yml into Symfony's config.yml file.
This is what my config.yml file looks like:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: '/our_stuff/admin/version.yml' }
This is what I have inside my version.yml file
version:
last_recorded_software_version: '10.12.1'
But this produces the error:
FileLoaderLoadException: Cannot import resource "/our_stuff/admin/version.yml" from "/our_stuff/admin/symfony/app/config/config.yml". (There is no extension able to load the configuration for "last_recorded_software_version" (in /our_stuff/admin/version.yml).
Looked for namespace "last_recorded_software_version", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "web_profiler", "sensio_distribution")
To test, I've also moved the version.yml file into Symfony's config folder. The path is symfony_root/app/config/, but this still produces the same error.
Why does importing work for the default YAML files that are included in Symfony, but not the one's I include?
EDIT Edited for clarity
Edit 2 Here is the entirety of the /our_stuff/admin/version.yml file:
# Update this variable ONLY RIGHT BEFORE creating a new numbered release
version:
last_recorded_software_version: '10.12.1'
Edit 3 The solution:
The version.yml file needs to have a namespace of parameters in order to read them in the config.yml file
# app/config/config.yml
imports:
- { resource: 'parameters.yml' }
- { resource: '/etc/sites/mysite.com/parameters.yml' }
#/etc/sites/mysite.com/version.yml
parameters:
some_key:
some_other_key: value
some_other_key1: value
...

You can store your configuration files outside the project direcoty or in project directory and can include check this link global-configuration-files
You can include you configuration file like this:
# app/config/config.yml
imports:
- { resource: 'parameters.yml' }
- { resource: '/etc/sites/mysite.com/parameters.yml' }
Your version.yml file format should be:
parameters:
some_key:
some_other_key: value
some_other_key1: value
...

All config files in Symfony are parsed by Configuration component. Symfony application by default import only one file: config_%environment%.yml. This file has 3 predefined sections that have significant value for Symfony:
imports
Contain array of resources that will be imported by configuration component in processing. These resources may be xml, yml or even php files that will return array.
services
Contain service definitions that have very significant value for ServiceContainer that will create services from this config section.
parameters
Parameters that will have significant value for ServiceContainer that will manage all included values in parameters section of container. If you want to get parameters from service container you should define it here.
Also you can import any config file in your bundle's configuration class.
If you import your config files inside imports block or in your bundle's Configuration class you should place them in appropriate sections: parameters, services.
As was requested examples:
parameters.yml:
parameters:
param: value1
array: {key1: value2}
services.yml:
services:
class: FQCN/To/Your/Class

If you put your version.config under some bundle you have to import it like this :
- { resource: "#AAA/YourBundle/Resources/config/version.yml" }
Otherwise your first import will work perfectly :
- { resource: version.yml }

Related

JMSSerializerBundle Yaml config for custom entity class

I've got in my symfony2 project, a custom entity outside Bundles and framework.
I need set json data into this entity, but i can't apply right configuration to user yaml file.
app/config/config.yml
jms_serializer:
metadata:
auto_detection: true
directories:
CORE:
namespace_prefix: "Core\Domain\Model"
path: "%kernel.root_dir%/Resources/serializer/CORE"
app/Resources/serializer/CORE/Model.Product.yml
Core\Domain\Model\Product\Product:
properties:
id:
type: integer
objectId:
type: string
name:
type: string ...
It's possible that this bundle dont works fine with entities outside bundles?.
Always I see error message: You must define a type for Core\Domain\Model\Product\Product::$id.
I think that JMSSerializerBundle don't read yaml file, because with annotations works fine.
Any idea?.
Thanks.
In your app/config/config.yml be sure to use \\ as namespace separator instead of \:
jms_serializer:
metadata:
auto_detection: true
directories:
CORE:
namespace_prefix: "Core\\Domain\\Model"
path: "%kernel.root_dir%/Resources/serializer/CORE"
Otherwise the backslashes are treated as escape characters for the following letters.
Edit:
Also make sure to name the JMS serializer config properly. For the class Core\Domain\Model\Product\Product you need a Product.Product.yml file inside the specified path of the config. In your example, your file is named Model.Product.yml.
So to get the serializer config file name for an entitiy in general:
Strip the namespace defined in the config from the class name
replace the namespace separators \ with .
append .yml and put the file in the path folder defined in the config

Error While Updating Symfony2 Project

I'm trying to inject a Google Analytics tracking number into all my Symfony2 views so I used the instructions here http://symfony.com/doc/current/cookbook/templating/global_variables.html using this method:
# app/config/config.yml
twig:
globals:
ga_tracking: "%ga_tracking%"
And then I added my tracking number to parameters.yml
# app/config/parameters.yml
parameters:
ga_tracking: UA-xxxxx-x
And everything works perfectly but as soon as I do a composer.phar update or install I get the following message:
You have requested a non-existent parameter "ga_tracking".
And the ga_tracking line in my parameters.yml file gets erased (along with a couple other variables I've defined using the same process).
Any help would be appreciated.
The parameters.yml file is edited by Composer upon update, there's actually a comment about this at the top of the file...
# This file is auto-generated during the composer install
If you want to store additional parameters, store them elsewhere. In your config.yml, add a custom parameters file to your current imports :
imports:
- { resource: parameters.yml }
- { resource: my_parameters.yml } # Your custom file.
- { resource: security.yml }
Once you've made the edit, add your variables/parameters into a my_parameters.yml file instead. This one should be left untouched when updating. Don't forget to specify the parameters group in your custom file as well :
my_parameters.yml
parameters:
ga_tracking: "Your-tracking-code"
#ga_tracking: "%ga_tracking%"

How to use a different console configuration in Symfony

I followed the guideline on how to expose a semantic configuration for a bundle and configured it in my app/config.yml (through parameters.yml).
My bundle also contains some console commands. Right now this command either uses the dev or prod configuration, which is fine.
But how can I make the console commands use an additional configuration file that sets some things different than in config.yml?
E.g.
#app/config.yml
imports:
- { resource: parameters.yml }
foo:
view_mode: %view_mode%
and
#app/parameters.yml
parameters:
view_mode: 1
How can I make it e.g. use a different parameters.yml
#app/parameters_console.yml
parameters:
view_mode: 2
when called through the console? A new environment is not what I want here.
I think you need to create a custom environement
You just have to create a config_console.yml in your app/config folder and override the configuration you need.
imports:
- { resource: config_dev.yml }
foo:
view_mode: 2
Then in your application, just run
php app/console --env=console
This will run your application with default configuration of dev and with foo.view_mode = 2
You may want to note that it will create a new cache folder named console

symfony2 how do you create your own custom yml file to share globally?

I am looking to create my own yml file to store some global settings and vars that I can re-use across my project. I have searched on here and found another answer however it does not work for me
Set own parameters in symfony2
I'm looking to add the report_settings.yml file so that its automatically loaded across my project.
this is what I have so far based
#app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: report_settings.yml }
.. rest of config file --
and the file im trying to include looks like this
#report_settings.yml
something:
something1: "test"
It returns the following error
FileLoaderLoadException: Cannot import resource "/var/www/pcPortal/app/config/report_settings.yml" from "/var/www/pcPortal/app/config/config.yml".
and
InvalidArgumentException: There is no extension able to load the configuration for "something" (in /var/www/pcPortal/app/config/report_settings.yml). Looked for namespace "something", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_aop", "jms_di_extra", "jms_security_extra", "sj_query", "web_profiler", "sensio_distribution"
From reading the error messages it looks like I have missed something out? Do I need to add some kind of extension?
The simplest would be to put your config into the parameters section like:
#report_settings.yml
parameters:
something.something1: "test"
You could also process the config with your own extension see http://symfony.com/doc/master/book/service_container.html#importing-configuration-via-container-extensions

how to set my own paramers and config symfony2

i want to use my own parametes.ini file in my symfony project.
does anyone know about that?
I know this file
#app/config/parameters.ini
but I wnat to include my own one.
In your app/config/config.yml there are lines:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
Just add your file there.
imports:
...
- { resource: your_custom_file.yml }
Your custom file should be located in app/config directory. In case if you would like to include file from Bundle - you can import it in next way:
imports:
...
- { resource: "#YourBundle/Resources/config/your_custom_file.yml" }
The parameters.ini file should be ignored in a VCS. Instead, copy it to parameters.ini.dist and put it under a VCS control. Then, when developing, each developer copies parameters.ini.dist to parameters.ini and changes parameters to her own needs.

Resources