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.
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!
- π 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)
- β 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
- 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
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.
composer require dconco/phpspaInclude the autoloader:
require 'vendor/autoload.php';Include the core files:
require 'path/to/phpspa/core/App.php';
require 'path/to/phpspa/core/Component.php';<script src="https://cdn.jsdelivr.net/npm/phpspa-js"></script><?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();phpspa.on("beforeload", ({ route }) => showLoader());
phpspa.on("load", ({ success }) => hideLoader());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.
- GitHub: dconco/phpspa
- JS Engine: dconco/phpspa-js
- Website: https://phpspa.readthedocs.io
- License: MIT
MIT License Β© dconco
Dave Conco Simple, fast, and native PHP β just the way we like it.
If you find phpSPA useful, please consider giving it a star on GitHub! It helps others discover the project and keeps the momentum going π
Your support means a lot! β€οΈ