December 13, 2023

Programming

NativePHP: Build Desktop Applications with PHP

love

Written by:

James Oluwaleye

love

Reviewed by:

Jaurel Team

Follow us:


Following in the footsteps of well-known programs like Trello, Slack, and Discord, NativePHP wraps your PHP application in Electro. While the UI is created with HTML, CSS, and any JavaScript framework, it permits PHP to handle the backend functionality.


Setting the Scene with NativePHP

This article will take a deep dive into NativePHP by turning a desktop Laravel application. Let's begin with a fundamental setup to help you see the bigger picture. The application I'm working on is built on a MySQL database and features a Laravel backend and React frontend.

Installing NativePHP

To get started, we install NativePHP via Composer:

$ composer require nativephp/electron

This adds a new set of NativePHP-specific commands to Laravel's artisan tool. To construct and administer your native application, for example, php artisan native displays a set of commands.

Next, running php artisan native:install sets up the basic structure.

Of particular interest are two files:

  • config/nativephp.php for application configuration
  • app/Providers/NativeAppServiceProvider.php for the bootup sequence and registering native components.

Running the Development Build

With the application set up, we can start our dev build:

$ php artisan native:serve &
$ npm run dev &

We need to start development servers for PHP and the UI. In this case, npm uses [Vite]() to build and serve the UI components to the Electron window.

NativePHP bundles the app with Electron, embedding a PHP interpreter. During development, it switches the backend to a local SQLite database, so we need to run migrations using php artisan native:migrate to set up the new database.


Adding Native Features

To enhance the desktop experience, NativePHP allows integration of native elements like notifications, menu bars, and hotkeys. You can see the complete list of elements in the docs page

I added a simple notification on app start by editing app/Providers/NativeAppServiceProvider.php. This helps us give the application that native feel we’re aiming for.

First, we add the Notification Facade:

use Native\Laravel\Facades\Notification;

Then, in the boot function I add the following lines after the window opens:

Notification::title('Application Started')
->message('This message is comming from NativePHP running on Electron')
->show();
}

Upon saving, the hot reload function should restart the application and show the notification (check that you haven’t enabled notifications from Electron if you don’t see them).

Building for Release

For a production build, we fill in the publishing details in config/nativephp.php. Notably, the environment file (.env) gets bundled in the build, so sensitive data must be cleaned up using cleanup_env_keys.

To build a release for your OS, simply run:

$ php artisan native:build

This process generates various bundles, including a DMG, a Zip file, and the binary for the application.

We can also build Windows and Linux bundles with:

$ php artisan native:build win
$ php artisan native:build linux

We should get a setup.exe, AppImage and, .deb files after the build process completes.

Caveats of NativePHP

Before diving into NativePHP, here are some considerations:

Alpha Stage: As of now, NativePHP is in its alpha stage, leaning more towards a beta experience.

Laravel-First Framework: While NativePHP claims compatibility with any PHP framework, its design is heavily tailored for Laravel.

Database Limitations: The database support is confined to local SQLite, replacing your existing database setup upon build.

Cross-Compilation Limits: While it supports building for different OSs, cross-architecture builds are not yet supported. For instance, binaries built on an M1 chip won’t work on most Linux and Windows machines (ARM builds won’t work on Intel chips). A workaround is using a different machine or a CI/CD pipeline for builds.

Conclusion

NativePHP, even in its alpha stage, is an impressive and user-friendly framework for PHP developers. It opens up a new world of possibilities for PHP-based desktop applications. If you’re into PHP development, NativePHP is definitely a project to keep an eye on.

That’s all for this exploration. If you found this interesting, feel free to like, share, and subscribe for more updates. Thanks for reading, and happy coding!

Follow us:

Join the waitlist

Subscribe to our newsletter channel. We provide reliable, scalable, and customizable content solutions for your business.

profile
profile
profile