Changing cache_dir and log_dir with Symfony2

前端 未结 7 1996
感动是毒
感动是毒 2021-02-19 13:50

Because of deployment constraints, I would like to have the log and cache directories used by my Symfony2 application somewhere under /var/... in my file system. For this reason

相关标签:
7条回答
  • 2021-02-19 14:15

    i think the easiest way is to link the folder to another place. We have made this on the prod server but when you develop local perhaps on windows its a bit complicated to set the symlinks.

    ln -s /var/cache/ /var/www/project/app/cache
    

    something like this.

    0 讨论(0)
  • 2021-02-19 14:20

    I used the configuration solution from Dragnic but I put the paths in the parameters.yml file because this file is ignored by git. in other words, it's not synchronized from my PC to the git repository so there is no impact in the prod environment.

    # app/config/parameters.yml
    parameters:
        database_driver: pdo_mysql
        [...]
        kernel.cache_dir: "T:/project/cache"
        kernel.logs_dir: "T:/project/logs"
    

    Configuration: Windows7, WAMP 2.4 and Symfony 2.3.20.

    But you have to know that:

    Overwriting the kernel.cache_dir parameter from your config file is a very bad idea, and not a supported way to change the cache folder in Symfony.

    It breaks things because you would now have different cache folders for the kernel Kernel::getCacheDir() and for the parameter.

    Source: https://github.com/symfony/AsseticBundle/issues/370

    So you should use it only in dev environment and if you don't want to change the content of the app/AppKernel.php file, otherwise see the other answers.

    0 讨论(0)
  • 2021-02-19 14:24

    No accepted answer, and a really old question, but I found it with google, so I post here a more recent way to change the cache directory, and the logs directory, (source here)

    • remember, short syntax for arrays require php 5.4
    • you can select the env to modify, and manage different cache and logs directories if you want

      public function getCacheDir()
      {
          if (in_array($this->environment, ['prod', 'test'])) {
          return '/tmp/cache/' .  $this->environment;
      }
      
      return parent::getCacheDir();
      }
      
      public function getLogDir()
      {
          if (in_array($this->environment, ['prod', 'test'])) {
          return '/var/log/symfony/logs';
      }
      
      return parent::getLogDir();
      }
      
    0 讨论(0)
  • 2021-02-19 14:30

    In symfony you can override the cache (and logs) directory by extending the method in AppKernel.

    // app/appKernel.php
    class AppKernel extends Kernel
    {
        // ...
    
        public function getCacheDir()
        {
            return $this->rootDir.'/'.$this->environment.'/cache';
        }
    }
    

    Check out http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html#override-cache-dir

    0 讨论(0)
  • 2021-02-19 14:30

    I was happy to find your post, but I was a little bit confused of the unhelping answers.

    I got the same problem and found out that the logs are depending on the config parameter kernel.logs_dir.

    So I just added it to my config.yml parameters:

    kernel.logs_dir: /var/log/symfonyLogs
    

    I hope it will helpfull for you even, if its a late answer.

    0 讨论(0)
  • 2021-02-19 14:32

    Add the following methods to app/AppKernel.php (AppKernel extends Kernel) making them return your preferred paths:

    public function getCacheDir()
    {
        return $this->rootDir . '/my_cache/' . $this->environment;
    }
    
    public function getLogDir()
    {
        return $this->rootDir . '/my_logs';
    }
    
    0 讨论(0)
提交回复
热议问题