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%"
Related
I'm new to Symfony and a little confused by the imports key found at the top of config.yml. I am trying to import /our_stuff/admin/version.yml into Symfony's config.yml file.
This is what my config.yml file looks like:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: '/our_stuff/admin/version.yml' }
This is what I have inside my version.yml file
version:
last_recorded_software_version: '10.12.1'
But this produces the error:
FileLoaderLoadException: Cannot import resource "/our_stuff/admin/version.yml" from "/our_stuff/admin/symfony/app/config/config.yml". (There is no extension able to load the configuration for "last_recorded_software_version" (in /our_stuff/admin/version.yml).
Looked for namespace "last_recorded_software_version", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "web_profiler", "sensio_distribution")
To test, I've also moved the version.yml file into Symfony's config folder. The path is symfony_root/app/config/, but this still produces the same error.
Why does importing work for the default YAML files that are included in Symfony, but not the one's I include?
EDIT Edited for clarity
Edit 2 Here is the entirety of the /our_stuff/admin/version.yml file:
# Update this variable ONLY RIGHT BEFORE creating a new numbered release
version:
last_recorded_software_version: '10.12.1'
Edit 3 The solution:
The version.yml file needs to have a namespace of parameters in order to read them in the config.yml file
# app/config/config.yml
imports:
- { resource: 'parameters.yml' }
- { resource: '/etc/sites/mysite.com/parameters.yml' }
#/etc/sites/mysite.com/version.yml
parameters:
some_key:
some_other_key: value
some_other_key1: value
...
You can store your configuration files outside the project direcoty or in project directory and can include check this link global-configuration-files
You can include you configuration file like this:
# app/config/config.yml
imports:
- { resource: 'parameters.yml' }
- { resource: '/etc/sites/mysite.com/parameters.yml' }
Your version.yml file format should be:
parameters:
some_key:
some_other_key: value
some_other_key1: value
...
All config files in Symfony are parsed by Configuration component. Symfony application by default import only one file: config_%environment%.yml. This file has 3 predefined sections that have significant value for Symfony:
imports
Contain array of resources that will be imported by configuration component in processing. These resources may be xml, yml or even php files that will return array.
services
Contain service definitions that have very significant value for ServiceContainer that will create services from this config section.
parameters
Parameters that will have significant value for ServiceContainer that will manage all included values in parameters section of container. If you want to get parameters from service container you should define it here.
Also you can import any config file in your bundle's configuration class.
If you import your config files inside imports block or in your bundle's Configuration class you should place them in appropriate sections: parameters, services.
As was requested examples:
parameters.yml:
parameters:
param: value1
array: {key1: value2}
services.yml:
services:
class: FQCN/To/Your/Class
If you put your version.config under some bundle you have to import it like this :
- { resource: "#AAA/YourBundle/Resources/config/version.yml" }
Otherwise your first import will work perfectly :
- { resource: version.yml }
I am trying to integarte IvoryCKEditor with sonata media bundle. Perpose is to allow image uploads in ckeditor. I tried it using a CoopTilleulsCKEditorSonataMediaBundle but i keep getting an error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_sonata_media_media_ckeditor_browser" as such route does not exist.") in "IvoryCKEditorBundle:Form:ckeditor_widget.html.twig".
Thanks :)
You should check your routes with the command:
$ php app/console debug:router | grep ckeditor
Then you have to replace admin_sonata_media_media_ckeditor_browser and admin_sonata_media_media_ckeditor_upload by routes founded with this command, in the config of ivory_ck_editor (maybe in your config.yml or your ivory_ckeditor.yml file)
If you don't have route for ckeditor, I think you should check if you install correctly the bundle.
1) install SonataFormatterBundle 2) add the bundle and its dependencies in AppKernel 3) config files 4) CLEAR CACHE 5) roll
Reason of the issue
This problem arises when trying to integrate CKEditor through the
SonataFormatterBundle without using the SonataAdminBundle.
In fact, the integration proposed by the SonataFormatterBundle is meant to only work for the SonataAdminBundle, and no easy integration for a custom admin bundle is currently available.
Note that this dependency is not specified in the documentation at the moment.
How to solve the problem
Simply install the SonataAdminBundle following this installation process. You do need to configure the bundle entirely as specified in the documentation. To add the routes that were missing, such as admin_sonata_media_media_ckeditor_browser, simply add the following to your config/routes.yml:
# This is your custom admin bundle
admin:
resource: "#AdminBundle/Controller/"
type: annotation
prefix: /admin/
# Import SonataAdminBundle routes
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin/sonata # put whatever prefix here
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
Just to add something.
If you are SURE that you have installed, all dependencies, and set all configs correctly, and still getting this error, then the cause may be that you have defined your 'sonata_media' prefix in routing.yml differently.
You can check all urls in your project in:
app/cache/dev/appDevUrlGenerator.php
In there you will find ALL routes in a variable $declaredRoutes
Afterwards simply put correct route name for browsing.
It will not work without SonataMediaBundle - you will need to install it with SonataFormatterBundle
Sonata become too complicated as for me, so I just use IvoryCKEditorBundle directly
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
After executing the following command for CRUD generation:
php app/console generate:doctrine:crud --entity=AcmeDemoBundle:Users --format=yml
i get error for automatic routing update by CRUD for each entity.
Confirm automatic update of the Routing [yes]? yes
Importing the CRUD routes: FAILED
The command was not able to configure everything automatically.
You must do the following changes manually.
- Import the bundle's routing resource in the bundle routing file
(C:\wamp\www\symfony\src\Acme\DemoBundle/Resources/config/routing.yml).
AcmeDemoBundle_categories:
resource: "#AcmeDemoBundle/Resources/config/routing/categories.yml"
prefix: /categories
I also tried creating a new bundle but still gets same error. So everytime i add the above code in routing file /src/Acme/DemoBundle/Resources/config/routing.yml
Can someone please suggest what i am missing?
I get the same thing, not sure how to get the generation to work right but it wants you to add that code to your main routing.yml file so it can link the generated routes:
AcmeDemoBundle_categories:
resource: "#AcmeDemoBundle/Resources/config/routing/categories.yml"
prefix: /categories
i want to use my own parametes.ini file in my symfony project.
does anyone know about that?
I know this file
#app/config/parameters.ini
but I wnat to include my own one.
In your app/config/config.yml there are lines:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
Just add your file there.
imports:
...
- { resource: your_custom_file.yml }
Your custom file should be located in app/config directory. In case if you would like to include file from Bundle - you can import it in next way:
imports:
...
- { resource: "#YourBundle/Resources/config/your_custom_file.yml" }
The parameters.ini file should be ignored in a VCS. Instead, copy it to parameters.ini.dist and put it under a VCS control. Then, when developing, each developer copies parameters.ini.dist to parameters.ini and changes parameters to her own needs.