Symfony 5集成Payum报错:'no container set'求助
I've run into this exact issue while setting up Payum in Symfony 5, so I know how frustrating it can be. The root problem is that Symfony 5+ doesn't automatically treat controllers as services by default, and Payum's CaptureController relies on the container to fetch its dependencies. Here are the most reliable fixes:
1. Register the CaptureController as a Service Subscriber
Edit your config/services.yaml file and add this configuration to explicitly tell Symfony that the Payum controller needs access to the container:
Payum\Bundle\PayumBundle\Controller\CaptureController: tags: ['container.service_subscriber'] public: true
- The
container.service_subscribertag tells Symfony to inject the container into the controller. - Setting
public: trueis necessary because the route directly references the controller class, so Symfony needs to access it as a public service.
2. Use Payum's Pre-Registered Service ID in Your Route
PayumBundle actually registers its controllers as services behind the scenes. If you're using a custom route, make sure you reference the service ID instead of the controller class directly. For example:
# config/routes.yaml payment_capture: path: /payment/capture/{token} controller: 'payum.capture_controller:doAction'
If you were using the fully qualified class name before (like Payum\Bundle\PayumBundle\Controller\CaptureController::doAction), switching to the service ID payum.capture_controller will ensure Symfony uses the container-managed instance of the controller, which already has the container injected.
3. Clear Your Symfony Cache
After making any configuration changes, always clear the cache to ensure Symfony picks up the new settings:
# For dev environment php bin/console cache:clear # For prod environment php bin/console cache:clear --env=prod
4. Verify PayumBundle Version Compatibility
Double-check that your PayumBundle version is compatible with Symfony 5. PayumBundle 2.5 and above supports Symfony 5. If you're on an older version, update it via Composer:
composer require payum/payum-bundle:^2.5
Most of the time, either step 1 or step 2 will resolve the issue immediately. I'd recommend trying step 2 first since it uses Payum's built-in service configuration, which is the intended way to use their controllers.
内容的提问来源于stack exchange,提问作者lorenzo89

