关于 php-symfony
用于构建 Symfony 应用的技能,涵盖 Symfony 6.4 LTS 和 7.x,以及 Doctrine ORM、依赖注入和 API Platform 等主题。它按主题和级别路由请求,并提供学习模块、代码示例和故障排查指导。
主要功能
- 01涵盖 Symfony 6.4 LTS 和 7.x
- 02涉及 Doctrine 与依赖注入
- 03按主题和级别路由请求
- 04提供代码示例和测试模板
- 05包含故障排查指导
适用场景
- 01学习 Doctrine ORM 和迁移
- 02通过依赖注入配置服务
- 03使用 API Platform 构建 API
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-php --skill php-symfony安装后在 Claude Code 中输入/php-symfony确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/config.yamlschema.jsonreferences/GUIDE.mdPATTERNS.mdscripts/validate.py详细介绍
Symfony Framework 技能
掌握企业级 Symfony 应用的原子技能
概述
用于构建稳健 Symfony 应用的综合技能。涵盖 Symfony 6.4 LTS 和 7.x,重点介绍 Doctrine ORM、依赖注入和 API Platform。
技能参数
输入验证
interface SkillParams {
topic:
| "doctrine" // ORM, DBAL, migrations
| "di-container" // Services, autowiring, tags
| "routing" // Routes, controllers
| "security" // Voters, firewalls
| "messenger" // Async messaging
| "api-platform" // REST/GraphQL APIs
| "forms"; // Form types, validation
level: "beginner" | "intermediate" | "advanced";
symfony_version?: "6.4" | "7.0" | "7.1" | "7.2";
project_type?: "webapp" | "api" | "microservice";
}
验证规则
validation:
topic:
required: true
allowed: [doctrine, di-container, routing, security, messenger, api-platform, forms]
level:
required: true
symfony_version:
default: "7.1"
学习模块
模块 1:Doctrine ORM
beginner:
- Entity creation with attributes
- Basic relationships
- Repository basics
intermediate:
- DQL and QueryBuilder
- Lifecycle events
- Custom repository methods
advanced:
- Inheritance mapping
- Second-level cache
- Event subscribers
模块 2:依赖注入
beginner:
- Service basics
- Autowiring
- Constructor injection
intermediate:
- Service tags
- Factory services
- Decorators
advanced:
- Compiler passes
- Service subscribers
- Lazy services
模块 3:Messenger 组件
beginner:
- Messages and handlers
- Sync vs async transports
intermediate:
- Message middleware
- Retry strategies
- Multiple transports
advanced:
- Custom transports
- Saga patterns
执行流程
graph TD
A[Skill Invoked] --> B{Validate}
B -->|Invalid| C[Return Error]
B -->|Valid| D[Load Content]
D --> E{Topic Router}
E --> F[Generate Examples]
F --> G[Add Console Commands]
错误处理与重试逻辑
errors:
CONTAINER_ERROR:
code: "SYMFONY_001"
recovery: "Run cache:clear, check services.yaml"
DOCTRINE_ERROR:
code: "SYMFONY_002"
recovery: "Run doctrine:schema:validate"
retry:
max_attempts: 3
backoff:
type: exponential
initial_delay_ms: 100
代码示例
Doctrine 实体
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private string $name;
#[ORM\ManyToOne(inversedBy: 'products')]
#[ORM\JoinColumn(nullable: false)]
private Category $category;
#[ORM\Column]
private \DateTimeImmutable $createdAt;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}
}
使用 DI 的服务
<?php
declare(strict_types=1);
namespace App\Service;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final readonly class ProductService
{
public function __construct(
private ProductRepository $repository,
private EntityManagerInterface $entityManager,
private LoggerInterface $logger,
#[Autowire('%app.tax_rate%')]
private float $taxRate,
) {}
public function createProduct(array $data): Product
{
$product = new Product();
$product->setName($data['name']);
$this->entityManager->persist($product);
$this->entityManager->flush();
$this->logger->info('Product created', ['id' => $product->getId()]);
return $product;
}
}
Messenger 处理器
<?php
declare(strict_types=1);
namespace App\MessageHandler;
use App\Message\SendOrderConfirmation;
use App\Service\EmailService;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
final readonly class SendOrderConfirmationHandler
{
public function __construct(
private EmailService $emailService,
) {}
public function __invoke(SendOrderConfirmation $message): void
{
$this->emailService->sendOrderConfirmation($message->orderId);
}
}
测试模板
<?php
declare(strict_types=1);
namespace App\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class ProductControllerTest extends WebTestCase
{
public function testListProducts(): void
{
$client = static::createClient();
$client->request('GET', '/api/products');
$this->assertResponseIsSuccessful();
}
}
故障排除
| 问题 | 解决方案 |
|---|---|
| 找不到服务 | 运行 debug:container,检查自动装配 |
| Doctrine 映射错误 | 运行 doctrine:schema:validate |
| 找不到路由 | 运行 debug:router,检查防火墙 |
质量指标
| 指标 | 目标 |
|---|---|
| 代码准确性 | 100% |
| Symfony 约定 | 100% |
| DI 最佳实践 | 100% |
用法
Skill("php-symfony", {topic: "messenger", level: "advanced"})
评论
支持 Markdown