DEV Community

 Rahul Gupta
Rahul Gupta

Posted on • Edited on

Showing Alerts, Prompts, and Dialog Boxes with NativePHP

Showing Alerts, Prompts, and Dialog Boxes with NativePHP

One of the biggest advantages of building desktop apps with NativePHP is having the ability to interact with the user through native system dialogs. Whether it’s a simple alert, a confirmation prompt, or a file picker dialog, NativePHP gives you the tools to integrate these into your Laravel-powered desktop apps — effortlessly.

In this article, we'll walk through how to:

  • Show basic alerts

  • Ask for user input via prompts

  • Use native file dialogs to open or save files


💡 Why Use Native Dialogs?

Using native dialogs ensures your app feels like it belongs on the user's system — not just a browser pretending to be a desktop app. Native dialogs offer:

  • Familiarity for the user

  • System-level access (file pickers, alerts, etc.)

  • Consistent experience across Windows, macOS, and Linux


🚨 Showing an Alert

To show an alert box with NativePHP, you can use the Dialog facade.

Example:

use Native\Laravel\Facades\Dialog;

Dialog::message('Update Complete', 'Your data has been saved successfully.');
Enter fullscreen mode Exit fullscreen mode

This displays a native system message box with a title and message body.


🧑‍💻 Prompting for Input

Need user input? Use a prompt dialog:

Dialog::prompt('Enter your name')
    ->then(function ($value) {
        // Do something with the input
        logger("User typed: {$value}");
    });
Enter fullscreen mode Exit fullscreen mode

The then() method handles the input from the user asynchronously.


✅ Confirmation Dialogs

Use a confirmation dialog when you need a yes/no response:

Dialog::confirm('Are you sure you want to exit?')
    ->then(function (bool $confirmed) {
        if ($confirmed) {
            // Exit logic
        }
    });
Enter fullscreen mode Exit fullscreen mode

This is great for destructive actions like deleting data or closing the app.


📁 Open File Dialog

Want the user to select a file? Use the Dialog::openFile() method.

Dialog::openFile()
    ->title('Select an image')
    ->filters([
        ['name' => 'Images', 'extensions' => ['jpg', 'png', 'gif']],
    ])
    ->then(function ($filePath) {
        if ($filePath) {
            logger("Selected file: {$filePath}");
        }
    });
Enter fullscreen mode Exit fullscreen mode

💾 Save File Dialog

To prompt the user for a file location to save something:

Dialog::saveFile()
    ->title('Save Report')
    ->defaultPath('report.pdf')
    ->then(function ($filePath) {
        if ($filePath) {
            Storage::put($filePath, 'Your report content');
        }
    });
Enter fullscreen mode Exit fullscreen mode

🧰 Other Dialog Options

Method

Description

Dialog::message()

Shows a simple message box

Dialog::prompt()

Prompts user for input

Dialog::confirm()

Displays yes/no confirmation

Dialog::openFile()

Opens file selection dialog

Dialog::openDirectory()

Opens folder selection dialog

Dialog::saveFile()

Prompts user to choose save path


🧪 Pro Tip: Combine with Livewire or Vue

You can trigger these dialogs from your UI using Livewire, Vue, or even Inertia. Just trigger a Laravel action, and run the Dialog logic from your controller or Livewire component.


✅ Wrapping Up

Dialogs are an essential part of any desktop app. NativePHP makes them clean, simple, and native — just like any framework should. With just a few lines of PHP, you can prompt users, alert them, or let them select files.

Top comments (0)