Building an App with Native PHP
Regarding building applications developers try to look out for frameworks and libraries
that help make it easier. The application, however, developed natively in PHP will
always be unique in terms of its control and deep understanding of the language itself.
In this blog, we will walk you through the basics of app building using native PHP,
introduce key concepts like building and versioning, and guide you through commands
that make it streamlined.
Why Native PHP?
● Frameworks like Laravel and Symfony offer a sort of structure, but native PHP
really comes into its own.
● Lightweight Performance: A native PHP application runs without the overhead of
a framework.
● Full Code Control: You dictate every aspect of your app without being burdened
by framework conventions.
● Learning Opportunity: Native PHP helps understand PHP at its core and is very
useful for both beginners and advanced developers.
Setting Up Your Development Environment
Before you start building, ensure your development environment is ready. Install PHP, set up a
local server (like XAMPP or the built-in PHP server), and use a code editor like VS Code. Start a
local server with:
php -S localhost:8000
Building Your App
Building your app means compiling it into a production-ready state. NativePHP simplifies this by
signing and notarizing your application, making it ready for distribution.
Build Process Overview:
1. Compiles your application and the Electron/Tauri runtime into a single executable for one
platform.
2. Allows automatic updates when paired with the publish command to upload build
artifacts to your provider.
3. Ensures you test the app on each supported platform before distribution.
To build your application, use the following command:
php artisan native:build
For cross-compilation, specify the target platform:
php artisan native:build win
Options include mac, win, and linux. Keep in mind, cross-compilation is not supported on all
platforms, especially for Windows binaries on Linux, which may require tools like Wine:
dpkg --add-architecture i386 apt-get -y update apt-get -y install wine32
Code Signing
Both Windows and macOS require apps to be signed before distribution. While NativePHP
simplifies the process, platform-specific requirements still apply:
● For Windows and macOS, refer to Electron documentation.
● Use environment variables for macOS signing and notarizing:
- NATIVEPHP_APPLE_ID=developer@abcwidgets.com
- NATIVEPHP_APPLE_ID_PASS=app-specific-password
- NATIVEPHP_APPLE_TEAM_ID=8XCUU22SN2
Add these to your .env file to avoid exposing sensitive data.
Versioning
Every build requires updating the version of your application in the config/nativephp.php
file.
● Use a simple incremental build number or a semantic versioning (SemVer) scheme.
● Migrations on a user’s machine occur only when the version changes, ensuring the
database stays updated with the latest structure.
First Run
When users launch your application for the first time, several processes occur automatically:
1. Appdata Folder Creation: The appdata folder is named based on the
nativephp.app_id configuration.
2. SQLite Database Initialization: The user’s database (database.sqlite) is created
and migrated.
3. Optional Database Seeding: You can seed the database by checking and running the
appropriate db:seed command:
use App\Models\Config;
use Illuminate\Support\Facades\Artisan;
if (Config::where('seeded', true)->count() === 1) { Artisan::call('db:seed'); }
Subsequent Runs
Each time the app runs, NativePHP checks if the app version has changed. If it has, it migrates
the user’s database in the appdata folder. This ensures seamless updates without breaking the
user experience.
Conclusion
Building an app with native PHP is both rewarding and empowering. By managing every aspect
of the process, from database handling to versioning, you gain full control over your application.
Tools like NativePHP enhance the process, enabling you to build, test, and distribute your app
effortlessly while maintaining PHP’s core simplicity.
Whether you’re a beginner learning the basics or an advanced developer optimizing for
lightweight performance, native PHP opens the door to creating robust, maintainable