Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

php - Symfony 4 Invalid service "my.myform.service"

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: AppControllerMyformController
    arguments:
        container: "@service_container"

src/Controller/MessageController.php:

namespace AppController;

use AppEntityProduct;
use DoctrineORMEntityManagerInterface;
use AppControllerMyformController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerAbstractController;


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 AppController;

use AppEntityMyform;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

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 AppControllerMyformController::get($service) must be compatible with SymfonyBundleFrameworkBundleControllerAbstractController::get(string $id): object in /home/admin/web/alpin52.ru/public_html/miriada/myform/src/Controller/MyformController.php on line 51 Thanks.

question from:https://stackoverflow.com/questions/65830935/symfony-4-invalid-service-my-myform-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...