Building a Desktop Application with NativePHP on Laravel
In the modern development landscape, PHP has often been written off as a language of the past, struggling to keep up with the dynamic needs of current software. But with the advent of frameworks like NativePHP, PHP is enjoying a resurgence, proving its relevance and flexibility in the context of modern application development.
NativePHP is an innovative framework that empowers PHP developers to craft rich, native desktop applications. It enables you to leverage your existing skills in PHP, along with HTML, CSS, and JavaScript, to create cross-platform native apps. Even if you’re a PHP novice, you’ll find NativePHP intuitive and easy to learn.
And guess what, PHP isn’t dead. It’s alive and kicking, thanks to frameworks like NativePHP. So, let’s dive into how you can build a desktop application with NativePHP on Laravel.
Step 1: Create a New Laravel Application
The first step in our journey is to create a new Laravel application. Laravel, an open-source PHP framework, is designed for developers who need a simple, elegant toolkit to build full-featured web applications.
You can easily create a Laravel application using Composer. Just run the following command in your terminal:
composer create-project laravel/laravel nativephp-example
This command creates a fresh Laravel installation in the nativephp-example
directory.
Step 2: Install NativePHP
After creating your Laravel application, the next step is to install NativePHP. This will provide the necessary tools and dependencies to transform your web application into a native desktop application.
To install NativePHP, you just need to require the package via Composer by running:
cd nativephp-example
composer require nativephp/electron
php artisan native:install
Install command might be not work. Checkout the troubleshooting. If it is work. You can skip that part.
This command installs the nativephp/electron
package into your Laravel application.
Step 2 — Troubleshooting Tip: Certificate Issues
Developers working on ARM processors may encounter certain hiccups during the installation process due to system architecture discrepancies. However, don’t worry — here’s a workaround to get you past this roadblock.
The necessary files are located at:
vendor/nativephp/php-bin/cacert.pem
vendor/nativephp/php-bin/bin/mac/arm64/php
orvendor/nativephp/php-bin/bin/mac/x86/php
(choose the file corresponding to your Mac's architecture)
You can fix the installation issue by copying these files into the vendor/nativephp/electron/resources/js/resources
directory.
Here are the commands to do this:
cp vendor/nativephp/php-bin/cacert.pem vendor/nativephp/electron/resources/js/resources
cp vendor/nativephp/php-bin/bin/mac/arm64/php vendor/nativephp/electron/resources/js/resources
Or if you’re using an x86 Mac:
cp vendor/nativephp/php-bin/bin/mac/x86/php vendor/nativephp/electron/resources/js/resources
This manual adjustment helps ensure the native:serve
command works smoothly on your Mac, irrespective of its architecture.
Step 3: Publish Configuration
Sometimes, after installing a package in Laravel, you may need to publish its configuration file into your application. This can be done using the vendor:publish
command. In case the NativePHP configuration doesn't get published automatically, use the following command:
php artisan vendor:publish --tag=nativephp-config
This command publishes the configuration files for the NativePHP\Electron
package to your application's config
directory. Now you have control over the configuration of your NativePHP package.
And your config/nativephp.php
file looks like this:
<?php
return [
/**
* The version of your app.
* It is used to determine if the app needs to be updated.
* Increment this value every time you release a new version of your app.
*/
'version' => env('NATIVEPHP_APP_VERSION', '1.0.0'),
/**
* The ID of your application. This should be a unique identifier
* usually in the form of a reverse domain name.
* For example: com.nativephp.app
*/
'app_id' => env('NATIVEPHP_APP_ID'),
/**
* If your application allows deep linking, you can specify the scheme
* to use here. This is the scheme that will be used to open your
* application from within other applications.
* For example: "nativephp"
*
* This would allow you to open your application using a URL like:
* nativephp://some/path
*/
'deeplink_scheme' => env('NATIVEPHP_DEEPLINK_SCHEME'),
/**
* The author of your application.
*/
'author' => env('NATIVEPHP_APP_AUTHOR'),
/**
* The default service provider for your application. This provider
* takes care of bootstrapping your application and configuring
* any global hotkeys, menus, windows, etc.
*/
'provider' => \App\Providers\NativeAppServiceProvider::class,
/**
* The NativePHP updater configuration.
*/
'updater' => [
/**
* Whether or not the updater is enabled. Please note that the
* updater will only work when your application is bundled
* for production.
*/
'enabled' => env('NATIVEPHP_UPDATER_ENABLED', true),
/**
* The updater provider to use.
* Supported: "s3", "spaces"
*/
'default' => env('NATIVEPHP_UPDATER_PROVIDER', 'spaces'),
'providers' => [
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'endpoint' => env('AWS_ENDPOINT'),
'path' => env('NATIVEPHP_UPDATER_PATH', null),
],
'spaces' => [
'driver' => 'spaces',
'key' => env('DO_SPACES_KEY_ID'),
'secret' => env('DO_SPACES_SECRET_ACCESS_KEY'),
'name' => env('DO_SPACES_NAME'),
'region' => env('DO_SPACES_REGION'),
'path' => env('NATIVEPHP_UPDATER_PATH', null),
],
],
],
];
Step 5: Serve Your Application
Finally, with the Laravel application set up and NativePHP installed, you can serve your application to start developing. Just run:
php artisan native:serve
And voila! You have your PHP desktop application up and running.
In conclusion, PHP has never been more relevant and versatile thanks to the advent of frameworks like Laravel and NativePHP.
So whether you’re an experienced PHP developer or a newbie, it’s an exciting time to dive into PHP desktop application development.
Happy coding!