backend-best-practices
SKILL.md
Backend Best Practices
Comprehensive backend development guidelines for API design, database patterns, authentication, error handling, performance, and security. Contains rules prioritized by impact to guide code generation and review.
When to Apply
Reference these guidelines when:
- Writing new API endpoints or controllers
- Designing database schemas or writing queries
- Implementing authentication or authorization logic
- Reviewing backend code for quality or security issues
- Refactoring existing backend code
- Enforcing coding standards across the team
Quick Reference
artisan-tests-use-command-class- Call$this->artisan(Command::class)instead of string signatures in testsconsole-command-signature-adra-prefix- Artisan command signatures start withadra:and a clear domain segmenteloquent-eager-load-relationships- Eager load relations withwith()/load()to avoid N+1 querieseloquent-mass-assignment-protection- Use$fillable/$guarded; never open mass assignment from raw request datafeature-flag-usage-backend- Gate features behind flags using the correct backend service patternfeature-specification-and-tdd-first- Document acceptance criteria, then write failing tests before implementationform-request-for-http-validation- Use Form Request classes for non-trivial validation and authorizationjson-resource-use-when-helper- Use$this->when()for conditional keys inJsonResource::toArray()laravel-coding-standards-naming- PSR-12, PascalCase classes, snake_case columns, kebab-case route namesno-unleash-facade-direct- UseFeatureFlagand route middleware, not Unleash facades directlyone-class-per-file- Keep only one class per PHP file for better organization and testabilityopenapi-spec-sync-with-api- Update each service’sopenapi/openapi.ymlwhen the HTTP contract changesphpunit-test-naming-and-groups- Usetest_or@test, add@group, and controlFeatureFlag::fake()per scenariotenant-scoped-test-case-pattern- Extend the service tenant test base (e.g.TenantTestCase) for tenant-bound tests
How to Use
Read individual rule files under rules/ for detailed explanations and code examples (see Quick Reference for slugs).
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references