dconco/phpspa

A lightweight, component-based PHP library for building Single Page Applications (SPAs) without relying on heavy frontend frameworks.

v1.0.0 2025-06-12 00:19 UTC

This package is auto-updated.

Last update: 2025-06-13 15:41:51 UTC


README

๐Ÿ“› Name

phpSPA lets you build fast, interactive single-page apps using pure PHP โ€” with dynamic routing, component architecture, and no full-page reloads. No JavaScript frameworks required.

License Version Documentation GitHub stars Latest Stable Version Total Downloads

๐ŸŽฏ Goal

To empower PHP developers to create modern, dynamic web apps with the elegance of frontend SPA frameworks โ€” but fully in PHP.

  • ๐Ÿšซ No full-page reloads
  • โšก Instant component swapping
  • ๐Ÿงฑ Clean, function-based components
  • ๐ŸŒ Real SPA behavior via History API
  • ๐Ÿง  Now with State Management!

๐Ÿงฑ Core Features

  • ๐Ÿ”„ Dynamic content updates โ€” feels like React
  • ๐Ÿงฉ Component-based PHP architecture
  • ๐Ÿ”— URL routing (client + server synced)
  • ๐Ÿง  Built-in State Management
  • โš™๏ธ Lifecycle support for loaders, metadata, etc.
  • ๐Ÿชถ Minimal JS: one small file
  • ๐Ÿ” Graceful fallback (no JS? Still works)

โœจ Features

  • โœ… Fully PHP + HTML syntax
  • โœ… No template engines required
  • โœ… Dynamic GET & POST routing
  • โœ… Server-rendered SEO-ready output
  • โœ… Per-component and global loading indicators
  • โœ… Supports Composer or manual usage
  • โœ… State system: update UI reactively from JS

๐Ÿง  Concept

  • Layout โ†’ The base HTML (with __CONTENT__)
  • Component โ†’ A PHP function returning HTML
  • App โ†’ Registers and runs components based on routes
  • State โ†’ Simple mechanism to manage reactive variables across requests

๐Ÿงฉ State Management

You can create persistent state variables inside your components using:

$counter = createState("counter", 0);

Update state from the frontend:

phpspa.setState("counter", newValue);

This will automatically re-render the component on update.

๐Ÿ“ฆ Installation

1. Via Composer (Recommended)

composer require dconco/phpspa

Include the autoloader:

require 'vendor/autoload.php';

2. Manual

Include the core files:

require 'path/to/phpspa/core/App.php';
require 'path/to/phpspa/core/Component.php';

๐ŸŒ JS Engine (CDN)

<script src="https://cdn.jsdelivr.net/npm/phpspa-js"></script>

๐Ÿš€ Getting Started (with Live Counter)

<?php
// layout.php
function layout() {
    return <<<HTML
    <html>
        <head>
            <title>My Live App</title>
        </head>
        <body>
            <div id="app">__CONTENT__</div>
            <script src="https://cdn.jsdelivr.net/npm/phpspa-js"></script>
        </body>
    </html>
    HTML;
}
<?php
// components.php
function HomePage() {
    $counter = createState("count", 0);

    return <<<HTML
        <h1>Counter: {$counter}</h1>
        <button onclick="phpspa.setState('count', {$counter} + 1)">Increase</button>
        <button onclick="phpspa.setState('count', 0)">Reset</button>
        <br><br>
        <Link to="/login" label="Go to Login" />
    HTML;
}

function LoginPage() {
    return <<<HTML
        <h2>Login</h2>
        <form method="post">
            <input name="username" placeholder="Username"><br>
            <input name="password" type="password" placeholder="Password"><br>
            <button type="submit">Login</button>
        </form>
    HTML;
}
<?php
// index.php
require 'layout.php';
require 'components.php';

$app = new App('layout');
$app->targetId('app');

$app->attach(
    (new Component('HomePage'))
        ->title('Home')
        ->method('GET')
        ->route('/')
);

$app->attach(
    (new Component('LoginPage'))
        ->title('Login')
        ->method('GET|POST')
        ->route('/login')
);

$app->run();

๐Ÿ›  JS Events

phpspa.on("beforeload", ({ route }) => showLoader());
phpspa.on("load", ({ success }) => hideLoader());

๐Ÿ“š Full Documentation

Looking for a complete guide to phpSPA?

๐Ÿ”— Read the full tutorial and advanced usage on Read the Docs:

๐Ÿ‘‰ https://phpspa.readthedocs.io

The docs include:

  • ๐Ÿ“ฆ Installation (Composer & Manual)
  • ๐Ÿงฉ Component system
  • ๐Ÿ” Routing & page transitions
  • ๐Ÿง  Global state management
  • โœจ Layouts, nesting, and loaders
  • ๐Ÿ›ก๏ธ CSRF protection
  • ๐Ÿงช Practical examples & best practices

Whether you're just getting started or building something advanced, the documentation will walk you through every step.

๐Ÿ“˜ License

MIT License ยฉ dconco

๐Ÿง‘โ€๐Ÿ’ป Maintained by

Dave Conco Simple, fast, and native PHP โ€“ just the way we like it.

๐ŸŒŸ Give Me a Star

If you find phpSPA useful, please consider giving it a star on GitHub! It helps others discover the project and keeps the momentum going ๐Ÿš€

๐Ÿ‘‰ Give us a โญ on GitHub

Your support means a lot! โค๏ธ