您的想法的问题是,在构造 ConfigCache 对象后立即初始化服务容器和参数,并使用绝对缓存路径作为参数。
namespace Symfony\Component\HttpKernel;
...
/**
* The Kernel is the heart of the Symfony system.
*
* It manages an environment made of bundles.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Kernel implements KernelInterface, TerminableInterface
{
...
/**
* Initializes the service container.
*
* The cached version of the service container is used when fresh, otherwise the
* container is built.
*/
protected function initializeContainer()
{
$class = $this->getContainerClass();
// !!!!!! cache config object construction
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
$fresh = true;
if (!$cache->isFresh()) {
$container = $this->buildContainer();
$container->compile();
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
$fresh = false;
}
require_once $cache->getPath();
$this->container = new $class();
$this->container->set('kernel', $this);
if (!$fresh && $this->container->has('cache_warmer')) {
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
}
}
}
因此,您无法在 getCacheDir() 方法中访问自定义参数。
你能覆盖 getCacheDir() 方法吗?
假设您的目录结构如下所示
-home
--symfony_app
--custom_cache_directory
比方法覆盖看起来像这样:
public function getCacheDir()
{
return dirname(__DIR__).'/../custom_cache_directory/cache/'.$this->getEnvironment();
}
官方文档中的更多信息:http: //symfony.com/doc/current/configuration/override_dir_structure.html#override-the-cache-directory