使用DataTables Omines捆绑包出现错误Symfony 4

时间:2019-04-22 07:48:43

标签: symfony datatables

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

我执行了此命令“ composer require omines / datatables-bundle”。 之后,我执行了“ php bin /控制台资产:安装”。 我还添加了依赖项。

但是当我进入页面时出现此错误,您请求的服务不存在“ 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]);
    }
}

3 个答案:

答案 0 :(得分:0)

    <?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]);
    }
}

答案 1 :(得分:0)

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]);
    }
}

答案 2 :(得分:0)

您不应该扩展AbstractController

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyClass extends Controller{}