Pre-note: I am learning Symfony from a Drupal 7 background.
I have created a custom model (although I think they are called services in Symfony from what I have read), and would like it to render an array via twig into a variable.
I found this link, and thought that this injection approach would do the trick: http://symfony.com/doc/2.8/service_container.html
Sadly, I and up with the below error:
Type error: Argument 1 passed to AppBundle\Model\Newsletter::__construct() must be an instance of AppBundle\Model\Twig_Environment, none given, called in /home/dan/working_files/symfony-playground/src/AppBundle/Controller/DefaultController.php on line 130
So, I am wondering what is an acceptable approach so I can use twig within my custom model/service/class?
If it is of use, here is some of my code for reference:
services.yml
services:
appbundle.newsletter:
class: AppBundle\Model\Newsletter
arguments: ['@twig']
src/AppBundle/Model/Newsletter.php
namespace AppBundle\Model;
class Newsletter
{
private $twig;
public function __construct(Twig_Environment $twig)
{
$this->twig = $twig;
}
}
Calling my model
$newsletter = new Newsletter();
$newsletter = $this->get('appbundle.newsletter');
– ArtOsi$newsletter = new Newsletter();
is being called fromDefaultController.php
at the moment. – retrodans$this->get('service_name');
will get you service with dependencies injected. If you calling class withnew ClassName()
then you have to manually pass twig instance to class constructor. – ArtOsi