How to use a different console configuration in Symfony - 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

Related

Symfony Bundle references "#Bundle" - FileLocatorFileNotFoundException

I am currently building my own Symfony bundle (I am using version 3.3). It works fine so far, but now I wanted to move the bundle-specific service definition out of my app/config/services.yml to a service definition within the bundle.
I created a src/MyBundle/Resources/config/services.yml and followed the guide How to Load Service Configuration inside a Bundle to load it. That works just fine, but I thought that the paths inside the newly created services.yml look a bit ugly:
MyBundle\:
resource: '../../*'
exclude: '../../{Tests}'
So I thought it would look a little cleaner, if I use the #Bundle-notation instead:
MyBundle\:
resource: '#MyBundle/*'
exclude: '#MyBundle/{Tests}'
However, than I was facing the error:
FileLocatorFileNotFoundException
The file "#MyBundle" does not exist (in: <...>\MyBundle\DependencyInjection/../Resources/config), where <...> corresponds to an absolute path.
I also tested whether it was a naming issue of the bundle name, but when I have a wrongly named bundle reference in the app/config/services.yml, e.g. #SomeBundleThatDoesNotExist then I get a different error:
FileLoaderLoadException
Bundle "SomeBundleThatDoesNotExist" does not exist or it is not enabled.
So my question is: Why does the #Bundle-notation work fine in the app/config/services.yml but not in the src/MyBundle/Resources/config/services.yml?
If you use the service remember make public for external use:
autowire: true
public: true
But, if your symfony dont know where is the bundle please chec this answer:
Symfony generated Bundle doesn't work

symfony: How to set configuration parameters files for different environments?

How to setup a different configuration parameters file for each environment?
At the moment parameters in parameters.yml are used in both dev and prod environment, but I need different parameters in order to deploy my app in prod.
You can put all the parameters used in your dev environment in a app\config\parameters_dev.yml file (you need to create it) and then import it in your app\config\config_dev.yml:
imports:
- { resource: config.yml }
- { resource: parameters_dev.yml }
So when you work in local any parameter used in production will be overwritten by the new file with the right parameters.
And remember to Clear the cache!

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%"

Symfony 2 how to create a parameters_dev.yml?

i just started coding with Symfony 2. On my local machine i setup my database using app/console doctrine:database:create and doctrine:schema:create which works great.
The problem i have is, local i have diffrent parameters defined in parameters.yml than on my production environment. How can i define paramters for dev and for prod? I've already tried to create a parameters_dev.yml but this did not work.
Should i maybe create a paramters.yml on my prod server and copy it after deployment so i dont have my DB password in version control?
parameters.yml should't be handled by version control.
You have to set it on ignore in .gitignore.
Only parameters.yml.dist has to be versioned
more over if you use symfony 2.3 and install vendors using composer on prod you will be prompt to enter all the setting from parameters.yml.dist so parameters.yml will be generated automatically
In Symfony 2.7 I had to add this to appKernel.php:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/parameters_'.$this->getEnvironment().'.yml');
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
And then create both parameters_dev.yml and parameters_prod.yml.
Don’t forget to add this:
imports:
- { resource: parameters.yml }
on the first line of each new ymls.

How to access custom config value in the application?

I placed some custom configs in my config_prod.yml file like this:
store:
plugins:
installed: [abc]
Since SF2 will load and compile and cache, the config_prod.yml is actually loaded just once only. I wonder (without the need to do some custom coding to use setParameter to set my custom config values to the container, or to use my own method of caching the custom configs), how do I access these custom config values? Are they automatically cached and made available somehow?
Everything you define in the parameters section will be accessible as a parameter:
parameters:
store:
plugins:
installed: [abc]
You can access your configuration by calling getParameter() on the container:
$store = $container->getParameter('store');
If you want to have your own section in the configuration file:
store:
plugins:
installed: [abc]
you'll have to create an extension and expose a semantic configuration of your bundle.
In both cases container with all its configuration is compiled and stored in cache.
Read more:
Service Container Parameters
How to expose a Semantic Configuration for a bundle

Resources