samaphp/microapp

A minimal PHP microframework router.

0.7.1 2025-05-01 13:04 UTC

This package is auto-updated.

Last update: 2025-05-06 08:50:40 UTC


README

MicroApp is a minimal PHP 7.4+ microframework for building super-microservices with clean routing and zero dependencies. Perfect for fast bootstraps, tiny APIs, internal tools, or focused endpoints where simplicity wins. Designed for developers who value clarity, control, zero framework overhead and long-term maintainability. Built for microservices that can live for decades without requiring upgrades or major refactors. You’re encouraged to follow future-proof design principles when building on top of it.

🌟 Features

  • βœ… GET, POST, PUT, DELETE, and PATCH method support
  • βœ… Named route parameters like /user/{id}
  • βœ… JSON response helper: MicroApp::json(...)
  • βœ… PSR-4 structure with Composer autoloading
  • βœ… Simple and readable one-file implementation
  • βœ… Ready to be used as a Composer package
  • βœ… Auto-discovery of controller classes with route definitions inside the class itself
  • βœ… Optional CLI available via the microapp-dev package

πŸš€ Getting Started

  • Install via Composer: composer require samaphp/microapp
  • You can set things up manually (see sections below) or automate it using the microapp-dev package from the Developer Tools section.

πŸ› οΈ Developer Tools

Looking to scaffold .htaccess, index.php, or generate controllers?
Use the official companion package:
➑️ samaphp/microapp-dev

πŸ”€ basePath Support

If your application is served from a subdirectory (e.g., example.com/myapp/), you can pass the base path to MicroApp during initialization:

$app = new MicroApp('/myapp');

This ensures all routes are matched correctly regardless of where your app is hosted.

πŸ“Œ You must also update your .htaccess rewrite rule to reflect the same subdirectory.

🟦 .htaccess example

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Enable this to support subdirectory installations after injecting basePath value to MicroApp('basePathHere')
  #RewriteBase /basePathHere/

  # Redirect everything except existing files and directories to index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [QSA,L]
</IfModule>

🟦 index.php Example

The auto generated index.php will be like this:

<?php
require __DIR__ . '/vendor/autoload.php';

use MicroApp\MicroApp;

$app = new MicroApp();
$app->loadRoutesFrom(__DIR__ . '/src/Controller', 'App\\Controller');
$app->dispatch();

🟦 Controller Example

Your controller class should be in the src/Controller directory and follow the PSR-4 autoloading standard. For example, if you create a controller named HomeController.php, it should look like this:

<?php
namespace App\Controller;

use MicroApp\MicroApp;

class HomeController
{
    public function routes(MicroApp $app): void
    {
        $app->get('/home', [$this, 'index']);
    }

    public function index(): void
    {
        echo 'Hello from HomeController';
    }
}

🧩 Extending MicroApp Class

You can extend the MicroApp class to customize internal behavior β€” such as centralized error handling:

class MyApp extends MicroApp {
    protected function handleException(\Throwable $e): void {
        error_log('Caught: ' . $e->getMessage());
        self::json(['error' => 'Something went wrong'], 500);
    }
}

πŸ”½ Accessing Request Input

You can use MicroApp::input() to safely retrieve and sanitize input from various sources:

Usage:

$name = MicroApp::input('name'); // GET by default
$email = MicroApp::input('email', 'POST', 'email');
$id = MicroApp::input('user_id', 'JSON', 'int');
$token = MicroApp::input('Authorization', 'HEADER');

MicroApp::input(string $key, string $method = 'GET', string $filter = 'string')

Parameters:

  • key: The input name to fetch.
  • method: One of 'GET', 'POST', 'JSON', or 'HEADER'.
  • filter: Sanitization type: 'string', 'int' or 'email'.

πŸ›£οΈ Roadmap

MicroApp aims to remain minimal and dependency-free while gradually improving developer experience and production readiness.

βœ… Core Roadmap

  • πŸ”Ή Middleware (before/after) for logging, authentication, CSRF protection, and other cross-cutting concerns
  • πŸ”Ή PHP 8+ Compatibility Check β€” audit for deprecated functions to ensure forward compatibility
  • πŸ”Ή SQLite Support for lightweight, embedded persistence (potentially via a dedicated utility package)
  • πŸ”Ή File Storage / Upload Handling for managing uploaded files and saving them to disk (potentially via a dedicated utility package)

🧠 Under Consideration

These features are being explored and may be added if they fit the minimalist design:

  • πŸ”Έ Route Grouping / Prefixing for modular organization (e.g., /api/v1)
  • πŸ”Έ Route Caching to speed up route resolution (no external dependencies)
  • πŸ”Έ Service Container or DI for clean dependency injection in handlers
  • πŸ”Έ Static File Serving / File Upload Handling β€” define usage and scope
  • πŸ”Έ Logging Integration via a simple log() method or PSR-3 support

βš–οΈ Tradeoffs

MicroApp intentionally leaves out features that are outside its core responsibility as a router. This keeps the framework lightweight, dependency-free, and easy to reason about.

❌ Not handled by design

  • Authentication & Authorization
    You are free to implement auth logic using your own classes or middleware.

  • CSRF Protection
    CSRF validation should be enforced at the application level, not in the router.

  • Session Management
    MicroApp does not handle PHP sessions or session storage by default.

  • Rate Limiting
    Should be implemented at the proxy (e.g. NGINX, Cloudflare) or middleware level.

  • Output Rendering (Templates/Views)
    MicroApp is designed for APIs and microservices, not traditional HTML applications. Template engines are out of scope β€” return raw JSON, plain text, or HTML directly as needed.

  • Advanced Database Abstraction / ORM
    MicroApp doesn’t include a database layer or ORM. You can use PDO directly or integrate any lightweight ORM as needed.

These tradeoffs allow MicroApp to stay focused, composable, and small β€” making it ideal for microservices, APIs, and focused backends.

🚫 Deferred by Design

The following are intentionally left out of the core to preserve MicroApp’s no-dependency philosophy:

  • ❌ Config File Support Loading .env files or configuration files is out of scope for the core. If needed, you can use a community-maintained package such as vlucas/phpdotenv.

🚧 Disclaimer

  • MicroApp is considered stable for use in real projects, though it has not yet reached version 1.0.0. APIs are unlikely to change significantly, but some refinements may still occur as the ecosystem evolves.
  • The codebase has been reviewed with security scanning tools, and fixes have been applied to address identified concerns. Ongoing best-practice audits will continue to ensure safe use in production environments.