Setting Up Your First NativePHP Project in Laravel



In our last post, we introduced you to NativePHP, a powerful tool that lets you build native desktop applications using Laravel. Today, we’re getting our hands dirty and setting up our very first NativePHP app—from scratch.

Let’s bring Laravel from the browser to the desktop.


🧰 Prerequisites

Before we begin, make sure you have the following tools installed on your machine:

  • PHP 8.1+

  • Composer

  • Node.js & NPM

  • Laravel (v10 or above recommended)

  • Git

  • Electron Builder (will be handled via NativePHP)

You can check versions with:

php -v
composer -V
node -v
npm -v

🧱 Step 1: Create a New Laravel Project

If you don’t already have a Laravel project, let’s create one:

composer create-project laravel/laravel nativephp-demo
cd nativephp-demo

Or you can use an existing Laravel app.


📦 Step 2: Install NativePHP

Now, install the NativePHP Electron package:

composer require nativephp/electron

After installation, you’ll have access to Artisan commands specific to NativePHP.


⚙️ Step 3: Install NativePHP Core Files

Run the following command to scaffold NativePHP-specific files:

php artisan native:install

This will publish:

  • native.php config file

  • Native-specific entry points and logic

  • Electron support structure

You’ll also notice a new file at app/NativePHP/MainWindow.php—this is where you can customize your app’s native window.


🧪 Step 4: Run Your Native App

To test your desktop app:

php artisan native:serve

This will:

  • Spin up your Laravel app locally

  • Launch an Electron window that renders your Laravel app

  • Open a native desktop window displaying your Laravel view

🎉 Boom! You now have a working Laravel-powered desktop app!


🪟 What’s Going On Under the Hood?

NativePHP:

  • Spins up a local server (typically 127.0.0.1:8000)

  • Wraps that in an Electron shell

  • Uses your default Blade views (or Vue/React SPA if set up)

You can customize the window by editing app/NativePHP/MainWindow.php:

use Native\Laravel\Facades\Window;

public function boot()
{
    Window::open()
        ->title('My First NativePHP App')
        ->width(1024)
        ->height(768);
}

🖼️ Optional: Add a Blade View

Create a basic homepage:

php artisan make:controller HomeController
// app/Http/Controllers/HomeController.php
public function index()
{
    return view('welcome');
}

Update routes/web.php:

Route::get('/', [HomeController::class, 'index']);

🔚 Wrapping Up

You’ve just created your first desktop application with Laravel using NativePHP!


📁 Files Summary

  • Laravel project root

  • composer.json with nativephp/electron

  • app/NativePHP/MainWindow.php

  • Native app served on Electron window


🧩 NativePHP Commands Cheat Sheet

Command Description
php artisan native:install Setup NativePHP structure
php artisan native:serve Serve Laravel as desktop app
php artisan native:build Package for production

🔗 Useful Links


🧠 Pro Tip: Treat this as a Laravel project first. You get all the Laravel goodies—routes, models, jobs, and queues—running on the desktop.