Module 8: Backend API (PHP)

This module covers the creation of the REST API server-side: routing, controllers, mappers, and integration with Dolibarr.

Prerequisites

  • PHP and Dolibarr knowledge
  • Modules 5-7 completed (frontend architecture and hooks)

Backend Structure

monmodule/
├── pwa/
│   └── api.php                    # API entry point
├── smartmaker-api/
│   ├── Controllers/               # Business logic
│   │   └── ItemController.php
│   └── Mappers/                   # Dolibarr -> JSON conversion
│       └── dmItem.php
└── smartmaker-api-prepend.php     # Bootstrap

Chapters

# Chapter Content
1 Routing Configure routes in api.php
2 Controllers Business logic and CRUD
3 Mappers Convert Dolibarr to React
4 Extrafields Dynamic forms and custom fields

Module Objectives

By the end of this module, you will know how to:

  • Configure API routes
  • Create controllers with complete CRUD
  • Map Dolibarr objects to JSON
  • Handle JWT authentication
  • Use extrafields

Request Flow

1. React: api.private.get('items/123')
           ↓
2. HTTP: GET /pwa/api.php?route=items/123
           ↓
3. api.php: Route to ItemController::show
           ↓
4. Controller: Load Dolibarr object
           ↓
5. Mapper: Convert to JSON
           ↓
6. api.php: Return JSON with HTTP code

SmartAuth

JWT authentication is handled by SmartAuth:

  • Automatic token validation
  • Access to logged Dolibarr user
  • Automatic token refresh

Start: Routing ->