[FIXED] SOLVED: Problem with attribute routing in symfony 5.4

Issue

There’s a solution via RFE for this in RFE; solve problem with bin commands and php versions

I’m dealing with a new app in symfony 5.4 and php 7.4 to test the new additions and changes in symfony 6. I’ve used the entity maker from the console to create the entity and the crud, and the db was created perfectly. However, the generator uses the new "attributes" (according to the convention in https://symfony.com/doc/5.4/routing.html) instead of the "classic" annotations. Debugging via console to see the resulting paths, none of the routes defined in the controllers is shown (of course, a 404 error was shown when accessing the url in dev mode). I decided to replace the attributes with classic annotations, and the paths are shown and the 404 error is gone. But now, I find that the logic the generator uses is via the repository to use the Entity Manager, and when accessing to the index to start from scratch, I get:

Could not find the entity manager for class "App\Entity\Room". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.

The portion of code shown in the debugger is this:

class RoomRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Room::class);  // Here is the error
    }

And the entity starts with this:

namespace App\Entity;

use App\Repository\RoomRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: RoomRepository::class)]
class Room
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column()]
    private ?int $id = null;

...

My biggest concern is that I guess I can’t rewrite the full crud reverting to annotations, which is a lot of work (just what I wanted to avoid by using the generator), so there must be something about the attributes that I’m missing. Here’s a controller whose crud I haven’t modified yet, so anyone can take a look and find out why the router can’t find the defined paths with this kind of annotation.

namespace App\Controller;

use App\Entity\RoomFeature;
use App\Form\RoomFeatureType;
use App\Repository\RoomFeatureRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/admin/feature')]
class RoomFeatureController extends AbstractController
{
    #[Route('/', name: 'admin_room_feature_index', methods: ['GET'])]
    public function index(RoomFeatureRepository $roomFeatureRepository): Response
    {
        return $this->render('room_feature/index.html.twig', [
            'room_features' => $roomFeatureRepository->findAll(),
        ]);
    }

...

What is the problem with all this? Thanks in advance.

Solution

As stated in the first comment, forcing every bin/console (for make: commnds) and composer commands to be run especifically by prepending php7.4 to the command, everything works but with "classic" annotations, I’ve found no way to use attributes in controllers with php7.4. The .php-version file seems to be used only by the symfony-cli to launch the dev webserver. I hope this helps anyone who can face this scenario in the future.

Answered By – Oscar Manuel Gómez Senovilla

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published