简体   繁体   English

Symfony2:服务容器未传递给控制器​​构造函数

[英]Symfony2: service container didn't pass to controller constructor

Yep - i've search an answer and spend several hours with google. 是的-我已经搜索了答案,并在Google上花费了几个小时。 But trouble still actual 但是麻烦仍然存在

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

in config.yml 在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 可捕获的致命错误:传递给Shop \\ WebSiteBundle \\ Controller \\ IndexController :: __ construct()的参数1必须实现接口Symfony \\ Component \\ DependencyInjection \\ ContainerInterface,未给定,在I:\\ sf2 \\ www \\ vendor \\ symfony \\ symfony \\ src中调用\\ Symfony \\ Bundle \\ FrameworkBundle \\ Controller \\ ControllerResolver.php在第77行并在I:\\ sf2 \\ www \\ src \\ Shop \\ WebSiteBundle \\ Controller \\ IndexController.php第13行中定义

Can anybody explain where is the error? 谁能解释错误在哪里?

Configuration in yml / annotaions please ( Cause different types for configuring make me crazy ) 请在yml /注释中进行配置(导致不同类型的配置使我发疯)

Thanks in advance 提前致谢

PS> Updated code id PS>更新的代码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 ( 相同的结果(

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 . 在这里看看。

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

Posilbe reason is next: 接下来是Posilbe的原因:

SF2 call your class constructor directly. SF2直接调用您的类构造函数。 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 http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#controller-as-service页面上阅读

And pay attention to Matthieu Napoli answer 并注意Matthieu Napoli的答案

Why would you inject the container to a controller? 为什么将容器注入控制器?

Just extends Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller and you can access any service like this: 只需扩展Symfony \\ Bundle \\ FrameworkBundle \\ Controller \\ Controller,您就可以访问像这样的任何服务:

    namespace AppBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM