How perezcontrerasluis/apiproject works
1. What this repo is
- A containerized PHP 8.2 REST API built without a framework.
- The application uses a classic layered design: HTTP entrypoint, routing, middleware, controllers, services, repositories, and simple domain/DTO classes.
- The current PHP application surface in this tree is centered on authentication and categories; the database schema also includes users, products, suppliers, and inventory movements.
2. HTTP entrypoint and web boundary
- Requests enter through public/index.php, which acts as the front controller.
- public/.htaccess supports Apache URL rewriting so all API routes are funneled into the front controller instead of hitting PHP files directly.
- This is the main boundary between the web server and the application code.
3. Routing and request dispatch
- Route definitions live in src/routes.php.
- src/Core/Router.php is responsible for matching the incoming path/method to a controller action.
- src/Core/Request.php and src/Core/Response.php likely wrap request parsing and JSON response formatting, keeping HTTP concerns out of business logic.
4. Middleware pipeline
- src/Core/Middleware.php provides the pipeline mechanism.
- Concrete middleware lives in src/Middleware/:
- CorsMiddleware.php handles cross-origin policy.
- AuthMiddleware.php protects authenticated endpoints via JWT validation.
- This sits between routing and controller execution, so cross-cutting concerns are enforced consistently.
5. Controller layer
- Controllers are the HTTP-facing adaptors: src/Controllers/AuthController.php and src/Controllers/CategoryController.php.
- They translate request input into service calls and return structured responses.
- Controllers do not appear to contain persistence logic; they delegate that to services.
6. Service layer
- Business rules are concentrated in src/Services/AuthService.php and src/Services/CategoryService.php.
- This layer coordinates validation, token generation, data shaping, and repository access.
- It is the main orchestration layer between HTTP and storage.
7. Persistence layer
- Data access is isolated in src/Repositories/UserRepository.php and src/Repositories/CategoryRepository.php.
- Repositories are the only layer that should talk directly to the database abstraction.
- src/Core/Database.php is the shared DB connection/abstraction used underneath.
8. Auth and token flow
- src/Core/JwtService.php encapsulates JWT creation and verification.
- Login follows the path: controller → auth service → user repository → JWT service → response.
- Protected requests rely on AuthMiddleware to validate the bearer token before the controller runs.
9. Domain objects and response shaping
- Models in src/Models/ represent core entities such as User and Category.
- DTOs in src/DTOs/ (UserDTO, CategoryDTO, CategoryCollectionDTO) are used to return stable, API-friendly shapes instead of raw database rows.
- This separation helps keep the API contract independent from schema details.
10. Manual dependency wiring
- src/Factories/RepositoryFactory.php and src/Factories/ServiceFactory.php centralize object creation.
- This is the repo’s lightweight dependency injection boundary: controllers and middleware should receive ready-made services, not construct them ad hoc.
- It keeps the architecture decoupled without bringing in a framework container.
11. Database structure and lifecycle
- Schema creation lives in database/migrations/*.sql.
- Initial data lives in database/seeders/*.sql.
- The migration set covers five tables: users, categories, suppliers, products, and inventory movements.
- cli/setup.php is the operational bootstrap for applying migrations and seed data.
12. Infrastructure and runtime
- docker-compose.yml defines the runtime stack, with PHP/Apache and MySQL running in containers.
- docker/php/Dockerfile, entrypoint.sh, and php.ini define the PHP runtime behavior.
- The application is designed to run consistently across dev/testing/prod-like environments through Docker rather than local host installs.
13. Testing and operational tooling
- postman/ contains a Postman collection and environment for exercising the API.
- The README documents example endpoints for categories and auth, plus JWT-based testing credentials.
- These assets are not part of runtime architecture, but they define how the API is validated and consumed during development.
14. Overall request/data flow
- The main flow is: browser/client → Apache/front controller → router → middleware → controller → service → repository → database → DTO/response → client.
- The main trust boundary is JWT-authenticated access, enforced before protected controller actions.
- The main architectural separation is between HTTP concerns, business logic, and persistence, with the database and Docker stack kept outside the application core.
Main entry points: AuthService.php Auth service · CategoryService.php Category service · index.php Front controller · Router.php Router
Components 31
Web boundary
HTTP entrypoint and request routing into the API
- Front controller · Main HTTP entrypoint for the API
public/index.php - Apache rewrite · Routes all requests into the front controller
public/.htaccess
Application core
Routing, middleware, controllers, services, and factories
- Routes · Application route definitions
src/routes.php - Router · Matches HTTP requests to controller actions
src/Core/Router.php - Request · Encapsulates incoming request data
src/Core/Request.php - Response · Formats outgoing HTTP responses
src/Core/Response.php - Pipeline · Executes middleware around request handling
src/Core/Middleware.php - CORS · Applies cross-origin response policy
src/Middleware/CorsMiddleware.php - Auth guard · Validates JWT bearer access for protected routes
src/Middleware/AuthMiddleware.php - Auth controller · HTTP adaptor for authentication actions
src/Controllers/AuthController.php - Category controller · HTTP adaptor for category endpoints
src/Controllers/CategoryController.php - Auth service · Coordinates login, token creation, and user lookup
src/Services/AuthService.php - Category service · Coordinates category business rules and shaping
src/Services/CategoryService.php - Service factory · Builds service instances for the app
src/Factories/ServiceFactory.php - Repository factory · Builds repository instances for the app
src/Factories/RepositoryFactory.php
Domain and data
Models, DTOs, repositories, and shared database access
- User repo · Database access for users
src/Repositories/UserRepository.php - Category repo · Database access for categories
src/Repositories/CategoryRepository.php - Database · Shared database connection and access layer
src/Core/Database.php - JWT service · Creates and verifies JSON Web Tokens
src/Core/JwtService.php - User model · User entity representation
src/Models/User.php - Category model · Category entity representation
src/Models/Category.php - User DTO · Stable API shape for user data
src/DTOs/UserDTO.php - Category DTO · Stable API shape for category data
src/DTOs/CategoryDTO.php - Category list DTO · Stable API shape for category collections
src/DTOs/CategoryCollectionDTO.php
Runtime and setup
Container runtime, migrations, seeders, and bootstrap tooling
- Compose stack · Defines the PHP and MySQL container runtime
docker-compose.yml - PHP image · Builds the PHP/Apache runtime image
docker/php/Dockerfile - Entrypoint · Container startup script
docker/php/entrypoint.sh - PHP config · PHP runtime tuning and settings
docker/php/php.ini - Setup CLI · Applies migrations and seed data
cli/setup.php - Migrations · SQL files that create the database schema
database/migrations/001_create_users_table.sql - Seed data · SQL files that load initial data
database/seeders/001_seed_users.sql
Connections 31
- Apache rewrite → Front controller: rewrites to · Apache funnels API requests into the front controller
- Front controller → Routes: loads · Bootstraps route definitions
- Front controller → Router: dispatches via · Front controller hands requests to the router
- Front controller → Request: wraps · Creates request abstraction from incoming HTTP data
- Router → Pipeline: passes through · Routes execute through the middleware pipeline
- Pipeline → CORS: includes · Applies cross-origin middleware
- Pipeline → Auth guard: includes · Applies authentication middleware where required
- Router → Auth controller: invokes · Resolves auth endpoints to controller actions
- Router → Category controller: invokes · Resolves category endpoints to controller actions
- Auth guard → JWT service: verifies · Checks bearer tokens before protected actions
- Auth controller → Auth service: delegates to · Controller forwards auth work to the service layer
- Category controller → Category service: delegates to · Controller forwards category work to the service layer
- Service factory → Auth service: builds · Provides configured auth service instances
- Service factory → Category service: builds · Provides configured category service instances
- Repository factory → User repo: builds · Provides user repository instances
- Repository factory → Category repo: builds · Provides category repository instances
- Auth service → User repo: reads from · Auth depends on user lookup for login
- Auth service → JWT service: issues · Creates tokens after successful authentication
- Category service → Category repo: reads from · Category operations depend on repository access
- User repo → Database: uses · Repository talks to the shared database layer
- Category repo → Database: uses · Repository talks to the shared database layer
- Auth service → User model: uses · Maps auth-related user data into domain objects
- Category service → Category model: uses · Works with category domain objects
- Auth service → User DTO: returns · Shapes user-facing auth responses
- Category service → Category DTO: returns · Shapes single-category responses
- Category service → Category list DTO: returns · Shapes category collection responses
- Setup CLI → Migrations: applies · Bootstrap process runs schema migrations
- Setup CLI → Seed data: loads · Bootstrap process loads initial seed data
- Compose stack → PHP image: builds with · Compose stack uses the PHP image build
- Compose stack → Entrypoint: starts with · Container startup is controlled by the entrypoint script
- PHP image → PHP config: configures · Image includes PHP runtime settings