laravel-architecture

fusengine 的头像作者 fusengine

Design Laravel app architecture with services, repositories, actions, and clean code patterns. Use when structuring projects, creating services, implementing DI, or organizing code layers.

📖 中文导读:Design Laravel app architecture with services, repositories, actions, and clean code patterns. Use when structuring projects, creating services, implementing DI, or organizing code layers. 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: laravel-architecture description: Design Laravel app architecture with services, repositories, actions, and clean code patterns. Use when structuring projects, creating services, implementing DI, or organizing code layers.

Laravel Architecture Patterns

Documentation

Core

Service Container & Providers

CLI & Tools

Development Environment

Performance & Deployment

Advanced

Error Handling

app/
├── Actions/              # Single-purpose action classes
├── Services/             # Business logic
├── Repositories/         # Data access layer
│   └── Contracts/
├── Http/
│   ├── Controllers/      # Thin controllers
│   ├── Requests/         # Form validation
│   └── Resources/        # API transformations
├── Models/               # Eloquent models only
├── Enums/                # PHP 8.1+ enums
├── Events/               # Domain events
├── Listeners/            # Event handlers
└── Policies/             # Authorization

Service Pattern

<?php

declare(strict_types=1);

namespace App\Services;

final readonly class UserService
{
    public function __construct(
        private UserRepositoryInterface $repository,
    ) {}

    public function create(CreateUserDTO $dto): User
    {
        return $this->repository->create($dto->toArray());
    }
}

Dependency Injection

// AppServiceProvider.php
public function register(): void
{
    $this->app->bind(
        UserRepositoryInterface::class,
        UserRepository::class
    );
}