错误Symfony 4与DataTables Omines捆绑

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误Symfony 4与DataTables Omines捆绑相关的知识,希望对你有一定的参考价值。

我想知道如何使DataTables捆绑工作,因为我无法使其工作。

我执行了这个命令“composer require omines / datatables-bundle”。在此之后,我执行了“php bin / console assets:install”。我还添加了依赖项。

但当我进入我的页面时,我有这个错误,你已经请求了一个不存在的服务“Omines \ DataTablesBundle \ DataTableFactory”。

你能帮我吗 ?

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\AdminUserFormType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Omines\DataTablesBundle\Column\TextColumn;
use Symfony\Component\Routing\Annotation\Route;
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Omines\DataTablesBundle\Controller\DataTablesTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UsersController extends Controller


    use DataTablesTrait;

    /**
     * @Route("/admin/user/test", name="users_test")
     */
    public function usersTest(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();
        

        return $this->render('users/test.html.twig', ['datatable' => $table]);
    

答案
    <?php

namespace App\Controller;

use App\Entity\User;
use App\Form\AdminUserFormType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Omines\DataTablesBundle\Column\TextColumn;
use Symfony\Component\Routing\Annotation\Route;
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Omines\DataTablesBundle\DataTableFactory;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UsersController extends Controller

    protected datatableFactory;
    public function __construct(DataTableFactory $datatableFactory) 
       $this->datatableFactory = $datatableFactory;
    

    /**
     * @Route("/admin/user/test", name="users_test")
     */
    public function usersTest(Request $request)
        $table = $this->datatableFactory->create([])
            ->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();
        

        return $this->render('users/test.html.twig', ['datatable' => $table]);
    

另一答案
class UsersController extends AbstractController

    use DataTablesTrait;
    protected datatableFactory;
    public function __construct(DataTableFactory $datatableFactory) 
       $this->datatableFactory = $datatableFactory;
    

    /**
     * @Route("/admin/user/test", name="users_test")
     */
    public function usersTest(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();
        

        return $this->render('users/test.html.twig', ['datatable' => $table]);
    

另一答案

你不应该扩展AbstractController

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyClass extends Controller

以上是关于错误Symfony 4与DataTables Omines捆绑的主要内容,如果未能解决你的问题,请参考以下文章

将现有的 Symfony 项目从 2.8.* 更新到 4.26.3

DataTables 使用 Bootstrap 4 条带表固定列透明

Symfony 4 + Sonata + Sonata Doctrine ORM Admin Bundle:错误:没有要处理的元数据类

为啥升级到 Symfony 4.4 后我不再看到错误预览页面?

Symfony 4.4 - 自定义错误模板不起作用

Symfony 4 - UniqueEntity 约束不显示消息错误