I Want to read a pareameter from parameters.yml. the solution is to extend the Controller but I got this error :
Error: Call to a member function getParameter() on null
I know that the container is null, but i don't know how to get the container?
class Configuration extends Controller
{
public function __construct()
{
$this->tempFolderPath = sys_get_temp_dir();
$parameter = $this->container->getParameter('a');
}
}
Any solution ? Any help ?
this is not a best practice, but for me this is the only solution :( because in my code I have a static method that instantiate the hole class. so the solution will be.
public function __construct()
{
$this->tempFolderPath = sys_get_temp_dir();
global $kernel;
$this->host = $kernel->getContainer()->getParameter('ws_host');
}
In classes which extend Controller
calling $this->container->getParameter('a')
is possible because of Container
being injected into such classes. When you are in other class you need to inject parameter you want by defining your class as a service.
Your service definition will look something like:
services:
class: App\Your\FooClass
arguments:
- %a%
Note %
- it's special character to inject parameters
Then your class will look like:
class FooClass
{
protected $a;
public function __construct($a)
{
$this->a = $a;
//you have your "a" parameter value injected into constructor
}
}