|
| 1 | +# Laravel & PHP Guidelines for Cursor AI |
| 2 | + |
| 3 | +## Core Principle |
| 4 | +Follow Laravel conventions first. If Laravel has a documented way to do something, use it. Only deviate when you have a clear justification. |
| 5 | + |
| 6 | +## PHP Standards |
| 7 | +- Follow PSR-1, PSR-2, and PSR-12 |
| 8 | +- Use camelCase for non-public-facing strings |
| 9 | +- Use short nullable notation: `?string` not `string|null` |
| 10 | +- Always specify `void` return types when methods return nothing |
| 11 | +- Use typed properties, not docblocks |
| 12 | +- Use constructor property promotion when all properties can be promoted |
| 13 | +- One trait per line |
| 14 | + |
| 15 | +## Type Declarations & Docblocks |
| 16 | +- Use typed properties over docblocks |
| 17 | +- Specify return types including `void` |
| 18 | +- Use short nullable syntax: `?Type` not `Type|null` |
| 19 | +- Always import classnames in docblocks - never use fully qualified names |
| 20 | +- Use one-line docblocks when possible: `/** @var string */` |
| 21 | +- For iterables, always specify key and value types: `@param array<int, MyObject> $myArray` |
| 22 | +- Use array shape notation for fixed keys, put each key on its own line: |
| 23 | + ```php |
| 24 | + /** @return array{ |
| 25 | + first: SomeClass, |
| 26 | + second: SomeClass |
| 27 | + } */ |
| 28 | + ``` |
| 29 | + |
| 30 | +## Control Flow |
| 31 | +- Happy path last: Handle error conditions first, success case last |
| 32 | +- Avoid else: Use early returns instead of nested conditions |
| 33 | +- Separate conditions: Prefer multiple if statements over compound conditions |
| 34 | +- Always use curly brackets even for single statements |
| 35 | +- Ternary operators: Each part on own line unless very short |
| 36 | + |
| 37 | +## Laravel Conventions |
| 38 | + |
| 39 | +### Controllers |
| 40 | +- Use singular resource names (BookController, UserController) |
| 41 | +- Stick to CRUD methods (index, create, store, show, edit, update, destroy) |
| 42 | +- Extract new controllers for non-CRUD actions |
| 43 | + |
| 44 | +### Routes |
| 45 | +- URLs: kebab-case (/open-source) |
| 46 | +- Route names: camelCase (->name('openSource')) |
| 47 | +- Parameters: camelCase ({userId}) |
| 48 | +- Use tuple notation: [Controller::class, 'method'] |
| 49 | + |
| 50 | +### Configuration |
| 51 | +- Files: kebab-case (pdf-generator.php) |
| 52 | +- Keys: snake_case (chrome_path) |
| 53 | +- Add service configs to config/services.php, don't create new files |
| 54 | +- Use config() helper, avoid env() outside config files |
| 55 | + |
| 56 | +### Artisan Commands |
| 57 | +- Names: kebab-case (delete-old-records) |
| 58 | +- Always provide feedback ($this->comment('All ok!')) |
| 59 | +- Show progress for loops, summary at end |
| 60 | +- Put output BEFORE processing item |
| 61 | + |
| 62 | +## Strings & Formatting |
| 63 | +- Use string interpolation over concatenation |
| 64 | +- Use __() function over @lang for translations |
| 65 | + |
| 66 | +## Enums |
| 67 | +- Use PascalCase for enum values |
| 68 | + |
| 69 | +## Comments |
| 70 | +- Avoid comments - write expressive code instead |
| 71 | +- Single line: // with space after |
| 72 | +- Multi-line: /* start with single * |
| 73 | +- Refactor comments into descriptive function names |
| 74 | + |
| 75 | +## Whitespace |
| 76 | +- Add blank lines between statements for readability |
| 77 | +- Exception: sequences of equivalent single-line operations |
| 78 | +- No extra empty lines between {} brackets |
| 79 | +- Let code "breathe" - avoid cramped formatting |
| 80 | + |
| 81 | +## Validation |
| 82 | +- Use array notation for multiple rules: ['email' => ['required', 'email']] |
| 83 | +- Custom validation rules use snake_case |
| 84 | + |
| 85 | +## Blade Templates |
| 86 | +- Indent with 4 spaces |
| 87 | +- No spaces after control structures: @if($condition) |
| 88 | + |
| 89 | +## Authorization |
| 90 | +- Policies use camelCase: Gate::define('editPost', ...) |
| 91 | +- Use CRUD words, but 'view' instead of 'show' |
| 92 | + |
| 93 | +## API Routing |
| 94 | +- Use plural resource names: /books |
| 95 | +- Use kebab-case: /error-occurrences |
| 96 | +- Limit deep nesting for simplicity |
| 97 | + |
| 98 | +## Naming Conventions Quick Reference |
| 99 | +- Classes: PascalCase (UserController, OrderStatus) |
| 100 | +- Methods/Variables: camelCase (getUserName, $firstName) |
| 101 | +- Routes: kebab-case (/open-source, /user-profile) |
| 102 | +- Config files: kebab-case (pdf-generator.php) |
| 103 | +- Config keys: snake_case (chrome_path) |
| 104 | +- Artisan commands: kebab-case (delete-old-records) |
| 105 | +- Controllers: singular resource name + Controller (BookController, UserController) |
| 106 | +- Views: camelCase (openSource.blade.php) |
| 107 | +- Jobs: action-based (CreateUser, SendEmailNotification) |
| 108 | +- Events: tense-based (UserRegistering, UserRegistered) |
| 109 | +- Listeners: action + Listener suffix (SendInvitationMailListener) |
| 110 | +- Commands: action + Command suffix (PublishScheduledPostsCommand) |
| 111 | +- Mailables: purpose + Mail suffix (AccountActivatedMail) |
| 112 | +- Resources/Transformers: plural + Resource/Transformer (UsersResource) |
| 113 | +- Enums: descriptive name, no prefix (OrderStatus, BookingType) |
| 114 | + |
| 115 | +## Code Quality Reminders |
| 116 | +- Use typed properties over docblocks |
| 117 | +- Prefer early returns over nested if/else |
| 118 | +- Use constructor property promotion when all properties can be promoted |
| 119 | +- Avoid else statements when possible |
| 120 | +- Use string interpolation over concatenation |
| 121 | +- Always use curly braces for control structures |
0 commit comments