Skip to content

project rulesBackend and APIsphplaravel

Laravel Rules

Project rules for Laravel apps: form request validation, Eloquent query hygiene, queued jobs, policies for authorization and Pest feature tests.

npx workspacetemplates add laravel-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file29 lines · 1.4 KBFile
# Project rules: Laravel

## Structure
- Controllers stay thin: authorize, call an action or service class, return a resource or redirect. Put reusable business steps in `app/Actions` or `app/Services`.
- Use `declare(strict_types=1)`, typed properties and return types.

## Input and output
- Validate in a Form Request class, never with inline `$request->validate` in long controllers. Use `$request->validated()` only; do not pass `$request->all()` to a model.
- Return API Resources, not raw models. Define `$fillable` explicitly.

## Authorization
- One Policy per model, called with `$this->authorize` or the `can` middleware. Do not compare user ids by hand.

## Eloquent
- Eager load with `with()` and catch lazy loading in development using `Model::preventLazyLoading()`. Select needed columns, use `chunkById` or `lazy` for large sets.
- Wrap multi-step writes in `DB::transaction`. Use scopes for repeated conditions.
- Every schema change is a migration with a working `down`. Use factories and seeders for sample data.

## Queues
- Move email, exports and API calls into queued jobs and notifications. Make jobs idempotent and set `tries` and `backoff`.

## Config
- Call `env()` only inside config files; use `config()` elsewhere. Secrets stay in `.env`.

## Tests
- Pest feature tests with `RefreshDatabase`, factories, `Queue::fake()` and `Mail::fake()`.

## Before finishing
Run `./vendor/bin/pint`, `php artisan test` and `./vendor/bin/phpstan analyse`.

What it does

Laravel offers a built-in place for almost everything, and these rules make the agent use those places: form requests for input, policies for permissions, jobs for slow work.

The goal is controllers that stay short and queries that stay cheap, with tests that describe behavior in plain Pest syntax.

Example

you › Send a receipt email after a purchase completes

agent › The agent dispatches a queued notification from the purchase action, makes it safe to retry, and adds a Pest test using Notification::fake to assert it was sent once.

Questions

Does it require Pest?
It prefers Pest, but the structure, validation and queue rules also hold for PHPUnit if that is what the project uses.
Why restrict env() to config files?
After config caching, env() outside config returns null, so values vanish in production while working locally.
  • ASP.NET Core Web API Rules

    Project rulesdotnet

    Free
  • Django Rules

    Project rulespython

    Free
  • Flask Rules

    Project rulespython

    Free
  • Go Service Rules

    Project rulesgo

    Free