Skip to content
forked from dconco/phpspa

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

License

Notifications You must be signed in to change notification settings

hallofcodes/phpspa

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ phpSPA - Build Native PHP SPAs Without JavaScript Frameworks

πŸ“› 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 JS Version Documentation GitHub stars PHP 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.


πŸ“˜ Docs & Links


πŸ“˜ 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! ❀️


About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 84.6%
  • JavaScript 15.4%