AgentSkillsCN

laravel-architecture

以服务、存储库、动作及整洁代码模式设计 Laravel 应用架构。适用于构建项目结构、创建服务、实施 DI 或组织代码层次时使用。

SKILL.md
--- frontmatter
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.
versions:
  laravel: "12.46"
  php: "8.5"
user-invocable: true
references: references/container.md, references/providers.md, references/facades.md, references/contracts.md, references/structure.md, references/lifecycle.md, references/configuration.md, references/installation.md, references/upgrade.md, references/releases.md, references/sail.md, references/valet.md, references/homestead.md, references/octane.md, references/artisan.md, references/helpers.md, references/filesystem.md, references/processes.md, references/context.md, references/pennant.md, references/mcp.md, references/concurrency.md, references/deployment.md, references/envoy.md, references/logging.md, references/errors.md, references/packages.md
related-skills: solid-php, laravel-api, laravel-eloquent

Laravel Architecture Patterns

Agent Workflow (MANDATORY)

Before ANY implementation, use TeamCreate to spawn 3 agents:

  1. fuse-ai-pilot:explore-codebase - Analyze existing architecture
  2. fuse-ai-pilot:research-expert - Verify Laravel patterns via Context7
  3. mcp__context7__query-docs - Check service container and DI patterns

After implementation, run fuse-ai-pilot:sniper for validation.


Overview

Laravel architecture focuses on clean separation of concerns, dependency injection, and maintainable code organization. This skill covers everything from project structure to production deployment.

When to Use

  • Structuring new Laravel projects
  • Implementing services, repositories, actions
  • Setting up dependency injection
  • Configuring development environments
  • Deploying to production

Critical Rules

  1. Thin controllers - Delegate business logic to services
  2. Interfaces in app/Contracts/ - Never alongside implementations
  3. DI over facades - Constructor injection for testability
  4. Files < 100 lines - Split larger files per SOLID
  5. Environment separation - .env never committed

Architecture

text
app/
├── Actions/              # Single-purpose action classes
├── Contracts/            # Interfaces (DI)
├── DTOs/                 # Data transfer objects
├── Enums/                # PHP 8.1+ enums
├── Events/               # Domain events
├── Http/
│   ├── Controllers/      # Thin controllers
│   ├── Middleware/       # Request filters
│   ├── Requests/         # Form validation
│   └── Resources/        # API transformations
├── Jobs/                 # Queued jobs
├── Listeners/            # Event handlers
├── Models/               # Eloquent models only
├── Policies/             # Authorization
├── Providers/            # Service registration
├── Repositories/         # Data access layer
└── Services/             # Business logic

Reference Guide

Core Architecture

ReferenceWhen to Use
container.mdDependency injection, binding, resolution
providers.mdService registration, bootstrapping
facades.mdStatic proxies, real-time facades
contracts.mdInterfaces, loose coupling
structure.mdDirectory organization
lifecycle.mdRequest handling flow

Configuration & Setup

ReferenceWhen to Use
configuration.mdEnvironment, config files
installation.mdNew project setup
upgrade.mdVersion upgrades, breaking changes
releases.mdRelease notes, versioning

Development Environments

ReferenceWhen to Use
sail.mdDocker development
valet.mdmacOS native development
homestead.mdVagrant (legacy)
octane.mdHigh-performance servers

Utilities & Tools

ReferenceWhen to Use
artisan.mdCLI commands, custom commands
helpers.mdGlobal helper functions
filesystem.mdFile storage, S3, local
processes.mdShell command execution
context.mdRequest-scoped data sharing

Advanced Features

ReferenceWhen to Use
pennant.mdFeature flags
mcp.mdModel Context Protocol
concurrency.mdParallel execution

Operations

ReferenceWhen to Use
deployment.mdProduction deployment
envoy.mdSSH task automation
logging.mdLog channels, formatting
errors.mdException handling
packages.mdCreating packages

Templates

TemplatePurpose
UserService.php.mdService + repository pattern
AppServiceProvider.php.mdDI bindings, bootstrapping
ArtisanCommand.php.mdCLI commands, signatures, I/O
McpServer.php.mdMCP servers, tools, resources, prompts
PennantFeature.php.mdFeature flags, A/B testing
Envoy.blade.php.mdSSH deployment automation
sail-config.mdDocker Sail configuration
octane-config.mdFrankenPHP, Swoole, RoadRunner

Feature Matrix

FeatureReferencePriority
Service Containercontainer.mdHigh
Service Providersproviders.mdHigh
Directory Structurestructure.mdHigh
Configurationconfiguration.mdHigh
Installationinstallation.mdHigh
Octane (Performance)octane.mdHigh
Sail (Docker)sail.mdHigh
Artisan CLIartisan.mdMedium
Deploymentdeployment.mdMedium
Envoy (SSH)envoy.mdMedium
Facadesfacades.mdMedium
Contractscontracts.mdMedium
Valet (macOS)valet.mdMedium
Upgrade Guideupgrade.mdMedium
Logginglogging.mdMedium
Errorserrors.mdMedium
Lifecyclelifecycle.mdMedium
Filesystemfilesystem.mdMedium
Helpershelpers.mdLow
Pennant (Flags)pennant.mdLow
Contextcontext.mdLow
Processesprocesses.mdLow
Concurrencyconcurrency.mdLow
MCPmcp.mdLow
Packagespackages.mdLow
Releasesreleases.mdLow
Homesteadhomestead.mdLow

Quick Reference

Service Injection

php
public function __construct(
    private readonly UserServiceInterface $userService,
) {}

Service Provider Binding

php
public function register(): void
{
    $this->app->bind(UserServiceInterface::class, UserService::class);
    $this->app->singleton(CacheService::class);
}

Artisan Command

shell
php artisan make:provider CustomServiceProvider
php artisan make:command ProcessOrders

Environment Access

php
$debug = env('APP_DEBUG', false);
$config = config('app.name');