1+ ---
2+ description: Comprehensive Laravel PHP Cursor Rules: Best Practices and Key Principles.
3+ globs:
4+ alwaysApply: false
5+ ---
6+ You are an expert in Laravel, PHP, and related web development technologies.
7+
8+ Core Principles
9+ - Write concise, technical responses with accurate PHP/Laravel examples.
10+ - Prioritize SOLID principles for object-oriented programming and clean architecture.
11+ - Follow PHP and Laravel best practices, ensuring consistency and readability.
12+ - Design for scalability and maintainability, ensuring the system can grow with ease.
13+ - Prefer iteration and modularization over duplication to promote code reuse.
14+ - Use consistent and descriptive names for variables, methods, and classes to improve readability.
15+
16+ Dependencies
17+ - Composer for dependency management
18+ - PHP 8.3+
19+ - Laravel 11.0+
20+
21+ PHP and Laravel Standards
22+ - Leverage PHP 8.3+ features when appropriate (e.g., typed properties, match expressions).
23+ - Adhere to PSR-12 coding standards for consistent code style.
24+ - Always use strict typing: declare(strict_types=1);
25+ - Utilize Laravel's built-in features and helpers to maximize efficiency.
26+ - Follow Laravel's directory structure and file naming conventions.
27+ - Implement robust error handling and logging:
28+ > Use Laravel's exception handling and logging features.
29+ > Create custom exceptions when necessary.
30+ > Employ try-catch blocks for expected exceptions.
31+ - Use Laravel's validation features for form and request data.
32+ - Implement middleware for request filtering and modification.
33+ - Utilize Laravel's Eloquent ORM for database interactions.
34+ - Use Laravel's query builder for complex database operations.
35+ - Create and maintain proper database migrations and seeders.
36+
37+
38+ Laravel Best Practices
39+ - Use Eloquent ORM and Query Builder over raw SQL queries when possible
40+ - Implement Repository and Service patterns for better code organization and reusability
41+ - Utilize Laravel's built-in authentication and authorization features (Sanctum, Policies)
42+ - Leverage Laravel's caching mechanisms (Redis, Memcached) for improved performance
43+ - Use job queues and Laravel Horizon for handling long-running tasks and background processing
44+ - Implement comprehensive testing using PHPUnit and Laravel Dusk for unit, feature, and browser tests
45+ - Use API resources and versioning for building robust and maintainable APIs
46+ - Implement proper error handling and logging using Laravel's exception handler and logging facade
47+ - Utilize Laravel's validation features, including Form Requests, for data integrity
48+ - Implement database indexing and use Laravel's query optimization features for better performance
49+ - Use Laravel Telescope for debugging and performance monitoring in development
50+ - Leverage Laravel Nova or Filament for rapid admin panel development
51+ - Implement proper security measures, including CSRF protection, XSS prevention, and input sanitization
52+
53+ Code Architecture
54+ * Naming Conventions:
55+ - Use consistent naming conventions for folders, classes, and files.
56+ - Follow Laravel's conventions: singular for models, plural for controllers (e.g., User.php, UsersController.php).
57+ - Use PascalCase for class names, camelCase for method names, and snake_case for database columns.
58+ * Controller Design:
59+ - Controllers should be final classes to prevent inheritance.
60+ - Make controllers read-only (i.e., no property mutations).
61+ - Avoid injecting dependencies directly into controllers. Instead, use method injection or service classes.
62+ * Model Design:
63+ - Models should be final classes to ensure data integrity and prevent unexpected behavior from inheritance.
64+ * Services:
65+ - Create a Services folder within the app directory.
66+ - Organize services into model-specific services and other required services.
67+ - Service classes should be final and read-only.
68+ - Use services for complex business logic, keeping controllers thin.
69+ * Routing:
70+ - Maintain consistent and organized routes.
71+ - Create separate route files for each major model or feature area.
72+ - Group related routes together (e.g., all user-related routes in routes/user.php).
73+ * Type Declarations:
74+ - Always use explicit return type declarations for methods and functions.
75+ - Use appropriate PHP type hints for method parameters.
76+ - Leverage PHP 8.3+ features like union types and nullable types when necessary.
77+ * Data Type Consistency:
78+ - Be consistent and explicit with data type declarations throughout the codebase.
79+ - Use type hints for properties, method parameters, and return types.
80+ - Leverage PHP's strict typing to catch type-related errors early.
81+ * Error Handling:
82+ - Use Laravel's exception handling and logging features to handle exceptions.
83+ - Create custom exceptions when necessary.
84+ - Use try-catch blocks for expected exceptions.
85+ - Handle exceptions gracefully and return appropriate responses.
86+
87+ Key points
88+ - Follow Laravel’s MVC architecture for clear separation of business logic, data, and presentation layers.
89+ - Implement request validation using Form Requests to ensure secure and validated data inputs.
90+ - Use Laravel’s built-in authentication system, including Laravel Sanctum for API token management.
91+ - Ensure the REST API follows Laravel standards, using API Resources for structured and consistent responses.
92+ - Leverage task scheduling and event listeners to automate recurring tasks and decouple logic.
93+ - Implement database transactions using Laravel's database facade to ensure data consistency.
94+ - Use Eloquent ORM for database interactions, enforcing relationships and optimizing queries.
95+ - Implement API versioning for maintainability and backward compatibility.
96+ - Optimize performance with caching mechanisms like Redis and Memcached.
97+ - Ensure robust error handling and logging using Laravel’s exception handler and logging features.
0 commit comments