how to set my own paramers and config symfony2 - symfony

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.

Related

Confused on Symfony2 YAML imports

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 }

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

Symfony2 multiple config and routing files for subdomain routing

We are building a Symfony2 application that will serve different sections using subdomains:
api.tld.com - API system
docs.tld.com - Documentation
assets.tld.com - System for serving images
How we are doing this is creating an app directory for each subdomain, and keeping the standard /app directory in place as the central shared config. There is also a custom bootstrap in the web directory for each app. Subdomains are routed accordingly using .htaccess.
The problem I am having is where multiple config files come in, particularly when they have their own routing imports. In some cases, there can be up to 4 configs.yml files. Take the following URL for example:
http://testing.docs.tld.com
The config setup currently works like this (and it works)
tld.com - Global config located at /app/config/config.yml
testing - Environment config located at /app/config/config_testing.yml. This config also imports config_dev.yml in the same directory.
docs - App config located at /app_docs/config/config.yml
These are all imported in the AppKernal in /app_docs/AppKernal.php:
// Load Global Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config.yml');
// Load Environment Configuration
// ROUTES INSIDE THIS CONFIG ARE NOT BEING LOADED
$loader->load(__DIR__.'/../app/config/config_' . $this->getEnvironment() . '.yml');
// Load App-centric Configuration
$loader->load(__DIR__.'/config/config.yml');
Now the configs load just fine. But what I'm having trouble with, and not found any definitive documentation on, is when more than one of these configs define framework: router: resources. In the above example configs, these are loaded (attempted to anyway) as follows:
/app/config/config.yml
framework:
secret:%secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: %kernel.debug%
/app/config/config_testing.yml
// No special Routing
/app/config/config_dev.yml
framework:
router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
/app_docs/config/config.yml
framework:
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: %kernel.debug%
All of the configs are loading fine. But what I found is that only the last routing file called above is being included. So I assume the rule is that they are overriden as a rule, rather than extended.
So what I have spent the last couple of days trying to find out is, is it possible to extend the inclusion of routing files within config files in the fashion above? Another option I investigated was to find a way to import routing files in the AppKernal files. I was only able to find this, which doesn't explain exactly at what point this should be used (or where). It doesn't work within the AppKernal where the configs are included, so I assume the Router is not active at that stage.
Anyone have any ideas? I'd be very grateful.
I had the same need so we did like this:
/apps/config
/apps/config/common_config.yml
/apps/config/common_routing.yml
/apps/config/...
/apps/myapp1
/apps/myapp1/myapp1Kernel.php
/apps/myapp1/...
/apps/myapp1/config
/apps/myapp1/config/config.yml
/apps/myapp1/config/routing.yml
/apps/myapp1/config/...
/apps/myapp2
/apps/myapp2/myapp1Kernel.php
/apps/myapp2/...
/apps/myapp2/config
/apps/myapp2/config/config.yml
/apps/myapp2/config/routing.yml
/apps/myapp2/config/...
...
And in each app's yml file, we had:
/apps/myapp1/config/config.yml
imports:
- { resource: "../../config/common_config.yml" }
And then, you have to reproduce the same way in /web
/web/myapp1/app.php
Who will be calling your app
$kernel = new myapp1Kernel('prod', false);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Resources