Title rewritten to read: Property routing issues in Symfony 5.4 resolved
P粉035600555
P粉035600555 2024-01-10 16:38:04
0
2
346

There is a solution in RFE; use bin command and php version to solve the problem

I'm working on a new application based on Symfony 5.4 and PHP 7.4 to test new additions and changes in Symfony 6. I created the entities and CRUD using the entity builder in the console and the database was created perfectly. However, generators use new "attributes" (according to the convention in https://symfony.com/doc/5.4/routing.html) instead of "classic" annotations. Debugging through the console to see the generated paths, any routes defined in the controller will not show up (a 404 error will of course show up when accessing the URL in development mode). I decided to replace the attribute with a classic annotation and the path showed up and the 404 error disappeared. But now, I found that the generator uses the repository for logic processing through the entity manager, and when accessing the index to start from scratch, I get the following error:

找不到类“App\Entity\Room”的实体管理器。请检查您的Doctrine配置,确保其配置为加载此实体的元数据。

The code snippet displayed in the debugger is as follows:

class RoomRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Room::class);  // 这里是错误的地方
    }

Entities start with:

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 completely fall back to annotations to rewrite the entire CRUD, which is a lot of work (which is what I'm trying to avoid using generators), so there's definitely something to do with properties Something I overlooked. Here's a controller that I haven't modified CRUD yet, so anyone can take a look and figure out why the router can't find a defined path with this type 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's the problem with all of this? Thanks in advance.

P粉035600555
P粉035600555

reply all(2)
P粉101708623

I ran into a similar situation and for me it was related to the wrong namespace on Traits (located under src/Entity/Traits).

This Trait isn't even used, but apparently it still causes this error.

P粉860370921

As mentioned in the first comment, force every bin/console (for the make: command) and composer command to be run by prefixing the command with php7.4 so everything works fine, but For "classic" annotations, I didn't find a way to use attributes in controllers in php7.4. The .php-version file seems to be only used by symfony-cli to start the development web server. Hope this helps anyone who may encounter this situation in the future.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template