Back to Rules
PHP

Laravel PHP Best Practices: Essential Coding Rules for Efficient Development

Claude Directory November 29, 2025
0 copies 1 downloads

Unlock expert Laravel PHP coding standards with rewritten rules focusing on SOLID principles, Eloquent ORM, MVC architecture, and performance optimizations for scalable web apps.

Rule Content
### Context
These guidelines are tailored for Laravel PHP developers using AI-assisted tools like Cursor. They promote clean, maintainable code by adhering to Laravel conventions, modern PHP features, and best practices. Prioritize modularity, dependency injection, and built-in Laravel tools to build robust applications efficiently.

### Rules
- **Adopt Modern PHP Standards**: Leverage PHP 8.1+ capabilities such as typed properties and match statements. Enforce strict typing with `declare(strict_types=1);` and comply with PSR-12 formatting.
- **Embrace Laravel Structure**: Stick to Laravel's directory conventions (e.g., `app/Http/Controllers` in kebab-case). Use MVC patterns, Blade for views, and Eloquent for models with proper relationships.
- **Prioritize OOP and SOLID**: Write modular, iterable code avoiding duplication. Use descriptive names, dependency injection via service containers, and Repository patterns for data abstraction.
- **Handle Data and Security Robustly**: Prefer Eloquent ORM and query builder over raw SQL. Implement Form Requests for validation, middleware for filtering, custom exceptions, and Laravel's auth, CSRF, and authorization.
- **Optimize Performance and Reliability**: Apply caching, job queues, pagination, indexing, and transactions. Use events/listeners for decoupling, scheduling for tasks, and Mix/Vite for assets.
- **Testing and Maintenance**: Integrate PHPUnit/Dusk for tests, API versioning/resources, localization, and comprehensive logging/monitoring.
- **Routing and APIs**: Define endpoints with Laravel routing, transform responses via API resources, and version public APIs.

### Examples
**Strict Typing and Typed Properties**:
```php
declare(strict_types=1);

class UserController
{
    public function __construct(private UserRepository $repository) {}

    public function index(Request $request): LengthAwarePaginator
    {
        return $this->repository->getUsers($request->get('search'));
    }
}
```

**Eloquent Relationship and Repository**:
```php
// app/Repositories/UserRepository.php
class UserRepository
{
    public function getUsers(?string $search = null): LengthAwarePaginator
    {
        return User::search($search)
            ->with('posts')
            ->paginate(15);
    }
}

// Model
class User extends Model
{
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}
```

**Form Request Validation**:
```php
// app/Http/Requests/StoreUserRequest.php
class StoreUserRequest extends FormRequest
{
    public function rules(): array
    {
        return ['email' => 'required|email|unique:users'];
    }
}

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory