用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/skeletorflet/opencode-kit --skill php-patterns命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | php-patterns |
| description | PHP development principles and decision-making. Modern PHP 8.3, Laravel 11, Symfony 7, PSR standards. |
PHP development principles and decision-making. Learn to THINK, not copy-paste patterns.
This skill teaches decision-making principles for PHP development.
What are you building?
│
├── REST API
│ ├── Laravel (full-featured, most popular)
│ └── Symfony (enterprise, components)
│
├── CMS / Dashboard
│ ├── Laravel + Filament (fast admin)
│ ├── Laravel + Livewire (interactive)
│ └── Symfony (Symfony UX)
│
├── Simple API / Microservice
│ ├── Laravel (if you need full features)
│ ├── Slim (minimalist)
│ ├── Lumen (lightweight Laravel)
│ └── Flight (very simple)
│
├── CLI Tools
│ ├── Laravel Artisan
│ └── Symfony Console
│
└── Real-time / WebSocket
├── Laravel + Reverb (Pusher alternative)
└── Symfony + Mercure
| Factor | Laravel | Symfony | Slim |
|---|---|---|---|
| Ecosystem | Largest | Very large | Small |
| Learning Curve | Low-Medium | Medium | Very Low |
| Admin Tools | Filament/Panel | EasyAdmin | None |
| Flexibility | Opinionated | Modular | Minimal |
| Best For | Full-stack, rapid dev | Enterprise, K8s | Simple APIs |
// OLD: Doctrine annotations
/**
* @Entity
* @Table(name="users")
*/
class User
{
/**
* @Id
* @GeneratedValue
*/
private int $id;
}
// NEW: PHP 8 Attributes
#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
private int $id;
}
// Readonly (cannot be changed after construction)
class User
{
public function __construct(
public readonly int $id,
public readonly string $name,
public readonly string $email
) {}
}
// Cannot do this (will error):
$user->name = "Other"; // ❌ Error!
// Before PHP 8
class User
{
private int $id;
private string $name;
private string $email;
public function __construct(int $id, string $name, string $email)
{
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
}
// PHP 8: Constructor property promotion
class User
{
public function __construct(
public int $id,
public string $name,
public string $email
) {}
}
// OLD: switch
$status = match ($statusCode) {
200 => 'success',
404 => 'not_found',
500 => 'error',
default => 'unknown',
};
// With full expression
$result = match ($input) {
$a => 'a',
$b => 'b',
default => 'other',
};
// Binding interfaces to implementations
$this->app->bind(
UserRepositoryInterface::class,
UserRepository::class
);
// Singleton (one instance)
$this->app->singleton(
ConfigService::class,
fn() => new ConfigService(config('app.config'))
);
// Facade pattern (static-like access)
User::find(1); // Actually: UserRepository::class->find(1)
// Model with relationships
class User extends Model
{
protected $fillable = ['name', 'email'];
// Relationship
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
// Accessor
public function getFullNameAttribute(): string
{
return "{$this->name} (ID: {$this->id})";
}
}
// Query scopes
class User extends Model
{
public function scopeActive($query)
{
return $query->where('is_active', true);
}
}
// Usage
User::active()->where('role', )->();
// REST controller
class UserController extends Controller
{
public function index(): JsonResponse
{
$users = User::with('posts')->paginate(10);
return response()->json($users);
}
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users'
]);
$user = User::create($validated);
return response()->json($user, 201);
}
}
// Services.yaml
services:
App\Service\UserService:
arguments:
$repository: '@App\Repository\UserRepository'
// Service class
class UserService
{
public function __construct(
private UserRepository $repository,
private MailerInterface $mailer
) {}
}
use Symfony\Component\Validator\Constraints as Assert;
class CreateUserCommand
{
#[Assert\NotBlank]
#[Assert\Email]
public string $email;
#[Assert\NotBlank]
#[Assert\Length(min: 8)]
public string $password;
}
<?php
namespace App;
use App\Other\Example;
// Braces on new line, 4 spaces indent
class UserService
{
private int $id;
private string $name;
public function __construct(
int $id,
string $name
) {
$this->id = $id;
$this->name = $name;
}
}
use Psr\Http\Message\ServerRequestInterface;
// PSR-7 compatible handlers
function handleRequest(ServerRequestInterface $request)
{
$method = $request->getMethod();
$uri = $request->getUri()->getPath();
return new JsonResponse(['method' => $method]);
}
// Unit test
class UserServiceTest extends TestCase
{
public function test_creates_user(): void
{
$service = new UserService(new InMemoryUserRepository());
$user = $service->create('test@example.com');
$this->assertNotNull($user->id);
$this->assertEquals('test@example.com', $user->email);
}
}
// Feature test with HTTP client
class UserApiTest extends TestCase
{
public function test_get_users_returns_json(): void
{
$response = $this->getJson('/api/users');
$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'*' => [, , ]
]
]);
}
}
| Tool | Purpose |
|---|---|
| PHPUnit | Core testing |
| Pest | Cleaner syntax, built on PHPUnit |
| Mockery | Mocking |
| Laravel Dusk | Browser testing |
| PHPStan | Static analysis |
| PHP-CS-Fixer | Code style |
// Custom exceptions
class UserNotFoundException extends Exception
{
public function __construct(int $id)
{
parent::__construct("User with ID {$id} not found", 404);
}
}
// Handler in Laravel
class ExceptionHandler extends IlluminateExceptionHandler
{
public function register(): void
{
$this->reportable(function (UserNotFoundException $e) {
Log::warning("User not found: " . $e->getId());
});
}
}
// Using Result/Either pattern
class UserService
{
public function findUser(int $id): User|UserNotFoundException
{
$user = User::find();
(!) {
();
}
;
}
}
Before implementing:
Remember: PHP has evolved significantly. Use PHP 8+ features and PSR standards. Think in modern PHP.
Web accessibility (a11y). WCAG 2.1, ARIA, keyboard navigation, screen readers, testing tools.
Analytics and event tracking. Product analytics, Mixpanel, PostHog, Segment, GDPR compliance, event taxonomy.
UI animation patterns. CSS transitions, Framer Motion, GSAP, performance, accessibility.
基于 SOC 职业分类