Integrate Vite with CakePHP 5 for modern frontend asset bundling with hot module replacement.
- Easy asset mapping (build/resources) with application
- Hot Module Replacement (HMR) with live reload
- DDEV support out of the box
- CLI command for quick setup
- Works with and without Docker/DDEV
| Requirement | Version |
|---|---|
| PHP | >= 8.1 |
| CakePHP | ^5.0 |
| Node.js | >= 18.0 |
- Add the repository to your
composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/CakePHPMitra/Vite-Plugin"
}
]
}- Install via Composer:
composer require cakephpmitra/vite-plugin:dev-main- Load the plugin in
config/plugins.php:
return [
// ... other plugins
'CakePhpViteHelper' => [],
];Or via CLI:
bin/cake plugin load CakePhpViteHelper- Install Node packages with Vite configuration:
bin/cake vite-helper installCreate resources/js/app.js:
console.log("Welcome to CakePHP!");Create resources/css/app.css:
body {
background-color: skyblue;
}// templates/layout/default.php
<?= $this->Vite->asset(['resources/js/app.js', 'resources/css/app.css']) ?>Development with HMR:
npm run devProduction build:
npm run buildIf using DDEV, create .ddev/config.vite.yaml:
web_extra_exposed_ports:
- name: vite
container_port: 5173
http_port: 5172
https_port: 5173
hooks:
post-start:
- exec: "[ -f package.json ] && npm install || true"
- exec: "[ -f vite.config.js ] && (npm run dev > /dev/null 2>&1 &) && sleep 3 && echo \"https://${DDEV_HOSTNAME}:5173\" > hot || true"Then restart DDEV:
ddev restartSee DDEV Setup Guide for detailed instructions and troubleshooting.
The ViteHelper uses a hot file to determine the development server URL:
| Mode | Hot File | Behavior |
|---|---|---|
| Development | Exists with URL | Assets loaded from Vite dev server (HMR enabled) |
| Production | Does not exist | Assets loaded from webroot/build/ (compiled) |
See Configuration Guide for details.
When deploying your CakePHP app under a subdirectory/alias (e.g., https://example.com/myapp/), asset URLs are automatically handled using Router::url().
This ensures assets like /build/assets/app.js correctly become /myapp/build/assets/app.js.
No additional configuration required - the helper auto-detects the base path.
See the docs folder for detailed documentation:
- Features - Usage and helper methods
- Development - Configuration and DDEV setup
- Configuration - Vite and hot file configuration
- DDEV Setup - Complete DDEV guide
- Production - Production deployment
See CONTRIBUTING.md for guidelines.
Report bugs and feature requests on the Issue Tracker.
MIT License - see LICENSE file.