byjg/resttemplate

This package is abandoned and no longer maintained. The author suggests using the byjg/rest-reference-architecture package instead.

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

6.1 2026-02-22 00:40 UTC

This package is auto-updated.

Last update: 2026-02-22 00:45:49 UTC


README

Sponsor Build Status Opensource ByJG GitHub source GitHub license GitHub release

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 โ€” FakeApiRequester runs the full API stack inside PHPUnit, no web server needed
  • ๐Ÿณ Docker ready โ€” MySQL, PHP-FPM, and Nginx pre-configured; docker compose up -d and 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

  1. Installation & Setup โ€“ Install the template, configure environments, and review prerequisites.
  2. Create Your First Table โ€“ Define your first migration and schema.
  3. Add Fields โ€“ Safely evolve existing tables.
  4. Create REST Endpoints โ€“ Generate REST handlers from your tables.
  5. Windows Setup โ€“ WSL/Windows-specific checklist.
  6. 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

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.

Dependencies

๐Ÿ“š Complete Component Dependency Graph โ†’

Open source ByJG