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

Symfony 4.2 DI issue #857

Open
ProfM2 opened this issue Dec 12, 2018 · 6 comments · May be fixed by #922
Open

Symfony 4.2 DI issue #857

ProfM2 opened this issue Dec 12, 2018 · 6 comments · May be fixed by #922

Comments

@ProfM2
Copy link
Contributor

ProfM2 commented Dec 12, 2018

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.

@AlexandraDeMatos
Copy link

Hi !
I know someone else tell that but I can't find response.
I'm on Symfony 4.1 and I can't use Datatable bundle

#> php bin/console sg:datatable:generate AppBundle:Post
There are no commands defined in the "sg:datatable" namespace.
Did you mean this?
doctrine:database

Thank's by advance

@ProfM2
Copy link
Contributor Author

ProfM2 commented Dec 13, 2018

Hi !
I know someone else tell that but I can't find response.
I'm on Symfony 4.1 and I can't use Datatable bundle

#> php bin/console sg:datatable:generate AppBundle:Post
There are no commands defined in the "sg:datatable" namespace.
Did you mean this?
doctrine:database

Thank's by advance

I believe this is for a different question. The above is for Symfony 4.2 that deprecated Controller for AbstractController and the modifications necessary to allow proper dependency injection.

@althaus
Copy link

althaus commented Mar 21, 2019

In addition the route annotations

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

got superseded by a single

use Symfony\Component\Routing\Annotation\Route;

@mpeshehonov
Copy link

@Seb33300 @stwe guys plz check and merge Symfony 4(actual for Symfony 5) issues.
Have to use forks =(

@Seb33300
Copy link
Collaborator

Seb33300 commented Apr 3, 2020

If you are encountering issue, feel free to submit a pull request.

@mpeshehonov
Copy link

@Seb33300 #762 #833 #922 I haven't access for submit pull requests.
plz check this PRs and merge

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants