APY / APYDataGridBundle

Symfony Datagrid Bundle
MIT License
492 stars 344 forks source link

Service "grid" not found on Symfony 4 #1033

Closed xgendrea closed 5 years ago

xgendrea commented 5 years ago

Hi all,

I installed Symfony 4.2.5 and lastest stable version from APY DataGrid Bundle and I just create an Entity called Child like this :

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use APY\DataGridBundle\Grid\Mapping as GRID;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ChildRepository")
 * @ORM\Table(name="`child`")
 * @GRID\Source(columns="firstName, lastName")
 */
class Child
{
    /**
     * @ORM\Id
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $lastName;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }
}

Then I created a controller :

<?php
namespace App\Controller\Web;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use APY\DataGridBundle\Grid\Source\Entity;

class ChildrenController extends AbstractController
{
    /**
     * @Route("/children")
     */
    public function view()
    {
        // Creates a simple grid based on your entity (ORM)
        $source = new Entity('Child');

        // Get a Grid instance
        $grid = $this->get('grid');

        // Attach the source to the grid
        $grid->setSource($source);

        // Return the response of the grid to the template
        return $grid->getGridResponse('App::grid.html.twig');
    }
}

And when I test it I have following issue :

ServiceNotFoundException Service "grid" not found: even though it exists in the app's container, the container inside "App\Controller\Web\ChildrenController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session", "templating" and "twig" services. Try using dependency injection instead.

Could you check what happens ?

Thanks

xgendrea commented 5 years ago

With Controller instead of AbstractController, it works. I close the issue

cierzniak commented 5 years ago

Controller is deprecated so why we should use it instead AbstractController?

npotier commented 4 years ago

@cierzniak indeed, if you want to access to the service grid directly, you mus sur Controller, which is not a best practice.

To have acces to a grid and use AbstractController, you can use the following code :

<?php
...
use APY\DataGridBundle\Grid\GridManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
...

class CourseController extends AbstractController
{
    public function index(GridManager $gridManager): Response
    {
        $grid = $gridManager->createGrid();
        ....
    }
}

Hope this helps.