我安装了该捆绑软件,并按照教程逐步进行操作:
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文件中缺少某些内容。但是在本教程中,他们没有对此发表任何评论。所以也许这是另一个原因。
答案 0 :(得分: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;
}
// ...
}
答案 1 :(得分:0)
您可以在config / bundles.php中将以下行添加到返回中。版本4中不存在AppKernel。
return [
... all other bundles
Omines\DataTablesBundle\DataTablesBundle::class => ['all' => true]
];