JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Cannot inherit from custom base controller

Open iamdto opened this issue 12 years ago • 7 comments

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 ?

iamdto avatar Jul 24 '12 11:07 iamdto

A good start would be to create a failing test case in the test suite.

schmittjoh avatar Jul 24 '12 11:07 schmittjoh

I'll give it a look as soon as I can.

iamdto avatar Jul 24 '12 12:07 iamdto

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.

daviddesberg avatar Aug 12 '12 03:08 daviddesberg

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!

albertofem avatar Jun 17 '13 17:06 albertofem

I had the same problem and tried to make a solution https://github.com/schmittjoh/JMSDiExtraBundle/pull/153

Evgenas avatar Jan 13 '14 23:01 Evgenas

Any updates on this? When I try to extend a Controller with injections I get a for URI "/" is not callable. error.

bassrock avatar Jun 20 '14 23:06 bassrock

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?

cocciagialla avatar Apr 28 '15 23:04 cocciagialla