question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service in action not autowired

See original GitHub issue

Hello,

I got an issue with an action using a service in __construct

  "@context": "/app_dev.php/contexts/Error",
  "@type": "hydra:Error",
  "hydra:title": "An error occurred",
  "hydra:description": "Type error: Argument 1 passed to AppBundle\\Action\\MailSpecial::__construct() must be an instance of AppBundle\\Services\\SWServices, none given, called in /var/www/projet/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php on line 202",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/var/www/projet/src/AppBundle/Action/MailSpecial.php",
      "line": 17,
      "args": []
    },

Here is my action :

<?php

// src/AppBundle/Action/MailSpecial.php

namespace AppBundle\Action;

use AppBundle\Entity\Mail;
use AppBundle\Services\SWServices;
use Doctrine\Common\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Routing\Annotation\Route;

class MailSpecial
{
    private $sWServices;

    public function __construct(SWServices $sWServices)
    {
        $this->sWServices = $sWServices;
    }

    /**
     * @Route(
     *     name="mail_special",
     *     path="/mails/special",
     *     defaults={"_api_resource_class"=Mail::class, "_api_collection_operation_name"="special"}
     * )
     * @Method("GET")
     */
    public function __invoke($data) // API Platform retrieves the PHP entity using the data provider then (for POST and
        // PUT method) deserializes user data in it. Then passes it to the action. Here $data
        // is an instance of Book having the given ID. By convention, the action's parameter
        // must be called $data.
    {
        $em = $this->sWServices->getEm();
        $mail = $em->find('AppBundle:Mail',7);

        return $mail; // API Platform will automatically validate, persist (if you use Doctrine) and serialize an entity
        // for you. If you prefer to do it yourself, return an instance of Symfony\Component\HttpFoundation\Response
    }
}

My service declaration :

    SWServices:
        class: AppBundle\Services\SWServices
        arguments: [ "@doctrine.orm.entity_manager" , "@service_container" ]

My service :

<?php
namespace AppBundle\Services;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\DependencyInjection\Container;

class SWServices{
    protected $em;
    private $container;

    public function __construct(EntityManager $entityManager, Container $container)
    {
        $this->em = $entityManager;
        $this->container = $container;
    }

    /**
     * Some method
     * @param string $somevar
     * @return mixed
     */
    public function someMethod($somevar) {
        return $somevar;
    }
}

Is there something that i’ve missed ? The service in action constructor should been autowired.

Thanks in advance

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
soyukacommented, Apr 18, 2017
SWServices:
    class: AppBundle\Services\SWServices
    arguments: [ "@doctrine.orm.entity_manager" , "@service_container" ]
MailSpecial:
    class: AppBundle\Action\MailSpecial
    autowire: true

Or

SWServices:
    class: AppBundle\Services\SWServices
    autowire: true
MailSpecial:
    class: AppBundle\Action\MailSpecial
    autowire: true

Should work, it does to me at least 😐.

0reactions
dunglascommented, Oct 2, 2017

Fixed in 2.1 and in docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

autowired annotation for service class is not working in ...
1 Answer 1 ... It is null because this @Configuration class also defines a BeanDefinitionRegistryPostProcessor that forces the context to create ...
Read more >
Java: How to fix Spring @Autowired annotation not working ...
The class in which you want to use @Autowired does not have a Spring annotation. Add one of the following annotatons to the...
Read more >
How to fix @Autowired - No qualifying bean of type found for ...
1. Annotation-based configuration - Using @Service or @Component annotations, scope details can be provided with @Scope annotation.
Read more >
Spring @Autowired Annotation - GeeksforGeeks
The @Autowired annotation marks a Constructor, Setter method, Properties and Config() method as to be autowired that is 'injecting ...
Read more >
Defining Services Dependencies Automatically (Autowiring)
Autowiring subsystem can not decide which one to use. Remember, autowiring isn't magic; it looks for a service whose id matches the type-hint....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found