Search by

limon / native-desktop-kit

limonhasan

Everything a Laravel app needs around NativePHP Desktop: loaded only where it belongs (never on a web server), live window icon and title, tray, network access for other devices, QR codes without PHP extensions, and hosting packages.

v0.4.1 2026-11-30 06:56 UTC

This package is auto-updated.

Last update: 2026-11-30 06:57:02 UTC


README

Everything a Laravel app needs around NativePHP Desktop, extracted from a real product that ships both as a desktop app and as an online (web) version from the same code.

What Why you want it
Web-safe loading NativePHP is loaded only where it belongs (the desktop app and its own artisan commands). On a web server its /_native/api/* endpoints are open to anyone without a login, so it must not exist there. Includes the php artisan route:cache trap (see below).
Live window icon and title The company logo becomes the icon of every window (title bar, taskbar, windows the page opens itself such as print views) and updates at runtime, without a rebuild. NativePHP has no API for this.
Keep running in the tray Closing the main window hides it; a tray icon (Open / Quit) keeps the app and its network listener running.
Network access A second listener lets phones and other PCs on the same network open the app in a browser: start/stop at runtime, free-port check, local addresses, QR code.
QR codes without extensions The PHP bundled with NativePHP has no xmlwriter, so BaconQrCode's SVG back end fails there. Qr::svg() draws the code itself.
desktop:doctor Compares the extensions your production dependencies require with the PHP the desktop app bundles, before a customer hits the missing one.
Hosting packages native-desktop-package builds a full package for a new server, or an update zip of recent changes for a live host. Never includes .env, tests or the desktop app.

Requires PHP 8.3+, Laravel 11–13, nativephp/desktop ^2.5.

Install

composer require limon/native-desktop-kit
php artisan desktop:install        # config, NativeAppServiceProvider stub (if missing), composer hook, Electron patches
php artisan native:install --publish   # NativePHP's own Electron project, then:
php artisan desktop:patch
php artisan desktop:doctor

The kit's provider is auto-discovered and is safe on a web server (no routes, no middleware). It also tells Laravel not to auto-discover nativephp/desktop, and registers it itself only when NativeRuntime::shouldLoadPackage() says so. No change to bootstrap/providers.php is needed.

Test suites must run with APP_ENV=testing (Laravel's default phpunit.xml does), otherwise NativePHP is not loaded in tests.

Web-safe loading: the route:cache trap

Registering NativePHP "for every artisan command" looks harmless, but a standard deploy step, php artisan route:cache, runs in the CLI: it would save NativePHP's anonymous /_native routes into the route cache and the web server would serve them. So the CLI loads it only for native:*, list, help and tests; the desktop shell's own PHP processes load it through NATIVEPHP_RUNNING. Verified on an unzipped production package: after route:cache, both endpoints return 404 and the cache holds no _native routes.

Branding: window icon and title

Bind your own source (default: config('app.name'), no logo):

use Limon\NativeDesktopKit\Branding\BrandingSource;

$this->app->bind(BrandingSource::class, fn () => new class implements BrandingSource {
    public function name(): string { return Setting::get('company.name'); }
    public function logoContents(): ?string { return Storage::get(Setting::get('company.logo')); } // bytes, or null
});

Call NativeBranding::sync() at app start (the provider stub does) and whenever the logo or name changes. Set default_icon in config/native-desktop-kit.php for apps without a logo. A logo changed from a phone on the network updates the desktop window too.

Tray, login item, restart, network access

use Limon\NativeDesktopKit\{Desktop, LanServer, Qr};

Desktop::syncTray(true, 'Open Acme', 'Quit', 'Acme');   // keep running in the tray
Desktop::openAtLogin(true);                              // installed builds only (a dev run would register bare Electron)
Desktop::relaunch();

LanServer::start(8800);  LanServer::stop();
LanServer::portInUse(8800);  LanServer::urls(8800);      // ['http://192.168.0.166:8800', ...]
Qr::svg('http://192.168.0.166:8800', 132);

Nothing here ever throws: outside the desktop app (or if the shell is unreachable) the calls simply return. Keep the settings screen in your app; it is UI and storage specific. Show it only when Desktop::running() and only to requests from this computer (request()->ip() is 127.0.0.1), otherwise a phone on the network could change the host's startup options.

Remote access (tunnel)

Tunnel publishes the network listener on a public https address with a tunnel program that runs beside the app, so it can be opened from outside the local network. The programs are not bundled: install cloudflared and/or ngrok once (or set native-desktop-kit.tunnel.binaries).

use Limon\NativeDesktopKit\{LanServer, Tunnel};

LanServer::start(8800, '127.0.0.1');            // listener for the tunnel only (use 0.0.0.0 to also serve the LAN)
Tunnel::start('cloudflare', 8800);               // quick tunnel: free, no account, random address per start
Tunnel::start('cloudflare_token', 8800, ['token' => $t, 'public_url' => 'garage.example.com']);  // named tunnel, fixed address
Tunnel::start('ngrok', 8800, ['token' => $t, 'domain' => 'optional.ngrok.app']);

Tunnel::status();   // ['provider', 'running', 'url', 'installed' => ['cloudflared' => bool, 'ngrok' => bool]]
Tunnel::stop();

Tokens go to the program through its environment, never the command line. A public tunnel exposes the whole app: put a strong login (ideally Cloudflare Access) in front of it, and have the app trust the tunnel's proxy headers so https links work. Tailscale is not wrapped: it is a private network, not a public tunnel.

Installing the tunnel program: Tunnel::installPlan() says how (winget on Windows, Homebrew on macOS, a copy-and-run command on Linux, a download link for ngrok), and Tunnel::install() runs the package manager in the background for cloudflared (Windows may show its own admin prompt). Follow it with Tunnel::status()['install'] (idle / installing / failed plus an error) and ['installed']['cloudflared'].

Electron patches

NativePHP re-mirrors its Electron project on composer update, which would erase customisations. desktop:patch re-applies them idempotently (the installer adds it to post-update-cmd, after native:install):

  1. POST /api/window/icon: swap the icon of every open window; new windows open with it.
  2. Windows opened by the page (window.open) get the current icon.
  3. Close-to-tray: POST /api/desktop/behaviour, /api/desktop/refresh-tray.

The patches edit specific lines of NativePHP's source. If a release moves them the script stops with an error instead of silently skipping, and tests/PatchScriptTest.php runs it against the pristine files of the installed nativephp/desktop, so a bump is checked before an app is.

Hosting packages

Run from the project folder:

php vendor/bin/native-desktop-package                 # FULL package for a new server (production vendor/, built assets, storage layout)
php vendor/bin/native-desktop-package update          # UPDATE: uncommitted changes
php vendor/bin/native-desktop-package update 3+       # last 3 commits + uncommitted (same arguments as the classic zip_updates.php)
php vendor/bin/native-desktop-package --build         # run "npm run build" first
php vendor/bin/native-desktop-package full --no-vendor

Only what a web server needs is packaged; never .env, tests, nativephp/, config/nativephp.php, app/Providers/NativeAppServiceProvider.php, local databases or logs. Update packages add public/build when views or CSS changed, list deleted files in _deleted_files.txt, warn when locked packages changed (composer install on the server) and print what to run afterwards. The packaged composer.json drops the desktop hooks.

Optional desktop-package.json in the project root:

{
    "full_roots": ["app", "bootstrap", "config", "database", "lang", "public", "resources", "routes"],
    "root_files": ["artisan", "composer.json", "composer.lock", ".env.production.example", "README.md"],
    "exclude": ["app/Private/", "docs/"],
    "deploy_guide": "deploy/DEPLOY.md"
}

Error exits return a non-zero status, so the script is safe in a pipeline.

Development

composer install
composer test      # Testbench: runtime gate, QR, LAN server, branding, shell calls, doctor, patch script, packaging script
composer format

Lessons baked in

  • Anything that fails on the desktop only (a missing PHP extension) fails on someone else's computer: run desktop:doctor in CI.
  • A modal dialog in the app's startup code blocks everything after it: show it last.
  • exit("message") exits with status 0 in PHP: use a helper that exits non-zero for errors.
  • Windows lets a second socket bind a taken port: test a port by connecting to it too.
  • Verify security claims against a real, unzipped production package, not the dev tree.

License

MIT. See LICENSE.