Instead of extending the standard controller, I’d like to inject Twig into one of my classes.
Controller:
namespace Project\SomeBundle\Controller;
use Twig_Environment as Environment;
class SomeController
{
private $twig;
public function __construct( Environment $twig )
{
$this->twig = $twig;
}
public function indexAction()
{
return $this->twig->render(
'SomeBundle::template.html.twig', array()
);
}
}
and then in services.yml I have the following:
project.controller.some:
class: Project\SomeBundle\Controller\SomeController
arguments: [ @twig ]
The error I’m getting is:
SomeController::__construct() must be an instance of Twig_Environment, none given
But I’m passing in @twig via config. I can’t see what I’m doing wrong.
Edit:
Adding in the correct code – this is what fixed the problem:
// in `routing.yml` refer to the service you defined in `services.yml`
project.controller.some
project_website_home:
pattern: /
defaults: { _controller: project.controller.some:index }
Try clearing your cache.
Is your route set up to refer to the controller as a service? If not, Symfony won’t utilize the service definition, and therefore any arguments you specify.