Symfony2.8更新依赖后FOS User Bundle报has()非对象调用错误
Hey, let's work through this error you're hitting on your login page after updating dependencies. That error at vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 184 boils down to one key issue: the controller's container object is null. The line in question tries to call $this->container->has(), and when the container isn't properly injected, you get that non-object error.
Given your composer config and the fact this started after an update, the most likely culprit is a compatibility shift with FOSUserBundle, but let's walk through all the fixes step by step:
1. Lock FOSUserBundle to a Stable Symfony 2.8-Compatible Version
Your composer.json uses friendsofsymfony/user-bundle": "~2.0@dev"—development branches are great for testing, but they can introduce breaking changes that don't mesh with older Symfony versions like 2.8. Let's switch to a stable, verified compatible version:
Update your composer.json's require section to:
"friendsofsymfony/user-bundle": "2.0.1"
Then run this command to update just that bundle and its dependencies safely:
composer update friendsofsymfony/user-bundle --with-dependencies
2. Double-Check Your Custom Login Controller's Inheritance
If you've got a custom login controller, make sure it's properly extending Symfony's base Controller class and isn't overriding the container injection logic accidentally. Here's the correct setup:
use Symfony\Bundle\FrameworkBundle\Controller\Controller; class SecurityController extends Controller { // Your login action and other methods here }
If you've switched to constructor injection for dependencies, make sure you're either injecting the container directly (not recommended long-term, but quick for testing) or injecting the specific services you need instead of relying on $this->get().
3. Clear Cache and Refresh Autoload Files
Old cache or stale autoload mappings can cause weird issues after dependency updates. Run these commands to clean things up:
# Clear dev environment cache php app/console cache:clear --env=dev # Clear prod environment cache php app/console cache:clear --env=prod # Refresh composer's autoload files composer dump-autoload
4. Verify Container Injection is Working
If the error persists, add a quick debug check in your login action to confirm if the container is present:
public function loginAction(Request $request) { // Temporary debug line—remove after testing var_dump($this->container); // Rest of your login logic here }
If this outputs null, your controller isn't being initialized with the container. Check your app/config/services.yml to make sure you haven't defined the controller as a service without injecting the container:
Bad (no container injection):
services: app.security_controller: class: AppBundle\Controller\SecurityController
Good (container injected):
services: app.security_controller: class: AppBundle\Controller\SecurityController calls: - [setContainer, ["@service_container"]]
Or better yet, use constructor injection for the specific services your controller needs instead of injecting the whole container.
5. Ensure Symfony Framework Version is Consistent
Double-check that symfony/symfony hasn't been updated beyond 2.8.x accidentally. Run this command to check your current version:
composer show symfony/symfony
If it's not a 2.8.x version, lock it down and update:
composer require symfony/symfony:2.8.* --no-update composer update
Work through these steps in order, and you should get your login page loading again in no time.
内容的提问来源于stack exchange,提问作者Matt Welander

