#!/usr/bin/env php render('{$entityLower}/index.html.twig', [ '{$entityPlural}' => \$repository->findAll(), ]); } #[Route('/new', name: '{$entityLower}_new', methods: ['GET', 'POST'])] public function new(Request \$request, EntityManagerInterface \$entityManager): Response { \${$entityLower} = new $entityName(); \$form = \$this->createForm({$entityName}Type::class, \${$entityLower}); \$form->handleRequest(\$request); if (\$form->isSubmitted() && \$form->isValid()) { \$entityManager->persist(\${$entityLower}); \$entityManager->flush(); \$this->addFlash('success', '$entityName created successfully!'); return \$this->redirectToRoute('{$entityLower}_show', ['id' => \${$entityLower}->getId()]); } return \$this->render('{$entityLower}/new.html.twig', [ '{$entityLower}' => \${$entityLower}, 'form' => \$form, ]); } #[Route('/{id}', name: '{$entityLower}_show', methods: ['GET'])] public function show($entityName \${$entityLower}): Response { return \$this->render('{$entityLower}/show.html.twig', [ '{$entityLower}' => \${$entityLower}, ]); } #[Route('/{id}/edit', name: '{$entityLower}_edit', methods: ['GET', 'POST'])] public function edit(Request \$request, $entityName \${$entityLower}, EntityManagerInterface \$entityManager): Response { \$form = \$this->createForm({$entityName}Type::class, \${$entityLower}); \$form->handleRequest(\$request); if (\$form->isSubmitted() && \$form->isValid()) { \$entityManager->flush(); \$this->addFlash('success', '$entityName updated successfully!'); return \$this->redirectToRoute('{$entityLower}_show', ['id' => \${$entityLower}->getId()]); } return \$this->render('{$entityLower}/edit.html.twig', [ '{$entityLower}' => \${$entityLower}, 'form' => \$form, ]); } #[Route('/{id}', name: '{$entityLower}_delete', methods: ['POST'])] public function delete(Request \$request, $entityName \${$entityLower}, EntityManagerInterface \$entityManager): Response { if (\$this->isCsrfTokenValid('delete'.\${$entityLower}->getId(), \$request->request->get('_token'))) { \$entityManager->remove(\${$entityLower}); \$entityManager->flush(); \$this->addFlash('success', '$entityName deleted successfully!'); } return \$this->redirectToRoute('{$entityLower}_index'); } } "; } else { // API Controller $controllerCode = "findAll(); return \$this->json(\${$entityPlural}, Response::HTTP_OK, [], [ 'groups' => ['{$entityLower}:read'] ]); } #[Route('', name: 'api_{$entityLower}_create', methods: ['POST'])] public function create(Request \$request): JsonResponse { \${$entityLower} = \$this->serializer->deserialize( \$request->getContent(), $entityName::class, 'json' ); \$errors = \$this->validator->validate(\${$entityLower}); if (count(\$errors) > 0) { \$errorMessages = []; foreach (\$errors as \$error) { \$errorMessages[\$error->getPropertyPath()] = \$error->getMessage(); } return \$this->json([ 'errors' => \$errorMessages ], Response::HTTP_BAD_REQUEST); } \$this->entityManager->persist(\${$entityLower}); \$this->entityManager->flush(); return \$this->json(\${$entityLower}, Response::HTTP_CREATED, [], [ 'groups' => ['{$entityLower}:read'] ]); } #[Route('/{id}', name: 'api_{$entityLower}_show', methods: ['GET'])] public function show($entityName \${$entityLower}): JsonResponse { return \$this->json(\${$entityLower}, Response::HTTP_OK, [], [ 'groups' => ['{$entityLower}:read', '{$entityLower}:detail'] ]); } #[Route('/{id}', name: 'api_{$entityLower}_update', methods: ['PUT'])] public function update(Request \$request, $entityName \${$entityLower}): JsonResponse { \$data = json_decode(\$request->getContent(), true); \$this->serializer->deserialize( \$request->getContent(), $entityName::class, 'json', ['object_to_populate' => \${$entityLower}] ); \$errors = \$this->validator->validate(\${$entityLower}); if (count(\$errors) > 0) { \$errorMessages = []; foreach (\$errors as \$error) { \$errorMessages[\$error->getPropertyPath()] = \$error->getMessage(); } return \$this->json([ 'errors' => \$errorMessages ], Response::HTTP_BAD_REQUEST); } \$this->entityManager->flush(); return \$this->json(\${$entityLower}, Response::HTTP_OK, [], [ 'groups' => ['{$entityLower}:read'] ]); } #[Route('/{id}', name: 'api_{$entityLower}_delete', methods: ['DELETE'])] public function delete($entityName \${$entityLower}): JsonResponse { \$this->entityManager->remove(\${$entityLower}); \$this->entityManager->flush(); return \$this->json(null, Response::HTTP_NO_CONTENT); } } "; } // Generate Form Type $formCode = "add('name', TextType::class, [ 'label' => 'Name', 'required' => true, 'attr' => [ 'class' => 'form-control', 'placeholder' => 'Enter name' ] ]) ->add('description', TextareaType::class, [ 'label' => 'Description', 'required' => false, 'attr' => [ 'class' => 'form-control', 'rows' => 4, 'placeholder' => 'Enter description' ] ]) // Add more fields based on your entity ; } public function configureOptions(OptionsResolver \$resolver): void { \$resolver->setDefaults([ 'data_class' => $entityName::class, ]); } } "; // Generate Templates $baseTemplate = "{% extends 'base.html.twig' %} {% block title %}$entityName Management{% endblock %} {% block body %}
{% for label, messages in app.flashes %} {% for message in messages %}
{{ message }}
{% endfor %} {% endfor %} {% block content %}{% endblock %}
{% endblock %}"; $indexTemplate = "{% extends '{$entityLower}/_base.html.twig' %} {% block content %}

{$entityName} List

New $entityName
{% for {$entityLower} in {$entityPlural} %} {% else %} {% endfor %}
ID Name Actions
{{ {$entityLower}.id }} {{ {$entityLower}.name|default('N/A') }} View Edit
No {$entityPlural} found
{% endblock %}"; $newTemplate = "{% extends '{$entityLower}/_base.html.twig' %} {% block content %}

Create New $entityName

{{ form_start(form) }} {{ form_widget(form) }} {{ form_end(form) }}
{% endblock %}"; $editTemplate = "{% extends '{$entityLower}/_base.html.twig' %} {% block content %}

Edit $entityName

{{ form_start(form) }} {{ form_widget(form) }}
Back
{{ form_end(form) }}
{% endblock %}"; $showTemplate = "{% extends '{$entityLower}/_base.html.twig' %} {% block content %}

$entityName Details

ID {{ {$entityLower}.id }}
Name {{ {$entityLower}.name|default('N/A') }}
Description {{ {$entityLower}.description|default('N/A') }}
Edit Back to list
{% endblock %}"; // Output the generated code echo "===========================================\n"; echo "GENERATED SYMFONY CRUD FOR: $entityName\n"; echo "===========================================\n\n"; echo "1. CONTROLLER CODE:\n"; echo "-------------------\n"; echo $controllerCode; echo "\n\n2. FORM TYPE CODE:\n"; echo "-------------------\n"; echo $formCode; if (!$isApi) { echo "\n\n3. TEMPLATES:\n"; echo "-------------------\n"; echo "Base Template (templates/{$entityLower}/_base.html.twig):\n"; echo $baseTemplate; echo "\n\n-------------------\n"; echo "Index Template (templates/{$entityLower}/index.html.twig):\n"; echo $indexTemplate; echo "\n\n-------------------\n"; echo "New Template (templates/{$entityLower}/new.html.twig):\n"; echo $newTemplate; echo "\n\n-------------------\n"; echo "Edit Template (templates/{$entityLower}/edit.html.twig):\n"; echo $editTemplate; echo "\n\n-------------------\n"; echo "Show Template (templates/{$entityLower}/show.html.twig):\n"; echo $showTemplate; } echo "\n\n===========================================\n"; echo "INSTALLATION INSTRUCTIONS:\n"; echo "===========================================\n"; echo "1. Save the controller to: src/Controller/" . ($isApi ? "Api/{$entityName}ApiController.php" : "{$entityName}Controller.php") . "\n"; echo "2. Save the form type to: src/Form/{$entityName}Type.php\n"; if (!$isApi) { echo "3. Create the template directory: mkdir -p templates/{$entityLower}\n"; echo "4. Save each template to its respective file in templates/{$entityLower}/\n"; } echo "\n"; echo "REQUIRED ENTITY SERIALIZATION GROUPS (for API):\n"; echo "Add these to your entity:\n"; echo "#[Groups(['{$entityLower}:read'])]\n"; echo "#[Groups(['{$entityLower}:write'])]\n"; echo "#[Groups(['{$entityLower}:detail'])]\n"; echo "\n"; echo "Don't forget to:\n"; echo "- Ensure your entity exists: src/Entity/$entityName.php\n"; echo "- Run migrations if needed: php bin/console doctrine:migrations:migrate\n"; echo "- Clear cache: php bin/console cache:clear\n";