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

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!

Related

Where to put secret keys in Netlify? [duplicate]

I'm trying to set an environment variable for an API key that I don't want in my code. My source javascript looks something like this :
.get(`http://api-url-and-parameters&api-key=${process.env.API_KEY}`)
I'm using webpack and the package dotenv-webpack https://www.npmjs.com/package/dotenv-webpack to set API_KEY in a gitignored .env file and it's all running fine on my local. I'd like to also be able to set that variable when deploying through Netlify, I've tried adding it through to GUI to the 'build environment variables', and also to set it directly in the build command, but without success.
Any idea what might be the issue ?
WARNING: If this is a secret key, you will not want to expose this environment variable value in any bundle that gets returned to the client. It should only be used by your build scripts to be used to create your content during build.
Issue
dotenv-webpack expects there to be a .env file to load in your variables during the webpack build of your bundle. When the repository is checked out by Netlify, the .env does not exist because for good reason it is in .gitignore.
Solution
Store your API_KEY in the Netlify build environment variables and build the .env using a script prior to running the build command.
scripts/create-env.js
const fs = require('fs')
fs.writeFileSync('./.env', `API_KEY=${process.env.API_KEY}\n`)
Run the script as part of your build
node ./scripts/create-env.js && <your_existing_webpack_build_command>
Caveats & Recommendations
Do not use this method with a public facing repository [open] because any PR or branch deploy could create a simple script into your code to expose the API_KEY
The example script above is for simplicity so, make any script you use be able to error out with a code other than 0 so if the script fails the deploy will fail.
You can set Dotenv-webpack to load system environment variables as well as those you have declared in your .env file by doing the following:
plugins: [
new Dotenv({
systemvars: true
})
]
I.e Setting the systemvars attribute of your webpack dotenv plugin to true.
Note that system environment variables with the same name will overwrite those defined in your .env file.
Source: https://www.npmjs.com/package/dotenv-webpack#properties
if you go to corresponding site's settings in Netlify, under build&deploy you can find a section called environment variables you can easily add your environment variables from there. if you add MY_API_KEY variable to environment variables you will be able to access it inside your project via process.env.MY_API_KEY.
If you're using Nuxt JS there is a more "straight forward" approach.
Just edit the nuxt.config.js like so:
module.exports = {
env: {
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY
},
// ...
Then add the GOOGLE_API_KEY to Netlify through the build environment variables as usual.
Credit goes to yann-linn and his answer on github.
What you can also do is also to define a global constant in Webpack. Netlify environment variables defined in UI will work with it. You don't need dotenv or dotenv-webpack.
webpack.config.js
const webpack = require("webpack");
module.exports = {
plugins: [
new webpack.DefinePlugin({
"process.env.API_KEY": JSON.stringify(process.env.API_KEY)
}),
]
}
However again, of course you shouldn't do it just inputting enviornmental variables in the frontend if your API key is confidential and project public. The API key will appear in the source code of the website and will be easily accessible for everyone visiting it. Lambda function would be a better option.
You can use the Netlify's config file also ...
You can find documentation here.
Also i wanted to have the same ENV variables with with different values per branch/environment.
This workaround worked for me:
Create a netlify.toml file like:
[build]
NUXT_ENV_BASE_API = "/api"
NUXT_ENV_HOST_DOMAIN = "https://your-domain.gr"
[context.branch-deploy]
environment = { NUXT_ENV_BASE_API = "/dev-api", NUXT_ENV_HOST_DOMAIN = "https://dev.your-domain.gr" }
[context.production]
environment = { NUXT_ENV_BASE_API = "/api", NUXT_ENV_HOST_DOMAIN = "https://your-domain.gr" }
And deploy in Netlify ...

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

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

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