Alexis_D
Alexis_D

Reputation: 1958

how to get cache directory from a service: $this->container->getParameter('kernel.cache_dir')

So far I was getting the cache directory from some controller. but as I want to set it in a specific service I would like to know which dependancy injection I should do to access it from a service.

Of course I could inject the container (as I settled here below as an exemple) but I guess there is some more spécific dependancy injection that I could use.

Here my code so far in my service

class mycache
{
   private $container;
   public function __construct($container){
      $this->container = $container;
   }
   public function transf($text, $code)
   {
    $filename = $this->container->getParameter('kernel.cache_dir') . '/MyCACHE/langue.txt';
   }
}

// service configuration

service    
cache_langue:
    class: MySite\BlogBundle\Services\mycache
    arguments: ["@service_container"]

Upvotes: 16

Views: 10758

Answers (1)

Javier Eguiluz
Javier Eguiluz

Reputation: 4107

You can inject the kernel.cache_dir parameter as follows:

services:
    cache_langue:
        class: MySite\BlogBundle\Services\mycache
        arguments: ["%kernel.cache_dir%"]

Upvotes: 28

Related Questions