Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Symfony 4.2 DI issue #857

Open
Open
@ProfM2

Description

@ProfM2

Starting with Symfony 4.2, Controller is deprecated, recommending AbstractController to be used.
However, by using AbstractController the 'old' way of getting the factory service no longer works.

Old: $datatable = $this->get('sg_datatables.factory')->create(EntityDatatable::class);

The new way to use the datatables factory is to use dependency injection in the constructor of the controller.

private $dtFactory;
private $dtResponse;
public function __construct(DatatableFactory $datatableFactory, DatatableResponse $datatableResponse)
{
  $this->dtFactory = $datatableFactory;
  $this->dtResponse = $datatableResponse;
}

This allows to access the factory:
$datatable = $this->dtFactory->create(EntityDatatable::class);
And the response:
$responseService = $this->dtResponse; in the Ajax response portion of the function.

However, now, Symfony complains:

Cannot autowire service "App\Controller\Support\SupportController": argument "$datatableFactory" of method "__construct()" references class "Sg\DatatablesBundle\Datatable\DatatableFactory" but no such service exists. You should maybe alias this class to the existing "sg_datatables.factory" service.

The fix for this is in the services.yaml to add the following:

Sg\DatatablesBundle\Datatable\DatatableFactory:
    alias: sg_datatables.factory
Sg\DatatablesBundle\Response\DatatableResponse:
    alias: sg_datatables.response

At this point, everything should be working properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions