ttpryg/slim-api-starter

There is no license information available for the latest version (0.0.1) of this package.

Slim 4 API Starter with PHP 8.2 and Docker

Maintainers

Package info

github.com/ttpryg/slim-api-starter

Type:project

pkg:composer/ttpryg/slim-api-starter

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

0.0.1 2026-04-23 15:48 UTC

This package is auto-updated.

Last update: 2026-04-27 02:21:24 UTC


README

A robust starter project using Slim Framework 4 with PHP 8.2, PHP-DI, Eloquent ORM, and Docker.

Folder Structure

.
├── app/                # Main application source code
│   ├── Action/         # API Actions (ADR Pattern)
│   ├── Commands/       # CLI Commands (Symfony Console)
│   ├── Model/          # Eloquent Models
│   └── Traits/         # Reusable Traits (e.g., ResponseTrait)
├── config/             # Configuration (Routes, Container, Settings, DB)
├── db/                 # Database Migrations & Seeds (Phinx)
├── public/             # Document root (Entry point index.php)
├── storage/            # Local storage (Logs, Caches, etc.)
│   └── logs/           # Application log files
├── tests/              # Automated testing (PHPUnit)
├── slim                # Executable CLI tool (Symfony Console)
├── Dockerfile          # PHP 8.2-FPM image configuration
├── docker-compose.yml  # Orchestration App & Web Server (Nginx)
└── nginx.conf          # Nginx Configuration

Key Features & Technologies

  • PHP 8.2
  • Slim Framework 4 & Slim PSR-7
  • PHP-DI 7 (Dependency Injection)
  • Eloquent ORM (Database Management)
  • Phinx (Database Migrations)
  • Symfony Console (Custom CLI Generator)
  • Monolog (File-based Error Logging)
  • Laravel Pint (Code Styling & Formatting)
  • PHPUnit (Testing)
  • Docker & Nginx

How to Run

  1. Clone/Download this project.
  2. Start the container with Docker Compose:
    docker compose up -d --build
  3. Install dependencies using composer (inside the container):
    docker exec -it slim_app composer install
  4. Access the API at URL: http://localhost:8080

Application Logging

This starter comes pre-configured with Monolog for error and debug logging.

  • Any unhandled exceptions or internal Slim errors will be automatically logged to: storage/logs/app.log
  • You can inject Psr\Log\LoggerInterface into your actions to log custom messages manually:
    public function __construct(private \Psr\Log\LoggerInterface $logger) {}
    
    public function __invoke(...) {
        $this->logger->info("This is a custom log entry");
    }

CLI Tool (Slim API Starter)

This project has a built-in CLI (./slim) to help accelerate the development process. All CLI commands can be executed inside the container:

docker exec -it slim_app php slim list

Generator Commands

  • Make a New Action: Generates an Action class in PascalCase format and automatically uses ResponseTrait.
    php slim make:action User/LoginAction
  • Make a New Model: Generates an Eloquent Model with the appropriate format.
    php slim make:model User
  • Make a New Migration: Generates a Phinx migration class.
    php slim make:migration CreateUsersTable

Formatting & Code Styling

This project is integrated with Laravel Pint to maintain code neatness (PSR-12/Laravel Style).

  • Check Code Style: composer style-check
  • Fix Code Style Automatically: composer style-fix

Testing

All test files are placed in the tests/ directory and tested using PHPUnit.

docker exec -it slim_app composer test

Architectural Notes

ADR Pattern (Action-Domain-Response)

This project uses the ADR pattern as an alternative to conventional MVC for API endpoints:

  • Each class in app/Action acts independently with Single Responsibility.
  • Classes are implemented as invokables (using the __invoke() magic method) so they can be routed dynamically by Slim.
  • Output is standardized using ResponseTrait which centralizes JSON payload creation ($this->success() and $this->error() methods).

Common Container Commands

  • View App Logs (Docker): docker compose logs -f app
  • Enter Container Shell: docker exec -it slim_app bash
  • Update Composer: docker exec -it slim_app composer update
  • Refresh Autoloader: docker exec -it slim_app composer dump-autoload