byjg / resttemplate
Production-ready PHP REST API boilerplate that lets you focus on building your business logic, not the infrastructure.
Installs: 113
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 2
Forks: 6
Open Issues: 1
pkg:composer/byjg/resttemplate
Requires
- php: >=8.3 <8.6
- ext-curl: *
- ext-json: *
- ext-openssl: *
- byjg/anydataset-db: ^6.0
- byjg/authuser: ^6.0
- byjg/config: ^6.0
- byjg/jinja-php: ^6.0
- byjg/mailwrapper: ^6.0
- byjg/micro-orm: ^6.0
- byjg/migration: ^6.0
- byjg/restserver: ^6.0
- byjg/scriptify: ^6.0
- byjg/shortid: ^6.0
- byjg/swagger-test: ^6.0
- zircote/swagger-php: ^4.7|^5.2
Requires (Dev)
- phpunit/phpunit: ^10.5|^11.5
- vimeo/psalm: ^5.9|^6.13
README
Production-ready PHP REST API boilerplate that lets you focus on building your business logic, not the infrastructure.
Why Use This?
Every new REST API needs the same boilerplate: authentication, migrations, an ORM, OpenAPI docs, a test harness, and a DI container. Setting all of that up correctly takes days โ and it's not the work your users care about.
This template does the wiring once, correctly, so you start on day one writing business logic instead of plumbing. And because it's a template you own โ not a framework you depend on โ you can change, remove, or replace any part of it without asking permission.
Quick Start
# Create your project composer -sdev create-project byjg/rest-reference-architecture my-api ^6.1 # Start containers cd my-api docker compose up -d # Run migrations composer migrate -- --env=dev reset # Your API is ready! curl http://localhost:8080/sample/ping
๐ Complete Getting Started Guide โ
Architecture Overview
mindmap
(("Reference Architecture"))
("PSR Standards")
("WebRequests")
("Container & Dependency Injection")
("Cache")
("Authentication & Authorization")
("Decoupled Code")
("Database")
("ORM Integration")
("Migration")
("OpenAPI Integration")
("Routing")
("Rest Methods")
("Contract Testing")
("Documentation")
("Error Handling")
Loading
Key Features
- ๐ Code generator โ one command scaffolds Model, Repository, Service, REST controller, and tests from any database table
- ๐๏ธ Two patterns โ choose Repository (DI + Service layer) or ActiveRecord per entity; mix them in the same project
- ๐ Auth out of the box โ JWT login, token refresh, password reset, and role-based access control (RBAC) included
- ๐ OpenAPI-first โ routes are driven by
openapi.json; Swagger UI, contract testing, and docs stay in sync automatically - ๐๏ธ Database migrations โ versioned up/down SQL migrations with a one-command runner and ORM integration
- ๐งช In-process testing โ
FakeApiRequesterruns the full API stack inside PHPUnit, no web server needed - ๐ณ Docker ready โ MySQL, PHP-FPM, and Nginx pre-configured;
docker compose up -dand you're running - โ๏ธ PSR standards โ PSR-7 (HTTP messages), PSR-11 (container), PSR-6/16 (cache)
# Generate a complete CRUD API from a single table
composer codegen -- --env=dev --table=products all --save
Documentation
Getting Started
- Installation & Setup โ Install the template, configure environments, and review prerequisites.
- Create Your First Table โ Define your first migration and schema.
- Add Fields โ Safely evolve existing tables.
- Create REST Endpoints โ Generate REST handlers from your tables.
- Windows Setup โ WSL/Windows-specific checklist.
- Unattended Setup โ Automate installs for CI/CD pipelines.
Guides
- REST Controllers โ Define routes with PHP attributes; keep controllers thin.
- Authentication โ Configure JWT login flows and RBAC enforcement.
- Database Migrations โ Version and run schema migrations in every environment.
- ORM โ Use MicroORM for repository and ActiveRecord patterns.
- Service Layer โ Organize business logic, orchestration, and transaction boundaries.
- Repository Patterns โ Implement complex queries, UUID handling, and filtering helpers.
- Template Customization โ Tailor the generator templates to match your coding standards.
- Testing โ Unit, integration, and contract testing with
FakeApiRequester. - JWT Authentication Advanced โ Extend tokens with custom claims and refresh logic.
- Error Handling โ Map exceptions to HTTP responses and logging patterns.
- Configuration โ Layer configurations, secrets, and environment overrides.
Concepts
- Architecture โ Architectural decisions: when to use Repository vs ActiveRecord.
- OpenAPI Integration โ How swagger-php, the spec file, and Swagger UI fit together.
- Dependency Injection โ PSR-11 container, environment hierarchy, and DI binding patterns.
- Request Lifecycle โ Trace an HTTP request from entry point to JSON response.
Reference
- Code Generator โ Automate models, repositories, services, controllers, and tests.
- Attributes โ
RequireAuthenticated,RequireRole,ValidateRequest, and custom attributes. - Traits โ Timestamp and soft-delete helpers for models.
- Scriptify โ REPL, CLI runner, and service manager utilities.
- Components โ Full PHP component dependency graph.
Real-World Example
# 1. Create database table cat > db/migrations/up/00002-create-products.sql << 'EOF' CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); EOF # 2. Run migration composer migrate -- --env=dev update # 3. Generate all code composer codegen -- --env=dev --table=products all --save # 4. Generate the OpenAPI spec so routing is active composer run openapi # 5. Log in and capture the token TOKEN=$(curl -s -X POST http://localhost:8080/login \ -H "Content-Type: application/json" \ -d '{"username":"admin@example.com","password":"!P4ssw0rdstr!"}' \ | jq -r '.token') # 6. Call your new endpoint curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8080/products | jq
You just created a complete CRUD API with:
- โ Model with validation
- โ Repository for data access
- โ Service for business logic
- โ REST controller with GET, POST, PUT endpoints
- โ Functional tests
- โ OpenAPI documentation
- โ JWT authentication
Requirements
- PHP 8.3+ (8.5 recommended)
- Docker & Docker Compose (optional but recommended)
- Composer
- Git
Support & Community
- ๐ Full Documentation
- ๐ Report Issues
- ๐ก Request Features
- ๐ ByJG Open Source
Not a Framework
This is a template, not a framework. You own the code:
- โ Full control over every file
- โ No vendor lock-in
- โ Customize anything you need
- โ Remove what you don't need
License
This project is open source. See LICENSE for details.