首页 文章

Symfony 3将服务注入控制器

提问于
浏览
1

我正在使用symfony 3.1.7并且我在将控制器注入控制器方面存在问题,我的英语很糟糕我相信如果我显示所有代码会更好 .

The error:

类型错误:参数1传递给... Bundle \ Controller \ AdminController :: __ construct()必须实现接口Symfony \ Component \ DependencyInjection \ ContainerInterface,没有给定,在/......./project/var/中调用第2512行的cache / dev / classes.php

这是我的代码:

GenericRestController

namespace MyCoreBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use MyCoreBundle\Handler\GenericRestHandlerInterface as Generic;
/** other uses like FOSRestController **/

/**
 * Class GenericRestController
 */
abstract class GenericRestController extends FOSRestController
{
  protected $handler;
  protected $container;

  public function __construct(Container $container,Generic $handler)
  {
      $this->container = $container;
      $this->handler = $handler;
  }

  /** other methods like get, post, put, etc **/
}

AdminController

namespace ...Bundle\Controller;
use MyCoreBundle\Controller\GenericRestController;

class AdminController extends GenericRestController
{

}

已解决

Cerad说,解决方案是改变路线 .

官方文件

services.yml

services:
    app.hello_controller:
        class: AppBundle\Controller\HelloController

routing.yml

hello:
    path:     /hello
    defaults: { _controller: app.hello_controller:indexAction }

谢谢mtaqi和Cerad

1 回答

  • 0

    Cerad说,解决方案是改变路线 .

    官方文件

    services.yml

    services:
        app.hello_controller:
        class: AppBundle\Controller\HelloController
    

    routing.yml

    hello:
       path:     /hello
       defaults: { _controller: app.hello_controller:indexAction }
    

    对不起,迟到了,谢谢!

相关问题