0
votes

I am new to Symfony. I try to make a service from one of my classes. When I run bin/console cache:clear I get this error:

In ResolveNamedArgumentsPass.php line 66:

Invalid service "my.myform.service": did you forget to add the "$" prefix to argument "container"?

Here is my code:

config/services.yaml:

my.myform.service:
    class: App\Controller\MyformController
    arguments:
        container: "@service_container"

src/Controller/MessageController.php:

namespace App\Controller;

use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\MyformController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;


class MessageController extends AbstractController
{
    /**
     * @Route("/message", name="message", methods="GET")
     */
    public function index(Request $request): Response
    {

            //$myform = new MyformController();
            //$myform->createMyform();
             $this->get("my.myform.service")->createMyform();
...

src/Controller/MyformController.php:

namespace App\Controller;

use App\Entity\Myform;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MyformController extends AbstractController
{
           public function __construct($container )
           {
                   $this->container = $container;
           }

/*    protected  function get($service)
    {
        return $this->container->get($service);
    }
 */
    public function createMyform(): Response
    {
        // you can fetch the EntityManager via $this->getDoctrine()

What's wrong? PS I commented function get() in MyformController because I had an error: PHP Fatal error: Declaration of App\Controller\MyformController::get($service) must be compatible with Symfony\Bundle\FrameworkBundle\Controller\AbstractController::get(string $id): object in /home/admin/web/alpin52.ru/public_html/miriada/myform/src/Controller/MyformController.php on line 51 Thanks.

1

1 Answers

0
votes

why do you use dedicated method for getting the service? Use the dependency injection, framework let you define the service as argument of your action method

public function index(Request $request, YourServiceClass $serviceClass): Response
{

      //$myform = new MyformController();
      //$myform->createMyform();
      $serviceClass->doSomething();
}