JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

[bug]@DI\Service and @Route on a controller

Open bronze1man opened this issue 11 years ago • 5 comments

I want AdminUserController can use as a controller as well as a service in dic I use follow code:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use JMS\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service("xxx.admin_user_role_controller", parent="tbf.container_aware")
 * @Route("/admin_user")
 */
class AdminUserController extends Controller{

It can use as service in dic,but it can not use as a controller any more. When I use it as controller I got:

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/xxx/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerResolver.php line 69
#0 [internal function]: Symfony\Component\HttpKernel\Debug\ErrorHandler->handle(2, 'call_user_func(...', '/home/xxx...', 69, Array)
#1 /home/xxx/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerResolver.php(69): call_user_func(NULL, Object(appTestDebugProjectContainer))
#2 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php(79): JMS\DiExtraBundle\HttpKernel\ControllerResolver->createController('XXX\AdminBun...')
#3 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/TraceableControllerResolver.php(46): Symfony\Component\HttpKernel\Controller\ControllerResolver->getController(Object(Symfony\Component\HttpFoundation\Request))
#4 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(117): Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController(Object(Symfony\Component\HttpFoundation\Request))
#5 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(73): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#6 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#7 /home/xxx/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(199): Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)

About injection into Controllers

Why there is a different between "Non-Controller Classes" and "Controllers" ?? We can configure a route on service in dic: http://symfony.com/doc/current/cookbook/controller/service.html Just define that controller as a service in dic,we can also inject some dependencies.

bronze1man avatar Jun 26 '13 03:06 bronze1man

to use controllers as services with the routing annotation of SensioFrameworkExtraBundle, you need to give it the service id: http://symfony.com/doc/current/cookbook/controller/service.html#using-annotation-routing

stof avatar Jun 26 '13 10:06 stof

I do not want use controllers as services. I just want to use it as a service in dic,and use it as a controller.... That controller is not route thought a services.

bronze1man avatar Jun 27 '13 03:06 bronze1man

Using the controller class as a service for something else than routing ? It looks like you are putting too much stuff in your controller. The logic should be moved to a separate class being defined as a service and used by both the controller and the other place needing it

stof avatar Jun 27 '13 18:06 stof

I want to allow JS call my controller, and I can call my controller in the php too. There is no different between 'js call' and 'php call'.... I tried defined those stuff in another separate service,then I found the controller was just a simple wrapper...

Whatever,I think we should not forbid this use case.

bronze1man avatar Jun 30 '13 03:06 bronze1man

I think relevant documentation moved to here https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#controller-as-service

flip111 avatar Nov 07 '15 00:11 flip111