Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
like image 858
Alexis_D Avatar asked Jul 28 '15 20:07

Alexis_D


1 Answers

You can inject the kernel.cache_dir parameter as follows:

services:
    cache_langue:
        class: MySite\BlogBundle\Services\mycache
        arguments: ["%kernel.cache_dir%"]
like image 107
Javier Eguiluz Avatar answered Nov 18 '22 04:11

Javier Eguiluz