Backend Documentation
Welcome to the CPR Backend documentation. This guide covers the Laravel 12 REST API that powers the Clinical Patient Records system.
Quick Start
bash
# Clone and install
git clone <repo-url>
cd cpr-backend
composer install
# Environment setup
cp .env.example .env
php artisan key:generate
# Database
php artisan migrate
php artisan db:seed
# Run development servers (Laravel + Queue + Logs + Vite)
composer devThe API will be available at http://localhost:8000/api/v1.
Using Docker (Laravel Sail)
bash
./vendor/bin/sail up -d
./vendor/bin/sail artisan migrate
./vendor/bin/sail artisan db:seedServices: Laravel (port 80), MySQL 8.4 (port 3306), Redis 7 (port 6379), Mailhog (port 8025).
Project Structure
cpr-backend/
├── app/
│ ├── Actions/ # Single-responsibility command classes
│ ├── Contracts/ # Repository interfaces
│ ├── Dtos/ # Data Transfer Objects
│ ├── Enums/ # PHP enums (status types, etc.)
│ ├── Exceptions/ # Custom exceptions
│ ├── Helpers/ # Utility functions (CodeGenerator)
│ ├── Http/
│ │ ├── Controllers/Api/V1/ # API v1 controllers
│ │ ├── Middleware/ # Auth, branch context, etc.
│ │ ├── Requests/ # Form request validation
│ │ └── Resources/ # API response transformers
│ ├── Models/ # Eloquent models (63 models)
│ ├── Pipelines/ # Processing chains (patient, auth)
│ ├── Providers/ # Service & repository bindings
│ ├── Repositories/ # Data access layer (46 repos)
│ ├── Services/ # Business logic (41 services)
│ └── Traits/ # Reusable traits
├── config/ # Laravel configuration
├── database/
│ ├── factories/ # Model factories (56)
│ ├── migrations/ # Schema migrations (114)
│ └── seeders/ # Data seeders (38)
├── routes/
│ ├── api.php # Main API routes
│ └── api/v1/ # Versioned route files (43 files)
├── tests/
│ ├── Feature/ # Integration tests (170+)
│ ├── Traits/ # Test helpers
│ └── Pest.php # Pest configuration
├── compose.yaml # Docker Compose
├── phpunit.xml # Test configuration
└── composer.json # Dependencies & scriptsAvailable Commands
| Command | Description |
|---|---|
composer dev | Start all dev servers (Laravel, Queue, Logs, Vite) |
composer test | Run tests in parallel (10 processes) |
composer stan | Run PHPStan static analysis |
composer pint | Run Laravel Pint code formatter |
composer setup | Full setup (install, migrate, build) |
Documentation Sections
Architecture & Standards
- File Structure - Directory layout and organization
- API Design - RESTful conventions and patterns
- Error Handling - Exception handling and error responses
- Logging - Application and audit logging
Guidelines
- Coding Standards - PSR compliance and formatting
- Best Practices - Architectural patterns in use
- Naming Conventions - File, class, and method naming
- Code Review - Review checklist
API
- API Overview - Endpoints, request/response format
- Authentication - Sanctum token auth
- Versioning - API version strategy
- Pagination - Paginated responses
- Rate Limiting - Throttling configuration
Database
- Migrations - Schema management
- Seeders - Test data and initial setup
- Relationships - Eloquent model relationships
- Query Optimization - Performance tips
Security
- Overview - Security architecture
- Authentication - Sanctum setup
- Authorization - Roles and permissions (Spatie)
- CSRF Protection - CSRF handling
- SQL Injection - Prevention techniques
Testing
- Overview - Testing strategy
- Unit Testing - Unit test patterns
- Feature Testing - Integration tests
- API Testing - Endpoint testing
Performance
- Caching - Redis and application caching
- Optimization - General optimization
- Eager Loading - N+1 prevention
- Profiling - Debugging tools
Tooling
- Composer - Dependency management
- PHPStan - Static analysis
- Laravel Pint - Code formatting
- Artisan - CLI commands
- Pest - Testing framework
- Scramble - API docs generator
- Sentry - Error tracking
Workflow
- Development Process - Day-to-day workflow
- Git Workflow - Branching strategy
- Deployment - Deployment process
- Release Process - Release management