Reputation: 2137
I have a multisite aplication with a log system depends on each site and I want that each of them has its own log file so I did that :
1) I use a dynamic variable in config.yml
with depends on $_SERVER['HTTP_HOST']
.
config.yml:
imports:
- { resource: param.php }
...
monolog:
handlers:
user:
type: stream
path: %kernel.logs_dir%/%domain_name%.%log_filename%
channels: [user]
=> %domain_name%
is my dynamic variable
2) I set it in app/config/param.php:
<?php
$url = $_SERVER['HTTP_HOST'];
$domain = preg_replace('/^www./', '', $url);
$container->setParameter('domain_name', $domain); // this set my variable `%domain_name%` with the current domain (ex: site1.com)
Problem :
This code works fine but when I switch to an other site, the value of %domain_name% remains equal to its initial value.
Exemple :
%domain_name%
value is "site1.com" so it works
fine%domain_name%
value is always "site1.com" and
not "site2.com"Why ?
Cache issue. Indeed, Symfony load in cache config.yml
so it keeps the first value of %domain_name%
To delete this value I have to execute this command: rm -rf app/cache/*
but it is not a solution.
So nobody has a solution for this problem ? Thanks
Upvotes: 1
Views: 775
Reputation: 7351
We had the same problem in our multi-site CMS environment. Symfony is primarily designed to run a single site, so the config.yml gets run once, and then cached. That means if you inject dynamic parameters into the config.yml (by any means: Globals, Constants, Apache Environment, etc), after the first run they are cached for each subsequent request :( So the next unique domain site request, will be using the cached values from the first site. In most cases, this creates problems.
Some people have suggested creating different environments for each site. But that's not practical when you host hundreds, thousands or millions of sites.
Alternative Cache Location
At the moment, the best solution we can conceive is to specify a different cache folder for each site. So you extend the app/appKernel.php
and customize the getCacheDir
function.
//app/appKernel.php
class AppKernel extends Kernel
{
public function getCacheDir()
{
return $this->rootDir.'/cache/'.$this->environment.'/'.$_SERVER['HTTP_HOST'];
}
}
Upvotes: 3
Reputation: 1
I am not quite sure, if you are using the parameter file/configuration as intended.
For me there is information missing, to answer your question directly, so here are the two possible scenarios:
1) Site www.site1.com and www.site2.com is pointing at the SAME source (same server):
In this case I wouldn't define the domain_name as an configuration value, course it is not an distinct configuration parameter it is more like a variable. Build a service or think about defining an additional environment.
2) www.site1.com and www.site2.com are two distinct projects:
In that case you shouldn't deploy the contents of the cache directory (which you should do anyway; but in this case it would be the issue ;)).
EDIT You could implement different configuration files to solve:
Controller in your /web directory:
initialize the AppKernel with the requested environment, and point to the controller in your vhost config or your htaccess file. I think you could change the app.php controller even to something like:
[...]
require_once __DIR__.'/../app/AppKernel.php';
$request = Request::createFromGlobals();
$kernel = new AppKernel($request->giveMeMyHostEnvironmentMappingShizzle(), false);
$kernel->loadClassCache();
[...]
kind regards
Upvotes: 0