JMSDiExtraBundle
JMSDiExtraBundle copied to clipboard
Cannot inherit from custom base controller
I use version 1.0.1 of this bundle.
In my setup I have a custom base controller, i.e. :
<?php
namespace Acme\CoreBundle\Controller\Base;
use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
use JMS\DiExtraBundle\Annotation as DI;
/**
* Controller that provides missing methods for the base controller
*/
class Controller extends BaseController
{
/**
* @DI\Inject
*/
protected $translator;
}
If in my children controller I inject a service, everything works as expected :
<?php
namespace Acme\CoreBundle\Controller\Frontend;
use Acme\CoreBundle\Controller\Base\Controller;
class BarController extends Controller
{
/**
* @DI\Inject
*/
protected $request;
public function indexAction()
{
// Works as expected
}
}
If I don't inject any service, I get an error like Method "Acme\CoreBundle\Controller\Base\Controller::indexAction" does not exist
<?php
namespace Acme\CoreBundle\Controller\Frontend;
use Acme\CoreBundle\Controller\Base\Controller;
class FooController extends Controller
{
public function indexAction()
{
// Method "Acme\CoreBundle\Controller\Base\Controller::indexAction" does not exist.
}
}
It looks like it tries to access to the parent controller instead of the children.
Is it an expected behaviour ? How can I fix that ?
A good start would be to create a failing test case in the test suite.
I'll give it a look as soon as I can.
I have the same issue. Basically, it can be summed up as follows: you can't extend from a base controller class if the class that's extending from that base controller does not have any injected dependencies of its own, because a proxy class is not generated and the resultant controller injector generated code tries to instantiate the base class instead of the extending class.
Same problem here. It also fails when the base controller is an abstract class, with the error message you could imagine: you cannot instantiate an Abstract class. A fix would be great!
I had the same problem and tried to make a solution https://github.com/schmittjoh/JMSDiExtraBundle/pull/153
Any updates on this? When I try to extend a Controller with injections I get a for URI "/" is not callable.
error.
I simple fix it by replacing on JMS\DiExtraBundle\Generator\DefinitionInjectorGenerator.php at line 83:
$writer->writeln('$instance = new \\'.$def->getClass().$this->dumpArguments($def->getArguments()).';');
with this:
$writer->writeln('$instance = new \\'.$className.$this->dumpArguments($def->getArguments()).';');
Could it be a good solution?