VolkanOner's avatar

How to turn laravel app into desktop app

How to turn laravel app into desktop app ?

0 likes
9 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

One way to turn a Laravel app into a desktop app is to use an Electron wrapper. Electron is a framework that allows you to build desktop applications using web technologies such as HTML, CSS, and JavaScript. Here are the steps to follow:

  1. Install Electron using npm:
npm install electron --save-dev
  1. Create a new file called main.js in the root directory of your Laravel app. This file will be the entry point for your Electron app. Here's an example of what it could look like:
const { app, BrowserWindow } = require('electron')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadURL('http://localhost:8000')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

This code creates a new Electron window and loads your Laravel app at http://localhost:8000.

  1. Modify your Laravel app's public/index.php file to check if the app is running in an Electron window. If it is, set the base URL to file://${__DIR__}. Here's an example:
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Electron') !== false) {
    $baseUrl = 'file://' . __DIR__;
} else {
    $baseUrl = getBaseUrl();
}

$app = require_once __DIR__.'/../bootstrap/app.php';

This code checks if the user agent contains the string "Electron". If it does, it sets the base URL to the file path of your Laravel app.

  1. Start your Laravel app using php artisan serve.

  2. Start your Electron app using electron . in the root directory of your Laravel app.

Your Laravel app should now be running in an Electron window. You can package your Electron app for distribution using tools like Electron Packager or Electron Forge.

2 likes
rzrokycse's avatar

Error Showing:: Fatal error: Uncaught Error: Call to undefined function getBaseUrl() .............

rzrokycse's avatar

@nexxai Thanks for your suggestion, I want to use Electron js for both online and offline data entry, and auto synchronize in single server. But If I use nativephp for desktop service then May I able to synchronize offline data with online server without any api development ?

nexxai's avatar

@rzrokycse Whether you use Electron or some other solution, I'm curious how you would consider synchronizing data between two sources without an API to connect them

1 like
comfygentech's avatar

Turning a Laravel app into a desktop app involves packaging it in a way that users can run it on their desktop without relying on a web browser. Here's a basic guide:

Use Electron or NW.js: Both Electron and NW.js are popular frameworks that enable the development of desktop applications using web technologies. They package web applications (like Laravel apps) into standalone executable files.

Install Electron or NW.js: Choose either Electron or NW.js and install it globally on your machine using npm (Node Package Manager).

bash Copy code

For Electron

npm install -g electron

For NW.js

npm install -g nw Create a New Project: Set up a new project for your desktop app. Create a new folder, navigate to it in the terminal, and initialize a new Node.js project.

bash Copy code npm init Modify Project Structure: Move your Laravel app files into the project folder. You'll need to reorganize the structure to suit the requirements of Electron or NW.js.

Create Main Entry File: For Electron, create a main entry file (e.g., main.js) that will act as the main process of your desktop app. In NW.js, this is typically the package.json file.

Handle Laravel App Launch: In your main entry file, configure the app to launch your Laravel app. This often involves using a webview or window to display your Laravel app.

Build the Desktop App: Use the Electron or NW.js command-line tools to build your desktop app.

bash Copy code

For Electron

electron .

For NW.js

nw . Distribute Your App: Once built, you'll get executable files for different operating systems. Distribute these files to users, and they can run your Laravel app as a desktop application.

Remember to consider the additional functionalities and features you might need for a desktop app, such as offline capabilities or native system integrations, and adjust your Laravel app accordingly.

1 like

Please or to participate in this conversation.