schmittjoh / JMSDiExtraBundle

Provides Advanced Dependency Injection Features for Symfony2
http://jmsyst.com/bundles/JMSDiExtraBundle
330 stars 130 forks source link

Cannot redeclare class error when using 'Controller' as service #33

Open predakanga opened 12 years ago

predakanga commented 12 years ago

I'm in the process of updating my current project to Symfony 2.1 - as part of that, I hit a stumbling block.

I have a number of controllers (extending the Controller class for now, for simplicity) which are also defined as services (not through DiExtra, but simply services.xml).

When attempting to render an action from inside one of these controllers which results in the controller being fetched as a service, a "Cannot redeclare class" error is thrown. In this case, I can see that identical proxy classes are generated in both the 'cache/dev/diextra' and 'cache/dev/jms_aop' folders.

It may sound slightly confusing, so I'll break down the workflow:

  1. Request is routed to [feature]AdminController through the simplified Controller class methods, not as a service
  2. [feature]AdminController renders a view
  3. The view runs {% render 'FusionAdminBundle:Default:sidebar' %} to generate a sidebar
  4. The sidebar controller requests the 'fusion.admin.manager' services, which is injected with each [feature]AdminController as a service
  5. Cannot redeclare class error occurs

This is not an urgent problem as it's rather poor design (currently refactoring it to just use KnpMenu), but it is nevertheless one which can occur.

schmittjoh commented 12 years ago

Can you paste the full error?

On Wed, Jul 11, 2012 at 7:52 PM, predakanga < reply@reply.github.com

wrote:

I'm in the process of updating my current project to Symfony 2.1 - as part of that, I hit a stumbling block.

I have a number of controllers (extending the Controller class for now, for simplicity) which are also defined as services (not through DiExtra, but simply services.xml).

When attempting to render an action from inside one of these controllers which results in the controller being fetched as a service, a "Cannot redeclare class" error is thrown. In this case, I can see that identical proxy classes are generated in both the 'cache/dev/diextra' and 'cache/dev/jms_aop' folders.

It may sound slightly confusing, so I'll break down the workflow:

  1. Request is routed to [feature]AdminController through the simplified Controller class methods, not as a service
  2. [feature]AdminController renders a view
  3. The view runs {% render 'FusionAdminBundle:Default:sidebar' %} to generate a sidebar
  4. The sidebar controller requests the 'fusion.admin.manager' services, which is injected with each [feature]AdminController as a service
  5. Cannot redeclare class error occurs

This is not an urgent problem as it's rather poor design (currently refactoring it to just use KnpMenu), but it is nevertheless one which can occur.


Reply to this email directly or view it on GitHub: https://github.com/schmittjoh/JMSDiExtraBundle/issues/33

predakanga commented 12 years ago

I've attached the error and a few other things that should be useful at https://gist.github.com/3a40ad44b7c08883d392

predakanga commented 12 years ago

Just noticed that I was using SecurityExtraBundle in there as well (likely what caused the generation of the jms_aop proxies)

Martiusweb commented 12 years ago

Hi,

Did you find a way to solve or work around this issue ?

predakanga commented 12 years ago

Hi there,

I just worked around it by switching from using {{ render }} to using a KnpMenu in a block. Unfortunately, that's pretty specific to my use case.

Fludimir commented 11 years ago

Upgraded Symfony from 2.0 to 2.2 and get same problem with controller defined as service, no solution for now?

jverdeyen commented 11 years ago

Any new on this one? I'm having this when clearing my cache:

Symfony 2.3.1

[exec] PHP Fatal error:  Cannot redeclare class EnhancedProxy_edd9228a94a69fd3938cb936b9b73c6d89b335d9\__CG__\ProjectX\Company\AppBundle\Controller\ClientController in /var/lib/jenkins/workspace/Company/app/cache/c_/jms_diextra/proxies/ProjectX-Company-AppBundle-Controller-ClientController.php on line 28
     [exec] PHP Stack trace:
     [exec] PHP   1. {main}() /var/lib/jenkins/workspace/Company/app/console:0
     [exec] PHP   2. Symfony\Component\Console\Application->run() /var/lib/jenkins/workspace/Company/app/console:31
     [exec] PHP   3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:121
     [exec] PHP   4. Symfony\Component\Console\Application->doRun() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
     [exec] PHP   5. Symfony\Component\Console\Application->doRunCommand() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:191
     [exec] PHP   6. Symfony\Component\Console\Command\Command->run() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:906
     [exec] PHP   7. Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->execute() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:244
     [exec] PHP   8. Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->warmup() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:83
     [exec] PHP   9. Symfony\Component\HttpKernel\Kernel->boot() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:110
     [exec] PHP  10. Symfony\Component\HttpKernel\Kernel->initializeContainer() /var/lib/jenkins/workspace/Company/app/bootstrap.php.cache:2142
     [exec] PHP  11. Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() /var/lib/jenkins/workspace/Company/app/bootstrap.php.cache:2370
     [exec] PHP  12. JMS\DiExtraBundle\HttpKernel\ControllerInjectorsWarmer->warmUp() /var/lib/jenkins/workspace/Company/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:47
     [exec] PHP  13. JMS\DiExtraBundle\HttpKernel\ControllerResolver->createInjector() /var/lib/jenkins/workspace/Company/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerInjectorsWarmer.php:37
     [exec] PHP  14. JMS\DiExtraBundle\HttpKernel\ControllerResolver->prepareContainer() /var/lib/jenkins/workspace/Company/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerResolver.php:98
     [exec] PHP  15. JMS\DiExtraBundle\Generator\DefinitionInjectorGenerator->generate() /var/lib/jenkins/workspace/Company/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerResolver.php:157