One way to turn a Laravel app into a desktop app is to use an Electron wrapper. Electron is a framework that allows you to build desktop applications using web technologies such as HTML, CSS, and JavaScript. Here are the steps to follow:
- Install Electron using npm:
npm install electron --save-dev
- Create a new file called
main.jsin the root directory of your Laravel app. This file will be the entry point for your Electron app. Here's an example of what it could look like:
const { app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
win.loadURL('http://localhost:8000')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
This code creates a new Electron window and loads your Laravel app at http://localhost:8000.
- Modify your Laravel app's
public/index.phpfile to check if the app is running in an Electron window. If it is, set the base URL tofile://${__DIR__}. Here's an example:
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Electron') !== false) {
$baseUrl = 'file://' . __DIR__;
} else {
$baseUrl = getBaseUrl();
}
$app = require_once __DIR__.'/../bootstrap/app.php';
This code checks if the user agent contains the string "Electron". If it does, it sets the base URL to the file path of your Laravel app.
-
Start your Laravel app using
php artisan serve. -
Start your Electron app using
electron .in the root directory of your Laravel app.
Your Laravel app should now be running in an Electron window. You can package your Electron app for distribution using tools like Electron Packager or Electron Forge.