Configure a symfony database connection from ini file? - symfony

On the server I need to deploy my symfony 5.2.4 application, the database configuration is defined in an ini file for which the path is set as an environment variable.
The way I have done it right now is to run composer dump-env dev then in .env.local.php, add some code to load the inifile, parse it, then construct my database url and set DATABASE_URL to that value like that:
$inifile = parse_ini_file($_SERVER["DB_INI_PATH"]);
$databaseurl = 'mysql://'.$inifile["user"].':'.$inifile["password"].'#'.$inifile["host"].':'.$inifile["port"].'/'.$inifile["db"];
But this means that I have some code in this file that can't be versioned (because it also contains my APP_SECRET value), and anytime I need to redump my env, I will need to readd that custom code.
I have not found a proper place to add this ini file decoding process in my symfony app, so I am looking for any advice on the proper way to do that, in a way that would be versionable.

You can write your doctrine config in php and you will have access to environment variables. You can add your logic in "config/packages/prod/doctrine.php". The example in the docs shows how to set doctrine.dbal.url go here and click on the php tab for the example code: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables

Related

Generated test can't read property from application.yml

Just trying hands with Spring Cloud Contract. While running the generated test on provider side and when the application context is instantiated, it is unable to read config values from application.yml. When i move the test from generated build folder to src/test/java then issue is not seen anymore.
Which implies since build folder is outside of project src/.. structure, it can't read the config.
How can i fix it?
How do you access the value from application.yml
Let's suppose in application.yml you have the following content:
example:
baseUri: https://jsonplaceholder.typicode.com
You can simply access it in your test using:
#Value("${example.baseUri}")
String exampleBaseUri;
Additionally, if you want a profile just for tests you can create a file application-test.yml where you add properties. To access it values from this file you need to add before your test class:
#ActiveProfiles("test")

How can I run Symfony 4 in production mode?

I am using the Symfony profiler. But I actually only want to make it visible in the Dev mode. But when I push the data via git on my server and open the website, I see the profiler. That does not make sense of cause, but I do not know how to remove it.
I was looking for the file called config/config_dev.yml and config/config.yml because I would think that in config/config.yml I should just set to:
web_profiler:
toolbar: false
intercept_redirects: false
But I do not have any config.yml files. Do I have to create them?
On your server, copy your .env.dist (if you have one) to .env, and set APP_ENV=prod
If you don't yet have a .env file, create one at the root of you project, and put APP_ENV=prod in it.
That being said, note that best practice is to use server level configuration in production env. Reference link : https://symfony.com/doc/current/configuration/external_parameters.html#configuring-environment-variables-in-production
EDIT (based on the comments) for your information :
.env is a file where you will mainly put your global configuration. The .dist variant is meant to be added to git, it won't be used by symfony but is useful for the developpers (including you) to have a default config file to rely on.
Basically, when they'll pull the project for the first time, they'll copy this file to .env then adjust the lines/config to their liking.
The .env must not be added to git for it will be the file that will be used by symfony. If you add it to git, each time you will push your local work then pull from your server, it will replace your server configuration with your local one.

What's the use of parameters.yml file in Symfony

I used to write my configs for Symfony project like this in config.yml file:
my_bundle:
internal_identifier: %test%
key: %someparam%
endpoint: %nobil_endpoint%
and used parameters.yml (which was ignored by git) for allowing other developers to have different values.
Bu I think using this:
my_bundle:
internal_identifier: my-identifier
key: 12345
endpoint: www.endpoint.com
still allows developers to have different values because they can use config_dev.yml which is also ignored by Git.
So my question is this: what's the purpose of parameters.yml file if config_dev.yml can be used for the same thing?
I think two things are being asked here:
Why is parameters.yml ignored be the default Symfony .gitignore file?
Why use parameters.yml instead of config_dev.yml?
Per the first question, parameters.yml is ignored by default because this file is meant to hold settings which are per-installation. For instance different developers might need different database settings. If parameters.yml wasn't ignored, your "personal" settings would be copied to every developer.
As of Symfony 2.3 you should put the needed parameters, along with default values, in a file called parameters.yml.dist. Then, when you run composer install a composer script will check for this file and create/update your local parameters.yml file, prompting you for each setting which gives you the opportunity to change the settings to be relevant to the given install.
Per the second issue, parameters are considered different than config settings. Parameters are settings which will change install to install, whereas config settings will stay the same for all installs of a particular app (although they may be different from dev to production environments.)
first, what is ignored by git is up to you, there is a .gitignore file in your repo-root
my advice to use different parameters would be:
you have different parameter.yml´s like :
parameters.yml.dev.one
parameters.yml.dev.two
parameters.yml.dev.three
and for example you are developer one then you make a symbolic link to "your" parameters.yml.dev.one like :
cd app/config; ln -s parameters.yml.dev.one parameters.yml
so now there is a parameters.yml on your machine that points on your parameters
developer two would make a symbolic link to his parameters and so on
if you are not clear about the difference between parameters and config, please check symfony book
you can do the same with your stages when you need for example another database-connection on prelive or live or whatever by using symbolic links
the advantage of this is that every developer has the parameters of every stage and developer on his machine
cheers
Being ignored by Git is the most useful, but it also prompts you for missing values when doing composer install.
If you want to have common parameters that are not ignored, you can create a parameters_common.yml and source it in config.yml (or add them directly in config.yml).
For an advanced use of config/parameters files, I suggest you check https://github.com/wemakecustom/DirectoryLoaderBundle
Because where you are in production environment, symfony needs to work in PRODuction environment and not in DEV.
Why you removed config_dev.yml from repository?

Upload Image file using Symfony2 without guessers?

I am using Symfony2 framework for one of my project. In this one, I want to upload an image. This is really easy to do so while following the Symfony2's cookbook. It works very well on my local machine. But when I put the whole application on my remote server (Planethoster.net shared hosting), it doesn't work because of the Type-Mime extension guessers. In fact, they are not enable on their servers... (phpinfo shows --disable-fileinfo)
So, basically the idea is to know if there is a solution to do the same action (uploading an image) without any extension guessers?
Thanks
What do you think of filtering the filename ? With the last three characters, you'll know the extension. Check out UploadedFile, there is a getClientOriginalName() method. What I would do is to explode it by the ., fetch the second entry of the resulting array, and then parse it to do what you want to do.
Would you like an example of code ?
To fix exception while uploading file using framework Symfony 2
Unable to guess the mime type as no guessers are available.
enable PHP extension php_fileinfo, to do this find your php.ini file and uncomment following line
; windows
extension=php_fileinfo.dll
or
; linux
extension=php_fileinfo.so

Log4j in Websphere

I recently take over a web application project using websphere and log4j running under AIX. To create a development environment, I setup all the components in windows, using eclipse to compile a WAR file and deploy it.
All is working fine except that log file is not created.
I changed the log file in log4j.properties from something like in below and and give everyone full access permission to the directory:
log4j.appender.F1.File=/abc/def/logs/admin.log
to
log4j.appender.F1.File=c:/logs/admin.log
What else can I check?
I create a simple standalone testapp which use the same log4j.properties and it can create the log file, but when the servlet deployed to websphere, it doesn't work. Please help! Thanks!
Ok, I think this article should help you. It seems that WebSphere CE uses log4j by default and controls it with a global properties file. There is a section on how to use application-specific properties files.
Here is what I try and do to troubleshoot similar issues.
Turn on log4j debugging to see where it actually picks up the file from. You need evidence of which file is picked up (so turning the debug on is a worthwhile activity) This provides you information with what log4j is trying to do to locate the configuration file.
-Dlog4j.debug=true
I would not hardcode the log4j location in the code. Instead I
would use the log4j.configuration System property and state that in
the JVM arguments. This way even I don't need to touch my code.
-Dlog4j.configuration=file:///home/manglu/log4j.properties
I would use this approach irrespective of the runtime server that I use (be it Tomcat or WAS CE or WAS)
Hope this helps
I suggest you use environment variables set on your server like this :
You must access the admin console of your server.
Under custom properties
Server_path=/abc/def/logs
In your log4j, use this : {$server_path}/log.txt
Make sure the user running the app has access to that directory.

Resources