1
votes

Having an issue with injecting services in services following: https://symfony.com/doc/current/service_container.html#injecting-services-config-into-a-service

This code:

namespace ServicesBundle\Services;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;

class Services
{

private $email;

public function __construct($email, $message, SessionInterface $sessInt) {

    $this->email = $email;
    $this->message = $message;
    $this->sessInt = $sessInt;

}

public function DisplayMessage(){


    return "This is the Services Class in the Services namespace. Email the administrator @ " . $this->email . " | " . $this->message;

}

public function sessionIdGetAction(){
    $hello = $this->sessInt->getID();
    return $hello;
}
}

Throws this error:

Type error: Argument 3 passed to ServicesBundle\Services\Services::__construct() must implement interface Symfony\Component\HttpFoundation\Session\SessionInterface, none given, called in /home/admin-daniel/symfony-test-sites/july262017/var/cache/dev/appDevDebugProjectContainer.php on line 412

When I call any one of these functions from my controller.

I am lost with this. Seems I following what the guide says but not working....

I am missing something somewhere...

3

3 Answers

2
votes

If you need to inject Session

service:
   my.service.name:
      class: ..\MyClassName
      arguments: 
        session: "@session"
0
votes

It says that in /home/admin-daniel/symfony-test-sites/july262017/var/cache/dev/appDevDebugProjectContainer.php on line 412 you need to pass object with type of SessionInterface as third parameter. And you did not do this.

You need to add private property $sessInt to your Services class. Because in constructor there is $this->sessInt = $sessInt;

It would be dependency injection.

0
votes

You are missing the create method:

public static function create(ContainerInterface $container) {
        return new static(
          $container->get('yourvariable')
        );
    }