PHP项目Symfony maker-bundle生成

wen PHP项目 2

本文目录导读:

PHP项目Symfony maker-bundle生成

  1. Symfony Maker Bundle 概述
  2. 安装
  3. 常用生成命令
  4. 实际示例
  5. 生成的代码结构示例
  6. 查看所有可用命令
  7. 配置选项

我将为您详细介绍 Symfony Maker Bundle 的生成功能与使用方法。

Symfony Maker Bundle 概述

Maker Bundle 是 Symfony 的代码生成工具,可以快速生成各种常用代码结构。

安装

composer require --dev symfony/maker-bundle

常用生成命令

生成控制器 (Controller)

# 生成基本控制器
php bin/console make:controller ProductController
# 生成API控制器
php bin/console make:controller --api ProductApiController

生成实体 (Entity)

# 生成实体类
php bin/console make:entity Product
# 交互式添加字段示例:
# - name (string, 255)
# - price (float)
# - description (text, nullable)
# - createdAt (datetime_immutable)

生成 CRUD

# 基于实体生成完整的CRUD
php bin/console make:crud Product

生成表单 (Form)

# 生成表单类型
php bin/console make:form ProductForm Product

生成实体与用户认证

# 生成用户实体
php bin/console make:user User
# 生成登录认证
php bin/console make:auth
# 生成注册表单
php bin/console make:registration-form

生成其他常用代码

# 生成事件订阅器
php bin/console make:subscriber
# 生成Voter权限
php bin/console make:voter
# 生成命令
php bin/console make:command
# 生成数据工厂
php bin/console make:factory
# 生成迁移
php bin/console make:migration
# 生成验证器
php bin/console make:validator
# 生成Twig扩展
php bin/console make:twig-extension
# 生成序列化器
php bin/console make:serializer-encoder

实际示例

创建博客系统示例

# 1. 创建用户实体
php bin/console make:user User
# 2. 创建文章实体
php bin/console make:entity Post
# 字段设置:(string, 255)
# - content (text)
# - publishedAt (datetime, nullable)
# - author (relation ManyToOne to User)
# 3. 创建分类实体
php bin/console make:entity Category
# 字段设置:
# - name (string, 255)
# - posts (relation ManyToMany to Post)
# 4. 生成CRUD
php bin/console make:crud Post
php bin/console make:crud Category
# 5. 生成登录系统
php bin/console make:auth
# 6. 生成表单验证
php bin/console make:validator UniquePostTitle

生成的代码结构示例

Entity 生成示例

#[Entity(repositoryClass: ProductRepository::class)]
class Product
{
    #[Id]
    #[GeneratedValue]
    #[Column(type: 'integer')]
    private ?int $id = null;
    #[Column(type: 'string', length: 255)]
    private ?string $name = null;
    #[Column(type: 'float')]
    private ?float $price = null;
    // getters and setters...
}

控制器生成示例

#[Route('/product')]
class ProductController extends AbstractController
{
    #[Route('/', name: 'product_index', methods: ['GET'])]
    public function index(ProductRepository $productRepository): Response
    {
        return $this->render('product/index.html.twig', [
            'products' => $productRepository->findAll(),
        ]);
    }
    #[Route('/new', name: 'product_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $product = new Product();
        $form = $this->createForm(ProductForm::class, $product);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($product);
            $entityManager->flush();
            return $this->redirectToRoute('product_index');
        }
        return $this->render('product/new.html.twig', [
            'product' => $product,
            'form' => $form->createView(),
        ]);
    }
}

查看所有可用命令

php bin/console list make

配置选项

config/packages/dev/maker.yaml 中可以配置:

# config/packages/dev/maker.yaml
when@dev:
    maker:
        root_namespace: 'App'
        generate_final_entities: false
        generate_final_embeddables: false

Maker Bundle 能极大提高 Symfony 开发效率,建议在开发环境中使用。

抱歉,评论功能暂时关闭!