A modern, lightweight starter template combining Vite 8 with Plain PHP.
Follows the standard Vite Vanilla JavaScript project structure, replacing index.html with index.php as the server-rendered entry point — without framework bloat (no Laravel, no public/index.php rewrite complexity).
- ⚡ Vite 8 + HMR: Instant server start and blazing-fast Hot Module Replacement.
- 🐘 Plain PHP Entry: Use
index.phpas your main template with full access to PHP runtime and environment variables. - 🎯 Vanilla JavaScript: Clean, standard ES module architecture (
src/main.js,src/style.css,src/counter.js). - 📦 Automated Production Pipeline: Production builds output hashed assets and a manifest (
dist/.vite/manifest.json), loaded automatically by PHP. - 🛡️ Zero Runtime Overhead in Dev: PHP automatically switches between Vite dev server and production manifest using an ephemeral hot-file.
- 🧪 PHPUnit Test Suite: Fully tested PHP adapter with isolated unit tests.
phpvite/
├── .env # Environment configuration
├── .env.example # Environment configuration example
├── .gitignore # Standard Vite + PHP ignores
├── composer.json # PHP dependencies & PSR-4 autoloading
├── package.json # Node dependencies & Vite scripts
├── phpunit.xml # PHPUnit test configuration
├── vite.config.js # Vite build & hot-file plugin configuration
├── README.md # Project documentation
│
├── index.php # Main application entry point (replaces index.html)
│
├── app/ # PHP application backend & adapter
│ └── Vite/ # Lightweight PHP adapter
│ ├── DevServer.php # Concurrent dev server runner
│ ├── Vite.php # Manifest & dev server resolver class
│ └── helpers.php # Global vite() helper function
│
├── public/ # Static public assets (copied directly to dist)
│ ├── favicon.svg
│ └── icons.svg
│
├── src/ # Clean Vite frontend source code
│ ├── assets/ # Bundled assets (images, SVGs)
│ ├── counter.js # Interactive counter component
│ ├── main.js # Main JavaScript entry point
│ └── style.css # Main stylesheet
│
├── storage/ # Runtime directory
│ └── vite.hot # Hot-file indicator (created while Vite dev runs)
│
├── dist/ # Vite build output (generated on build)
│ ├── .vite/
│ │ └── manifest.json
│ └── assets/
│
└── tests/ # PHP unit tests
└── ViteTest.php
- PHP 8.2 or higher
- Composer 2.x
- Node.js 20 or higher
- pnpm (or npm / yarn)
-
Clone or use this repository as a GitHub template:
git clone https://github.com/Realitaa/phpvite.git cd phpvite -
Install dependencies:
pnpm install composer install
Tip (Package Manager): This repository uses pnpm by default. However, if you prefer npm, yarn, or bun, simply delete the
pnpm-lock.yamlfile and install using your preferred package manager (e.g.,npm installorbun install). Thecomposer devcommand will detect and use it automatically. -
Setup environment file:
cp .env.example .env
Start both the Vite development server and the PHP built-in server concurrently with a single command powered by spatie/fork:
composer run dev
# or: composer devOpen your browser at http://localhost:8000.
composer run devforks two concurrent processes: Vite dev server (http://localhost:5173) and PHP built-in server (http://localhost:8000).- Vite writes its active address to
storage/vite.hot. - When
index.phpis rendered,vite()->tags('src/main.js')detectsstorage/vite.hot. - It emits the Vite HMR client script followed by the entry script:
<script type="module" src="http://localhost:5173/@vite/client"></script> <script type="module" src="http://localhost:5173/src/main.js"></script>
- JavaScript and CSS changes update instantly in the browser via HMR.
- When you stop the server (
Ctrl+C), both processes terminate gracefully andstorage/vite.hotis automatically cleaned up.
To build and serve production assets:
# 1. Build optimized assets with Vite
pnpm build
# 2. Start your PHP web server
php -S localhost:8000- Vite bundles and hashes your assets into
dist/and writesdist/.vite/manifest.json. - With no
storage/vite.hotpresent, PHP operates in production mode. - PHP reads the manifest, extracts required CSS
<link>tags and the hashed JavaScript<script type="module">tag:<link rel="stylesheet" href="/dist/assets/main-CsUDhMuy.css"> <script type="module" src="/dist/assets/main-ChP9mZAA.js"></script>
- Manifest content is cached in memory per request for maximum performance.
Generates the required <script> and <link> tags for an entry file:
<?= vite()->tags('src/main.js') ?>- JS Entries: Emits
<script type="module">and associated CSS links. - CSS Entries: Emits
<link rel="stylesheet">. - Security: All generated URLs are safely escaped against XSS.
Resolves the raw public URL for an asset entry:
<img src="<?= vite()->asset('src/assets/logo.png') ?>">Returns true when the Vite development server is actively running.
Run unit tests using Composer:
composer run test
# or: composer testMIT License. Feel free to use this template for your personal or commercial projects.