1
votes

Yep - i've search an answer and spend several hours with google. But trouble still actual

class IndexController extends Controller\CommonController
{
    private $container;
    public function __construct(Container $container) {
        $this->container = $container;
    }

in config.yml

shop.website.index_controller:
    class: %shop.website.index_controller%
    parent: shop.common.common_controller
    arguments:  [@service_container]

and

Catchable Fatal Error: Argument 1 passed to Shop\WebSiteBundle\Controller\IndexController::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in I:\sf2\www\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver.php on line 77 and defined in I:\sf2\www\src\Shop\WebSiteBundle\Controller\IndexController.php line 13

Can anybody explain where is the error?

Configuration in yml / annotaions please ( Cause different types for configuring make me crazy )

Thanks in advance

P.S> Updated code id

services:
    shop.common.common_controller:
        abstract: true
        class: %shop.common.common_controller%
        arguments: ["@templating"]

and

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class CommonController
{
    protected $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

the same result (

4
I try to create self controller wich didn't extends internal symfony controller - so i need to implement get method myself - that is the reason to pass containeruser4227063
I recommend checking ControllerAutowire bundle, that really simplified this whole process: tomasvotruba.cz/blog/2016/03/10/…Tomas Votruba

4 Answers

1
votes

Posilbe reason is next:

SF2 call your class constructor directly. But you should say to get it as service ( to provide all service options as calls / arguments )

Add

/**
 * @Route(service="shop.website.index_controller")
 */
class IndexController extends Controller\CommonController
{
    private $container;
    public function __construct(Container $container) {
        $this->container = $container;
    }shop.website.index_controller

Rew this at page http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#controller-as-service

And pay attention to Matthieu Napoli answer

5
votes

How did you configure your controller in your route?

I have a feeling that you didn't use the "Controller as a service" notation:

my_controller:
    pattern:   /
    defaults:  { _controller: shop.website.index_controller:indexAction }

This syntax is different from the default route syntax. Have a look here.

1
votes

You don't need to extend any other file / controller.

Remove Extends first.

Add:

use Symfony\Component\DependencyInjection\ContainerInterface;

Replace:

Container $container

to

ContainerInterface $container
-1
votes

Why would you inject the container to a controller?

Just extends Symfony\Bundle\FrameworkBundle\Controller\Controller and you can access any service like this:

    namespace AppBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    class HelloController extends Controller
    {
        public function indexAction()
        {
            // ...
            $myService = $this->get('my_service');
        }
    }