I have created a simple class inside my bundle in Symfony 2:
class MyTest {
public function myFunction() {
$logger = $this->get('logger');
$logger->err('testing out');
}
}
How can i access the container?
You need to inject the service container. Your class will be look this:
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyTest
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function myFunction()
{
$logger = $this->container->get('logger');
$logger->err('testing out');
}
}
Then within a controller or a ContainerAware instance:
$myinstance = new MyTest($this->container);
If you need more explanations: http://symfony.com/doc/current/book/service_container.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With