【问题标题】:How can I configure a non-existent service in Symfony 4?如何在 Symfony 4 中配置一个不存在的服务?
【发布时间】:2018-07-12 10:48:29
【问题描述】:

我安装了这个包并按照教程一步一步来:

https://omines.github.io/datatables-bundle/#introduction

我的控制器:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\Controller\DataTablesTrait;


class DataTableController extends Controller
{

  /**
  * @Route("/")
  */

  use DataTablesTrait;

  public function showAction(Request $request)
  {
    $table = $this->createDataTable()
    ->add('firstName', TextColumn::class)
    ->add('lastName', TextColumn::class)
    ->createAdapter(ArrayAdapter::class, [
      ['firstName' => 'Donald', 'lastName' => 'Trump'],
      ['firstName' => 'Barack', 'lastName' => 'Obama'],
    ])
    ->handleRequest($request);

    if ($table->isCallback()) {
      return $table->getResponse();
    }

    $this->render('list.html.twig', ['datatable' => $table]);
  }
}

但我收到错误消息:

您请求的服务不存在 "Omines\DataTablesBundle\DataTableFactory"。

我认为 services.yaml 文件中缺少某些内容。但在教程中,他们并没有说什么。所以也许是另一个原因。

【问题讨论】:

  • 文档确实明确包含短语“安装后,如果不使用 Flex,您应该将包注册到内核”。这是 Symfony 的基本知识,我们的文档确实假设在使用外部包开发应用程序时存在。

标签: php symfony service bundles


【解决方案1】:

该服务似乎是在 services.xml 中定义的,它本身是由捆绑配置引入的,并且当捆绑在 AppKernel 类中注册时会发生这种情况。

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            // ....

            new \Omines\DataTablesBundle\DataTablesBundle(),

            // ... other bundle registration
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            // ... 
        }

        return $bundles;
    }

    // ...
}

【讨论】:

【解决方案2】:

您可以将以下行添加到 config/bundles.php 中的返回。 AppKernel 在版本 4 中不存在。

return [
    ... all other bundles
    Omines\DataTablesBundle\DataTablesBundle::class => ['all' => true]
];

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 2012-08-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2014-11-13
    相关资源
    最近更新 更多