Simple way to expose some symfony2 container parameters to javascript
$ php composer require fnash/js-param-bundle
<?php
// AppKernel.php
$bundles = [
// ...
new Fnash\JsParamBundle\FnashJsParamBundle(),
// ...
];
# app/routing.yml
fnash_js_param_routing:
resource: "@FnashJsParamBundle/Resources/config/routing.yml"
Given your parameters file
# app/parameters.yml
parameters:
param1: value1
param2: value2
param3: value3
param4: value4
Add some parameters you want to expose in your javascript
# app/config.yml
fnash_js_param:
expose: [param1, param2]
Be careful! Do not expose critical infos such as passwords etc..
{% block javascripts %}
.............
<script src="{{ path('fnash_js_param_js') }}"></script>
.............
{% endblock %}
</body>
</html>
alert(window.FnashJsParam.param1); // value1
alert(window.FnashJsParam.param2); // value2