Symfony2 app/console --env throws no envirorment name - symfony

I'm working with symfony2 and while migrating from Beta2 version to the current one I messed up with some environment configuration.
My trouble here is when I run
php app/console --env
I get the following error
[InvalidArgumentException]
The file "MyWebRoute\symfony-standard\app/config/config_.yml" does not exist.
How should I get this working?
What should the proper configuration be?

That is b/c you have to specify your environment.
Available by default are "prod" and "dev".
So if you want to have console do something regarding your development environment you do
./console --env=dev [...]
The error message stems from console trying to load the appropriate configuration file, which is config_dev.yml for "dev" and config_prod.yml for "prod" and config_.yml for ""; but that one doesn't exist.

Related

Clear cache for prod. env. in Orocommerce

I am trying to make changes to my .yml files during the run time and the php bin/console cache:clear command must be executed so these changes can affect the application.
I tried running the cache:clear command with the Controller during the run time, but I got the same error every time in different methods.
Error:
"warning: ini_set (): A session is active. You cannot change the
session module's ini settings at this time at
NativeFileSessionHandler.php"
I am aware that executing the cache:clear command during the run time is not a good method as seen in the error, so are there any other ways to execute this command programmatically or to see the effect of changes in .yml files during the execution of the application without using console?
This error is discussed here: ErrorException: Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in /../../
Possible sollution would be to:
Make sure your php configuration php.ini has this configured: session.auto_start = 0.
When clearing cache for prod environment you must specify the environment since dev is the default. use --env parameter like so:
php bin/console cache:clear --env=prod

PHP Fatal error: Uncaught RuntimeException: APP_ENV environment variable is not defined

I am deploying Symfony 4 project on production.
Created .env file with a content:
APP_ENV=prod
APP_DEBUG=0
Run composer:
$ composer install --no-dev --optimize-autoloader
Getting error:
Script cache:clear returned with error code 255
!! PHP Fatal error: Uncaught RuntimeException:
APP_ENV environment variable is not defined.
You need to define environment variables for
configuration or add "symfony/dotenv" as a Composer
dependency to load variables from a .env file. in
/var/www/symfony4_project/bin/console:20
!! Stack trace:
!! #0 {main}
!! thrown in /var/www/symfony4_project/bin/console on line 20
!!
Script #auto-scripts was called via post-install-cmd
Symfony documentation about deployment and environment setting is very narrow and foggy.
It is not clear where I should set the production environments.
From https://symfony.com/doc/current/configuration.html
sentence "If you decide to set real environment variables on production, the .env files will not be loaded if Symfony detects that a real APP_ENV environment variable exists and is set to prod."
Where should I look for this real APP_ENV existence?
As I found on stackoverflow and github, there are many questions realated, but not so many answers.
Thanks for help.
you need to install symfony/dotenv composer package to load your .env file.
Take a look to the official documentation https://symfony.com/doc/current/components/dotenv.html
The Dotenv Component parses .env files to make environment variables accessible.
Run composer require symfony/dotenv this should work.
If you've got this error in prod since Flex 1.2 in 2019 and you don't want to use .env file but the environment variables of your system :
It's because of changes :
https://symfony.com/blog/new-in-symfony-flex-1-2 :
Please read Improved the handling of .env files on blog
In order :
You have to push your .env on git, but only this .env file
Developpers will create .env.local to override .env file for dev
symfony/dotenv is in require-dev section of composer.json
When you want to deploy :
Get the git repo (without /vendors, of course)
Run composer dump-env prod --empty to generate a .env.local.php file
Install your dependencies via composer install --no-dev [Other prod args]
You can now enjoy your prod env without symfony/dotenv composant installed.
I need a little bit more information to solve your problem.
First of all - just to be sure: did you add the dotenv dependency to your composer as hinted in your debug message: composer require symfony/dotenv
Also, what kind of symfony installation is it? Just a basic symfony/skeleton?
In generall if you just installed a basic symfony/sekleton or website-skeleton changing the value of APP_ENV in .env should work! But we'll work that out.
Please verify that you installed dotenv and provide the information I am looking for.

Syntax error in Doctrine?

We are working on our first site with Symfony. In development, everything works. But when we are on production, nothing works!
Trying to use app_dev.php, we see:
Parse error: syntax error, unexpected 'const' (T_CONST), expecting
variable (T_VARIABLE)" in AbstractProxyFactory.php (line 72), script
of Doctrine (in composer.json, we ask for Doctrine > 2.5)
What does that mean?
Clear the cache for the production environment.
bin/console cache:clear --env=prod
Switch to the production environment with composer during deployment.
composer install --no-dev --optimize-autoloader

Error 500 "PHP Fatal error: Uncaught RuntimeException: APP_ENV environment variable is not defined." on Symfony app start

I recently deployed a new Symfony 4 project to a prod server where I set (via ssh) both APP_ENV and APP_SECRET among others, as environment variables. Both commands printenv and set list those variables in their output so I assume everything should be fine on that part.
I get an error 500 on the site though, the log returning PHP Fatal error: Uncaught RuntimeException: APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.
What am I missing here ?
[EDIT 2020] --> use the 'symfony/dotenv'
According to the how to deploy official documentation what you can do is to move 'symfony/dotenv' in your composer file from require-dev to require so it's installed also on production environement. Then you can keep using a .env.local file.
If you don't want or can't use the 'symfony/dotenv'
For Apache and on Ubuntu first you need mod_env activated
sudo a2enmod env
sudo service apache2 restart
Then put the environement variable in the .htacess or in the vhost config file. For SF4 the .htacess is in /public and here is the syntax. I don't know if order matter, I would say no, so I added them on the top of the file.
SetEnv APP_ENV prod
SetEnv APP_SECRET fjsdkfj...
SetEnv DATABASE_URL mysql://...
Now some random useful tricks :
You don't want to commit the .htaccess file. Add it to the shared part of your deployment tool. In my case (easy-deploy-bundle)
->sharedFilesAndDirs(['public/.htaccess'])
You'll need to copy the environment variables in /etc/environment so it's available by composer ... this one is a real pain in the ass, because now you duplicated your variables. So the Symfony team said env variable are betters because safer and standard. But duplicate the information is not safer at all. Maybe I'm doing it wrong (which is completly possible). If you know how to avoid this please share.
The printenv command will show you if it's correctly configured.
If you use easy-deploy-bundle for production remove the --quiet option on composer so you have a clear error message if it fail (because it doesn't find the env variable for example). Also check that --no-dev option is here, so it won't install dotenv package.
->composerInstallFlags('--prefer-dist --no-interaction --no-dev')
System variables won’t be available to your web server. The variable should be defined in the vhost/htaccess file. I assume you are using Apache.
if you are using nginx have to defined at the end of pool configuration file.
php5
/etc/php5/fpm/pool.d/www.conf
php7
/etc/php/7.1/fpm/pool.d/www.conf

Troubleshooting "There is no extension able to load the configuration for 'knp_menu'"

I am running Symfony 2.0.6, and I get the following error:
InvalidArgumentException: There is no extension able to load the
configuration for "knp_menu" (in
/home/www/Symfony/app/config/config.yml). Looked for namespace
"knp_menu"
It works in DEV, but not in PROD enviroment.
The error message means that you got an entry "knp_menu" in your config.yml which is not used by any extension.
It's either a caching problem (you introduced the extension, e.g. a bundle, and then not cleared the prod cache), or you load a specific bundle only within the dev environment (as the profiler in a default symfony2 app).
Try clearing the cache using php app/console clear:cache --env=prod --no-debug. If the problem exists anyhow, find out which bundle uses knp_menu and either delete the configuration or enable the bundle.

Resources