From 12027d9e985f8be2370aaef0d70c2f4293d97999 Mon Sep 17 00:00:00 2001 From: theRADcozaDEV Date: Wed, 11 Mar 2026 07:59:39 +0200 Subject: [PATCH] Add CLAUDE.md - AI agent instructions for module development Created comprehensive anti-hallucination documentation: - EXACT file templates with full code examples - Precise naming conventions (PascalCase, snake_case, kebab-case) - Step-by-step module creation checklist - Common mistakes to avoid section - Filament form/table component reference - Debugging commands and troubleshooting - Updated AI_CONTEXT.md and README.md to reference CLAUDE.md --- AI_CONTEXT.md | 4 + CLAUDE.md | 779 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 +- 3 files changed, 785 insertions(+), 1 deletion(-) create mode 100644 CLAUDE.md diff --git a/AI_CONTEXT.md b/AI_CONTEXT.md index 78308fe..3fb3a61 100644 --- a/AI_CONTEXT.md +++ b/AI_CONTEXT.md @@ -1,5 +1,8 @@ # AI Assistant Context +> **๐Ÿšจ CRITICAL: For detailed module development instructions, see [CLAUDE.md](CLAUDE.md)** +> That document contains EXACT file templates, naming conventions, and step-by-step instructions. + This document provides context for AI coding assistants working on projects built with this template. ## Template Overview @@ -331,6 +334,7 @@ make restore file=... # Restore database ## Documentation Links +- **[CLAUDE.md](CLAUDE.md)** - ๐Ÿšจ **AI AGENTS: START HERE** - Exact module templates - [GETTING_STARTED.md](GETTING_STARTED.md) - Setup walkthrough - [README.md](README.md) - Overview - [docs/modules.md](docs/modules.md) - Module system diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..d7ec8de --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,779 @@ +# AI Agent Instructions for Laravel Module Development + +> **CRITICAL**: This document provides EXACT specifications for building modules in this Laravel template. +> Follow these instructions PRECISELY. Do NOT deviate, guess, or improvise. +> When in doubt, READ THE EXISTING CODE before making changes. + +--- + +## ๐Ÿšจ BEFORE YOU START - MANDATORY STEPS + +### 1. Always Check Existing Patterns First +```bash +# List existing modules to see patterns +ls -la src/app/Modules/ + +# Read an existing module's structure +find src/app/Modules/[ExistingModule]/ -type f -name "*.php" +``` + +### 2. Always Check Logs After Errors +```bash +docker-compose exec app tail -n 100 storage/logs/laravel.log +``` + +### 3. Never Guess at Solutions +- If you encounter an error, READ the full stack trace +- Find the EXACT file and line number causing the issue +- Understand what the code is trying to do before fixing + +--- + +## ๐Ÿ“ PROJECT STRUCTURE - EXACT PATHS + +``` +Laravel-Docker-Dev-Template/ +โ”œโ”€โ”€ src/ # โ† ALL Laravel code goes here +โ”‚ โ”œโ”€โ”€ app/ +โ”‚ โ”‚ โ”œโ”€โ”€ Filament/ +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Pages/ +โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Settings.php # Site settings page +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Resources/ # Core Filament resources +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ UserResource.php +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ RoleResource.php +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ PermissionResource.php +โ”‚ โ”‚ โ”œโ”€โ”€ Http/ +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Controllers/ +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Middleware/ +โ”‚ โ”‚ โ”œโ”€โ”€ Models/ +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ User.php +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Setting.php +โ”‚ โ”‚ โ”œโ”€โ”€ Modules/ # โ† ALL modules go here +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [ModuleName]/ # โ† Each module is a folder +โ”‚ โ”‚ โ”œโ”€โ”€ Providers/ +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ AppServiceProvider.php +โ”‚ โ”‚ โ””โ”€โ”€ Traits/ +โ”‚ โ”‚ โ””โ”€โ”€ ModuleAuditable.php +โ”‚ โ”œโ”€โ”€ bootstrap/ +โ”‚ โ”‚ โ””โ”€โ”€ app.php # Middleware registration +โ”‚ โ”œโ”€โ”€ config/ +โ”‚ โ”œโ”€โ”€ database/ +โ”‚ โ”‚ โ”œโ”€โ”€ migrations/ +โ”‚ โ”‚ โ””โ”€โ”€ seeders/ +โ”‚ โ”‚ โ”œโ”€โ”€ DatabaseSeeder.php +โ”‚ โ”‚ โ””โ”€โ”€ RolePermissionSeeder.php +โ”‚ โ”œโ”€โ”€ resources/ +โ”‚ โ”‚ โ””โ”€โ”€ views/ +โ”‚ โ”‚ โ”œโ”€โ”€ layouts/ +โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ app.blade.php # Authenticated layout +โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ guest.blade.php # Guest layout +โ”‚ โ”‚ โ””โ”€โ”€ components/ +โ”‚ โ””โ”€โ”€ routes/ +โ”‚ โ”œโ”€โ”€ web.php +โ”‚ โ”œโ”€โ”€ api.php +โ”‚ โ””โ”€โ”€ auth.php +โ”œโ”€โ”€ docker-compose.yml +โ”œโ”€โ”€ setup.bat # Windows setup +โ””โ”€โ”€ setup.sh # Linux/Mac setup +``` + +--- + +## ๐Ÿ”ง MODULE STRUCTURE - EXACT SPECIFICATION + +When creating a module named `[ModuleName]` (e.g., `StockManagement`): + +``` +src/app/Modules/[ModuleName]/ +โ”œโ”€โ”€ Config/ +โ”‚ โ””โ”€โ”€ [module_name].php # snake_case filename +โ”œโ”€โ”€ Database/ +โ”‚ โ”œโ”€โ”€ Migrations/ +โ”‚ โ”‚ โ””โ”€โ”€ YYYY_MM_DD_HHMMSS_create_[table_name]_table.php +โ”‚ โ””โ”€โ”€ Seeders/ +โ”‚ โ””โ”€โ”€ [ModuleName]Seeder.php +โ”œโ”€โ”€ Filament/ +โ”‚ โ””โ”€โ”€ Resources/ +โ”‚ โ”œโ”€โ”€ [ModelName]Resource.php +โ”‚ โ””โ”€โ”€ [ModelName]Resource/ +โ”‚ โ””โ”€โ”€ Pages/ +โ”‚ โ”œโ”€โ”€ List[ModelName]s.php +โ”‚ โ”œโ”€โ”€ Create[ModelName].php +โ”‚ โ””โ”€โ”€ Edit[ModelName].php +โ”œโ”€โ”€ Http/ +โ”‚ โ”œโ”€โ”€ Controllers/ +โ”‚ โ”‚ โ””โ”€โ”€ [ModuleName]Controller.php +โ”‚ โ”œโ”€โ”€ Middleware/ +โ”‚ โ””โ”€โ”€ Requests/ +โ”‚ โ””โ”€โ”€ [ModelName]Request.php +โ”œโ”€โ”€ Models/ +โ”‚ โ””โ”€โ”€ [ModelName].php +โ”œโ”€โ”€ Policies/ +โ”‚ โ””โ”€โ”€ [ModelName]Policy.php +โ”œโ”€โ”€ Services/ +โ”‚ โ””โ”€โ”€ [ModelName]Service.php +โ”œโ”€โ”€ Routes/ +โ”‚ โ”œโ”€โ”€ web.php +โ”‚ โ””โ”€โ”€ api.php +โ”œโ”€โ”€ Resources/ +โ”‚ โ””โ”€โ”€ views/ +โ”‚ โ”œโ”€โ”€ index.blade.php +โ”‚ โ””โ”€โ”€ layouts/ +โ”‚ โ””โ”€โ”€ module.blade.php +โ”œโ”€โ”€ Permissions.php +โ””โ”€โ”€ [ModuleName]ServiceProvider.php +``` + +--- + +## ๐Ÿ“ EXACT FILE TEMPLATES + +### 1. Service Provider (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/[ModuleName]ServiceProvider.php` + +```php +mergeConfigFrom( + __DIR__ . '/Config/[module_name].php', + '[module_name]' + ); + } + + public function boot(): void + { + $this->loadMigrationsFrom(__DIR__ . '/Database/Migrations'); + $this->loadViewsFrom(__DIR__ . '/Resources/views', '[module-slug]'); + + $this->registerRoutes(); + } + + protected function registerRoutes(): void + { + Route::middleware(['web', 'auth']) + ->prefix('[module-slug]') + ->name('[module-slug].') + ->group(__DIR__ . '/Routes/web.php'); + + if (file_exists(__DIR__ . '/Routes/api.php')) { + Route::middleware(['api', 'auth:sanctum']) + ->prefix('api/[module-slug]') + ->name('api.[module-slug].') + ->group(__DIR__ . '/Routes/api.php'); + } + } +} +``` + +**NAMING RULES**: +- `[ModuleName]` = PascalCase (e.g., `StockManagement`) +- `[module_name]` = snake_case (e.g., `stock_management`) +- `[module-slug]` = kebab-case (e.g., `stock-management`) + +### 2. Config File (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/Config/[module_name].php` + +```php + '[Module Display Name]', + 'slug' => '[module-slug]', + 'version' => '1.0.0', + + 'audit' => [ + 'enabled' => true, + 'strategy' => 'all', // 'all', 'include', 'exclude', 'none' + 'include' => [], + 'exclude' => [], + ], +]; +``` + +### 3. Model (REQUIRED for data modules) + +**File**: `src/app/Modules/[ModuleName]/Models/[ModelName].php` + +```php +id(); + // Define ALL columns here + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('[table_name]'); + } +}; +``` + +### 5. Permissions (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/Permissions.php` + +```php + 'View [Module Name]', + '[module_name].create' => 'Create [Module Name] records', + '[module_name].edit' => 'Edit [Module Name] records', + '[module_name].delete' => 'Delete [Module Name] records', +]; +``` + +### 6. Filament Resource (REQUIRED for admin) + +**File**: `src/app/Modules/[ModuleName]/Filament/Resources/[ModelName]Resource.php` + +```php +user()?->can('[module_name].view') ?? false; + } + + public static function form(Form $form): Form + { + return $form + ->schema([ + Forms\Components\Section::make('[Model Name] Details') + ->schema([ + // Add form fields here + ]), + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + // Add table columns here + ]) + ->filters([ + // + ]) + ->actions([ + Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make(), + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\List[ModelName]s::route('/'), + 'create' => Pages\Create[ModelName]::route('/create'), + 'edit' => Pages\Edit[ModelName]::route('/{record}/edit'), + ]; + } +} +``` + +### 7. Filament Resource Pages (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/Filament/Resources/[ModelName]Resource/Pages/List[ModelName]s.php` +```php +name('index'); +``` + +**File**: `src/app/Modules/[ModuleName]/Routes/api.php` (if API needed) +```php +group(function () { + // API routes here +}); +``` + +### 9. Controller (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/Http/Controllers/[ModuleName]Controller.php` +```php +authorize('[module_name].view'); + + return view('[module-slug]::index'); + } +} +``` + +### 10. Index View (REQUIRED) + +**File**: `src/app/Modules/[ModuleName]/Resources/views/index.blade.php` +```blade + + +

+ {{ __('[Module Display Name]') }} +

+
+ +
+
+
+
+ {{ __('[Module Display Name] content goes here.') }} +
+
+
+
+
+``` + +--- + +## โœ… MODULE REGISTRATION + +After creating all files, register the module in `src/config/app.php` providers array: + +```php +'providers' => ServiceProvider::defaultProviders()->merge([ + // ...existing providers... + App\Modules\[ModuleName]\[ModuleName]ServiceProvider::class, +])->toArray(), +``` + +Or add to `src/bootstrap/providers.php`: +```php +return [ + App\Providers\AppServiceProvider::class, + // ...existing providers... + App\Modules\[ModuleName]\[ModuleName]ServiceProvider::class, +]; +``` + +--- + +## ๐Ÿ”„ AFTER MODULE CREATION - REQUIRED COMMANDS + +```bash +# 1. Run migrations +docker-compose exec app php artisan migrate + +# 2. Clear all caches +docker-compose exec app php artisan optimize:clear + +# 3. Seed permissions (if using permissions) +docker-compose exec app php artisan db:seed --class=RolePermissionSeeder + +# 4. Reset permission cache +docker-compose exec app php artisan permission:cache-reset +``` + +--- + +## โš ๏ธ COMMON MISTAKES TO AVOID + +### 1. Wrong Namespace +โŒ `namespace App\Modules\StockManagement\Models;` +โœ… `namespace App\Modules\StockManagement\Models;` + +The namespace MUST match the folder structure EXACTLY. + +### 2. Wrong View Namespace +โŒ `return view('stockmanagement::index');` +โŒ `return view('StockManagement::index');` +โœ… `return view('stock-management::index');` + +View namespace is ALWAYS kebab-case. + +### 3. Wrong Permission Names +โŒ `'StockManagement.view'` +โŒ `'stock-management.view'` +โœ… `'stock_management.view'` + +Permissions are ALWAYS snake_case. + +### 4. Forgetting to Register Provider +The module WILL NOT LOAD if you forget to add its ServiceProvider. + +### 5. Wrong Import Paths +โŒ `use App\Models\Product;` +โœ… `use App\Modules\StockManagement\Models\Product;` + +Module models are in the MODULE namespace, not the core App namespace. + +### 6. Missing Fillable Array +โŒ Empty `$fillable` array causes mass-assignment errors +โœ… List ALL fields that should be mass-assignable + +### 7. Forgetting to Run Migrations +Always run `php artisan migrate` after creating migrations. + +--- + +## ๐ŸŽจ FILAMENT FORM FIELD REFERENCE + +```php +// Text +Forms\Components\TextInput::make('name') + ->required() + ->maxLength(255); + +// Email +Forms\Components\TextInput::make('email') + ->email() + ->required(); + +// Password +Forms\Components\TextInput::make('password') + ->password() + ->required(); + +// Textarea +Forms\Components\Textarea::make('description') + ->rows(3); + +// Select +Forms\Components\Select::make('status') + ->options([ + 'active' => 'Active', + 'inactive' => 'Inactive', + ]) + ->required(); + +// Checkbox +Forms\Components\Toggle::make('is_active') + ->default(true); + +// Date +Forms\Components\DatePicker::make('date'); + +// DateTime +Forms\Components\DateTimePicker::make('published_at'); + +// Number +Forms\Components\TextInput::make('price') + ->numeric() + ->prefix('$'); + +// File Upload +Forms\Components\FileUpload::make('image') + ->image() + ->directory('uploads'); + +// Relationship Select +Forms\Components\Select::make('category_id') + ->relationship('category', 'name') + ->searchable() + ->preload(); +``` + +--- + +## ๐Ÿ“Š FILAMENT TABLE COLUMN REFERENCE + +```php +// Text +Tables\Columns\TextColumn::make('name') + ->searchable() + ->sortable(); + +// Badge +Tables\Columns\TextColumn::make('status') + ->badge() + ->color(fn (string $state): string => match ($state) { + 'active' => 'success', + 'inactive' => 'danger', + default => 'gray', + }); + +// Boolean Icon +Tables\Columns\IconColumn::make('is_active') + ->boolean(); + +// Image +Tables\Columns\ImageColumn::make('avatar') + ->circular(); + +// Date +Tables\Columns\TextColumn::make('created_at') + ->dateTime() + ->sortable(); + +// Money +Tables\Columns\TextColumn::make('price') + ->money('USD'); +``` + +--- + +## ๐Ÿ” EXISTING CORE RESOURCES + +### User Management (already exists in template) +- `src/app/Filament/Resources/UserResource.php` - User CRUD with role assignment +- `src/app/Filament/Resources/RoleResource.php` - Role CRUD with permissions +- `src/app/Filament/Resources/PermissionResource.php` - Permission CRUD + +### Settings (already exists) +- `src/app/Filament/Pages/Settings.php` - Site settings (name, logo, colors, registration toggle) +- `src/app/Models/Setting.php` - Settings model with get/set helpers + +### Authentication (pre-installed) +- Laravel Breeze with Blade templates +- Login: `/login` +- Register: `/register` (controlled by enable_registration setting) +- Admin: `/admin` + +--- + +## ๐Ÿงช TESTING MODULES + +Create tests in `src/tests/Feature/Modules/[ModuleName]/`: + +```php +create(); + $user->givePermissionTo('[module_name].view'); + + $this->actingAs($user) + ->get('/[module-slug]') + ->assertStatus(200); + } +} +``` + +--- + +## ๐Ÿ“‹ CHECKLIST FOR NEW MODULES + +Before considering a module complete: + +- [ ] ServiceProvider created and registered +- [ ] Config file created +- [ ] Model created with fillable and casts +- [ ] Migration created and run +- [ ] Permissions.php created +- [ ] Filament Resource created with all pages +- [ ] Controller created +- [ ] Routes (web.php and optionally api.php) created +- [ ] Index view created +- [ ] `php artisan migrate` run +- [ ] `php artisan optimize:clear` run +- [ ] `php artisan db:seed --class=RolePermissionSeeder` run +- [ ] Module accessible at `/[module-slug]` +- [ ] Admin panel shows module in navigation +- [ ] CRUD operations work in admin + +--- + +## ๐Ÿ†˜ DEBUGGING + +### Module Not Loading +```bash +# Check if provider is registered +docker-compose exec app php artisan about + +# Clear everything +docker-compose exec app php artisan optimize:clear +``` + +### Filament Resource Not Showing +```bash +# Clear Filament cache +docker-compose exec app php artisan filament:cache-components + +# Check canAccess() method returns true +# Check user has required permission +``` + +### Permission Denied +```bash +# Reset permission cache +docker-compose exec app php artisan permission:cache-reset + +# Verify permissions exist +docker-compose exec app php artisan tinker +>>> \Spatie\Permission\Models\Permission::pluck('name'); +``` + +### View Not Found +```bash +# Verify view namespace (must be kebab-case) +# Check file exists in Resources/views/ +``` + +--- + +**Remember**: When in doubt, look at existing code in the codebase. The patterns are already established - follow them exactly. diff --git a/README.md b/README.md index d505d6e..83986c0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ A comprehensive Laravel development environment with Docker for local developmen > **New here?** Start with [GETTING_STARTED.md](GETTING_STARTED.md) for a step-by-step setup guide. > -> **AI Assistant?** See [AI_CONTEXT.md](AI_CONTEXT.md) for project context and conventions. +> **AI Assistant?** See [CLAUDE.md](CLAUDE.md) for EXACT module templates and step-by-step instructions. +> Also see [AI_CONTEXT.md](AI_CONTEXT.md) for project context and conventions. ## Architecture Overview