Symfony3.4控制器方法自动注入服务失败问题求助
Alright, let's tackle this Symfony 3.4 autowiring issue for your controller action — I've run into this exact pitfall before, so here's how to fix it step by step:
Symfony 3.4 doesn't treat controllers as services by default, even if you've enabled autowiring globally. That means action method parameters won't get auto-injected unless you explicitly configure your controllers to use service-based injection.
First, double-check your ContactService class:
- It should live in a standard namespace (like
AppBundle\Service\ContactService) - Make sure there are no typos in the class name or namespace
- You don't need to manually configure it in
services.yml— your_defaultssettings (autowire: true) will auto-register it as long as it's in the correct directory.
This is the key fix. Add a rule to your app/config/services.yml to auto-register all your controllers and tag them to enable action-level parameter injection:
services: _defaults: autowire: true autoconfigure: true public: true # Auto-register all controllers and enable method injection AppBundle\Controller\: resource: '../../src/AppBundle/Controller' tags: ['controller.service_arguments']
- Adjust the
resourcepath if your project structure differs (e.g., if controllers are insrc/Controllerinstead ofAppBundle/Controller, use../../src/Controller) - The
controller.service_argumentstag tells Symfony that these controllers can accept auto-injected services as action parameters.
Make sure your controller method uses the correct type hint for ContactService:
namespace AppBundle\Controller; use AppBundle\Service\ContactService; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; class ContactController extends Controller { public function indexAction(ContactService $contactService) { // Use your ContactService here return new Response('Success! ContactService injected.'); } }
- Ensure you've imported the correct
ContactServiceclass with theusestatement - The type hint must match the exact class name and namespace.
Symfony 3.4 holds onto old service configurations tightly — don't skip this:
php bin/console cache:clear --env=dev
If you don't want to auto-register all controllers, you can register just the one you need:
services: # ... keep your existing _defaults ... app.controller.contact: class: AppBundle\Controller\ContactController tags: ['controller.service_arguments'] autowire: true
Then specify the service in your route annotation:
/** * @Route("/contact", service="app.controller.contact") */ public function indexAction(ContactService $contactService) { // ... your logic ... }
- Double-check that
ContactServicehas no unresolvable dependencies (if its constructor has parameters, those must also be auto-wireable or configured as services) - Confirm the
resourcepath inservices.ymlpoints to the correct controller directory - Make sure there are no typos in the class name or namespace for both the controller and service
内容的提问来源于stack exchange,提问作者Developer

