Building a Native Desktop Application with NativePHP, Laravel, and Electron
This guide explains how to package a Laravel‑based PHP application into a standalone desktop app using NativePHP and Electron, covering installation, development workflow, adding native features like notifications, and creating production builds for Windows, macOS, and Linux.
NativePHP allows you to package your PHP application into a self‑contained web‑based desktop app similar to Slack, Discord, or Trello. The backend logic remains in PHP while the UI can be built with HTML, CSS, and any JavaScript framework.
To explore NativePHP, the author converts a Laravel application into a desktop app: the backend is provided by Laravel, the frontend by React, and the database by MySQL.
Install NativePHP
First, install NativePHP via Composer:
<code>composer require nativephp/electron</code>This adds NativePHP to your Laravel project and extends the artisan tool with new commands. For example, php artisan native lists all native‑app commands.
Run php artisan native:install to create the basic project structure.
Two important files are created:
config/nativephp.php – configuration for the application.
app/Providers/NativeAppServiceProvider.php – handles the startup sequence and registers native components.
Development Build
After setting up the application, start the development servers:
<code>php artisan native:serve &
npm run dev &</code>This launches both the PHP server and the UI development server. npm uses Vite to build the UI components and serve them to the Electron window.
NativePHP bundles the app with Electron and embeds the PHP interpreter. During development it switches the backend to a local SQLite database, so run the migration command php artisan native:migrate to set up the new database.
Add Native Features
NativePHP supports native elements such as notifications, menu bars, and hotkeys. By editing app/Providers/NativeAppServiceProvider.php , a simple notification can be added at startup:
<code>use Native\Laravel\Facades\Notification;</code>Inside the boot() method, add:
<code>Notification::title('Application Started')
->message('This message is coming from NativePHP running on Electron')
->show();</code>After saving, hot‑reload should restart the app and display the notification (ensure Electron notifications are enabled).
Build Release
For production builds, fill in release details in config/nativephp.php . The environment file .env is bundled, so use the cleanup_env_keys method to remove sensitive data.
Run the following command to build for the current OS:
<code>php artisan native:build</code>This generates packages such as DMG, Zip, and the application binaries.
To build Windows and Linux bundles, run:
<code>php artisan native:build win
php artisan native:build linux</code>After the build completes, you will have files like:
setup.exe – Windows installer
AppImage – Linux application
.deb – Linux package
NativePHP Caveats
Alpha stage: NativePHP is still in alpha and may contain bugs or instability.
Laravel‑centric: Designed for Laravel; compatibility with other PHP frameworks may vary.
Database limitation: Only supports local SQLite; existing database settings are replaced during build.
Cross‑compilation limits: Builds are not cross‑architecture; binaries built on Apple M1 cannot run on most Linux or Windows machines. Use a different machine or CI/CD pipeline for those targets.
NativePHP is an emerging framework that enables PHP developers to create native desktop applications. Although it is still in alpha, it shows great potential for developers interested in bringing PHP apps to the desktop.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.