Changing cache_dir and log_dir with Symfony2

前端 未结 7 1727
星月不相逢
星月不相逢 2021-02-19 13:53

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:16

    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)
  • I would like to offer an alternative and that is to set environment variables to change these directories. This way it's easier to set depending on the stage. (testing, production or development)

    export SYMFONY__KERNEL__CACHE_DIR "/your/directory/cache"
    export SYMFONY__KERNEL__LOGS_DIR "/your/directory/logs"
    

    Environment variables can also be set in the virtual host with SetEnv. When reading kernel parameters symfony will look for all the $_SERVER variables that start with SYMFONY__, strip the first part and convert all the double underscores into a .

    Source code

    See line 568 to 608

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

    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:30

    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:35

    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:37

    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)
提交回复
热议问题