When I run: php composer.phar update after adding a line to require-dev in composer.json, I observe that parameters.yml loses all the changes I made from it initial state (when Symfony2 standard edition is first installed). What are reasons behind this?
You should store your parameters in parameters.yml.dist, because parameters.yml is regenerated from the .dist file after each composer update.
The .dist file can be added to your VCS of choice and when someone pulls the changes, Symfony will check if there are any differences between parameters.yml.dist and local parameters.yml, will ask the user to provide a value for any new parameter and it will add it to local parameters.yml file.
The indeed of this behaviour is because the script want to remove outdated params.
If you need to keep outdated params you can use keep-outdated param in the configuration:
{
"extra": {
"incenteev-parameters": {
"keep-outdated": true
}
}
}
More info in the bundle's doc here
Related
i try to deploy an app in "integration" environment but after the "composer update" command, the parameters.yml file generated doesn't contains may integration parameters
Here is what i did :
I created a "config_integ.yml" file in app/config with :
# app/config/config_integ.yml
imports:
- { resource: parameters_integ.yml }
- { resource: config.yml }
framework:
profiler: { only_exceptions: false }
the parameters_integ.yml file contains parameters for integration environment. example of the beggining :
parameters:
database_host: localhost
database_port: null
database_name: test_integ
database_user: root
the parameters.yml.dist contains these parameters with default values :
parameters:
database_host: 127.0.0.1
database_port: ~
database_name: symfony
database_user: root
How can i tell to composer to take the parameters_integ.yml file to build the parameter.yml file ? Because i will have a prod environment after, i can't write my parameters in parameters.yml.dist file.
Tell me if you need other informations
Thanks for your help
1) You should include composer.json and composer.lock in your VCS
2) You should not include parameters.yml in your VCS
3) When deploying Sf application you should use composer install
http://symfony.com/doc/current/deployment.html#c-install-update-your-vendors
difference between composer install and composer update (+ composer.lock): https://blog.engineyard.com/2014/composer-its-all-about-the-lock-file
4) I can think of a few ways for creating parameters.yml in the integration environment:
you manually create parameters.yml file and place it beside parameters.dist.yml
or
if there is no parameters.yml or parameters.yml has some missing parameters, when you run composer install, you will be prompted to enter missing parameters and parameters.yml will be created/updated during the composer install process
or
you can have an app/config/parameters folder containing parameters_prod.yml and parameters_integ.yml files, and one of those files is automatically copied into parameter.yml file during deployment (most complicated solution)
If you want integration environment to be accessible via a browser, you should also create a front controller for it. Copy the web/app.php file to web/app_integ.php and edit the environment to be integration:
// web/app_integ.php
// ...
// change just this line
$kernel = new AppKernel('integ', false);
// ...
More info at https://symfony.com/doc/current/configuration/environments.html#creating-a-new-environment
And the answer to the question from the title, "How does composer know symfony environment" - I think composer has no idea about Sf environment :)
The composer has require-dev option for packages that you do not need in your production, packages required for running tests (phpunit for example), some debugging stuff during development, etc.
For an example this is how one of my composer.json files looks like:
...
"require-dev": {
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0",
"doctrine/doctrine-fixtures-bundle": "^2.3",
"nelmio/alice": "^2.1",
"deployer/deployer": "^4.0"
},
...
If I run composer install --no-dev, these packages won't be installed in the vendor directory.
But, how Sf knows when to include/omit some packages in the app.
That is defined inside AppKernel.php.
In the composer.json, under "require-dev" key I defined that I don't want doctrine fixtures in production.
So, inside AppKernel.php I put these lines of code:
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
...
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
And if I run my app in production mode, there will not be DoctrineFixturesBundle.
Best practice is to set your default parameters in your app/config/parameters.yml.dist file.
See this link:
http://symfony.com/doc/current/best_practices/configuration.html#canonical-parameters
When you run:
composer update
it will use the parameters.yml.dist and overwrite the parameters.yml with the values in the dist file.
I have created a bundle with symfony 2.3 but in this case (cause my teacher asked to me) outside src/ folder so I have ../symfony/fuentes/NameBundle instead of ../symfony/src/NameBundle. The new line appears in AppKernel and my new bundle appears on routing.yml, but when I try to launch the server
Bundle generation
Generating the bundle code: OK
Checking that the bundle is autoloaded: FAILED
Confirm automatic update of your Kernel [yes]?
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]?
Importing the bundle routing resource: OK
The command was not able to configure everything automatically.
You must do the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
I have edit autoload and tried a lot of things (looking for here) but it appears the same error always.
C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony>php app/console server:run 127.0.0.1:8080
PHP Fatal error: Class 'AulaMentor\ExdosBundle\AulaMentorExdosBundle' not found in C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony\app\AppKernel.php on line 20
Can someone help me? I'm really stuck with it.
Thanks in advance.
First of all I want to thanks to the people who has answered. Here is what we have to do if we want to create a bundle outside 'src' folder, for example in 'fuentes' > '..Symfony/fuentes'.
If you have create with 'generate:bundle' I suggest you accept when the generator ask you if you want to create the complete structure, if you have create a bundle with generator goto step 3.
1- Make sure you have registered bundle in AppKernel and It exists there a line like this:
new YourProject\NameprojectBundle\YourProjectNameprojectBundle(),
2- Make sure you have adding a route to your app/config/routing and 'routing.yml' has you new bundle route, like this (you can add a prefix to your url, in this case fuentes):
your_project_name:
resource: "#YourProjectNameprojectBundle/Resources/config/routing.yml"
prefix: /fuentes
3- We edit 'app/autoload.php' and we add this line:
$loader->add('YourProject',realpath(__DIR__.'/../fuentes'));
Finally we can update assets doing with command line:
php app/console assets:install web
Hope it helps to someone.
Best Regards.
Do exactly as said in the comment:
Edit the composer.json file and register the bundle namespace in the "autoload" section
The src folder is automatically loaded using PSR convention. If you set classes outside of the src folder, they have to be declared as well.
In your composer.json file you can add another element in the autoload section. Here's an example that I think will work for your use case:
"autoload": {
"psr-0": {
"NameBundle\\": "fuentes/",
"": "src/"
}
},
Here's how to add simply another bundle outside your Symfony project, when we don't want to mess up with composer.json file.
The 2 first points are the same as in the Michael J.'s answer. Now to the 3rd point:
Say, we need to add a OurCompany/SomeBundle residing in the other project, which relative path is ../../OtherProject/src/OurCompany/SomeBundle to CurrentProject/app dir.
So we add this bundle to the CurrentProject application this way:
$loader->add('OurCompany\\SomeBundle', realpath(__DIR__.'/../../OtherProject/src'));
PLEASE NOTICE how the slashes and backslashes should be used (the remaining / or \ at the end doesn't matter, it's smart enough to figure it out).
And for the whole namespace to be loaded (all bundles namespaces in the other project available in CurrentProject):
$loader->add('OurCompany', realpath(__DIR__.'/../../OtherProject/src'));
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!
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%"
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.