In this case I use a particular Symfony module, that fills JavaScript variables with PHP code.
To do so, you can create a module called "javascript" for example, which has a single action in it. The template associated to this action must be have the following name : ...Success.js.php.
In this template, you can fill your JavaScript variables like this :
var global = {
misc: {
userCulture : "<?php echo $sf_user->getCulture() ?>",
serverName : "<?php echo $_SERVER["SERVER_NAME"]?>",
},
...
}
Then, in your *appName*Configuration.class.php (located in the config folder of your app), you must add the following code :
class indexConfiguration extends sfApplicationConfiguration
{
public function configure()
{
$this->dispatcher->connect('context.load_factories', array($this, 'listenToContextLoadFactoriesEvent'));
}
public function listenToContextLoadFactoriesEvent(sfEvent $event)
{
$event->getSubject()->getResponse()->addJavascript($event->getSubject()->getRouting()->generate('javascript_variables'));
}
}
Then, you must add the following route to your routing.yml file :
javascript_variables:
url: /*moduleName*/*actionName*.:sf_format
param: { module: *moduleName*, action: *actionName*, sf_format: js }
Now, you're able to access these JavaScript variables in any of your js files, i.e. :
$(document).ready(function() {
alert(global.misc.userCulture);
});