编程 使用 PHP 和 Web 技术(而不是 Electron)构建跨平台桌面应用程序:探索 Boson

2025-07-11 09:31:00 +0800 CST views 172

使用 PHP 和 Web 技术(而不是 Electron)构建跨平台桌面应用程序:探索 Boson

随着 Web 技术的发展,我们已经习惯于在浏览器中用 HTML/CSS/JavaScript 构建复杂应用。但如果你是一位 PHP 开发者,是否想过:能否用 PHP 构建一个无需 Electron 的桌面应用?

答案是 可以!本文将带你了解一个创新的项目 —— Boson,它让你可以仅使用 PHP 和前端技术开发原生桌面应用,运行于 Windows、macOS 和 Linux。


💡 什么是 Boson?

Boson 是一个开源、轻量级的 PHP 桌面应用程序运行时和编译器平台,其核心特性包括:

  • 内置 Chromium WebView 引擎
  • 集成 PHP 解释器
  • 输出为 可执行文件(.exe/.app)
  • 无需 Node.js、Electron 或本地 HTTP 服务器
  • 支持前端技术栈:HTML、CSS、JavaScript、React、Vue、Tailwind、Bootstrap 等

✅ 为什么选择 Boson?

特性说明
无 Node 依赖不需要安装 Node.js,避免 Electron 的臃肿
不使用 HTTP 服务与 NativePHP 不同,不需要开启本地服务器
真正本地访问直接访问文件系统、调用本地 API
极致轻量不像 Electron 动辄几百 MB,Boson 应用体积仅数 MB

🚀 快速开始

1. 安装运行时(必需)

composer require boson-php/runtime

使用示例代码:

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Boson\Application();

2. 安装编译器(开发时使用)

composer require boson-php/compiler --dev

编译器用于将你的项目构建成可执行程序。


🧩 使用 Twig 构建组件(可选)

如果你使用 Twig 模板引擎进行前端渲染,Boson 同样支持:

第一步:安装 Twig

composer require twig/twig

第二步:创建 TwigComponent 基类

use Boson\WebView\Api\WebComponents\ReactiveContext;
use Boson\WebView\Api\WebComponents\WebComponent;
use Boson\WebView\WebView;
use Twig\Environment;
use Twig\TemplateWrapper;

abstract class TwigComponent extends WebComponent
{
    private TemplateWrapper $template {
        get => $this->template ??= $this->twig->createTemplate($this->renderTwig());
    }

    public function __construct(
        protected readonly Environment $twig,
        ReactiveContext $ctx,
        WebView $webview,
    ) {
        parent::__construct($ctx, $webview);
    }

    abstract protected function renderTwig(): string;

    final public function render(): string
    {
        return $this->template->render(\get_object_vars($this));
    }
}

第三步:实现 Instantiator

use Boson\WebView\Api\WebComponents\Instantiator\WebComponentInstantiatorInterface;
use Boson\WebView\Api\WebComponents\ReactiveContext;
use Boson\WebView\WebView;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

final readonly class TwigComponentInstantiator implements WebComponentInstantiatorInterface
{
    private Environment $twig;

    public function __construct()
    {
        $this->twig = new Environment(new ArrayLoader());
    }

    public function create(WebView $webview, ReactiveContext $context): object
    {
        $component = $context->component;

        if (\is_subclass_of($component, TwigComponent::class)) {
            return new $component($this->twig, $context, $webview);
        }

        return new $component($context, $webview);
    }
}

第四步:注册组件系统配置

$webComponentsConfig = new WebComponentsCreateInfo(
    instantiator: new TwigComponentInstantiator(),
);

$applicationConfig = new ApplicationCreateInfo(
    window: new WindowCreateInfo(
        webview: new WebViewCreateInfo(
            webComponents: $webComponentsConfig,
        ),
    ),
);

$app = new Boson\Application($applicationConfig);

🧪 实战:创建一个 Twig 列表组件

class MyTwigComponent extends TwigComponent
{
    protected array $items = [1, 2, 3];

    protected function renderTwig(): string
    {
        return <<< 'twig'
            <ul>
                {% for item in items %}
                <li>{{ item }}</li>
                {% endfor %}
            </ul>
        twig;
    }
}

注册组件:

$app->webview->defineComponent('my-list', MyTwigComponent::class);
$app->webview->html = '<my-list />';

📦 分发部署

一旦开发完成,只需使用 Boson 编译器打包为可执行文件,即可直接分发给 Windows/macOS/Linux 用户,无需用户安装 PHP 或其他依赖环境。


🧭 总结

Boson 是专为 Web 和 PHP 开发者打造的跨平台桌面应用开发平台。它的出现,打破了传统 Electron 一统桌面端的局面,为 PHP 开发者提供了构建本地 GUI 应用的新可能:

  • 你可以继续使用熟悉的 PHP + HTML + CSS
  • 实现强大的桌面功能访问(文件系统、系统窗口等)
  • 减少打包体积,提升运行性能
  • 更贴近原生体验,不依赖繁重运行时

这是属于 PHP 开发者的桌面时代。


项目地址:

如果你热衷于 PHP 开发,不妨试试 Boson —— 让 PHP 不再只属于后端!

复制全文 生成海报 PHP 桌面应用 Web开发 开源技术 跨平台

推荐文章

Vue3中如何处理状态管理?
2024-11-17 07:13:45 +0800 CST
MySQL用命令行复制表的方法
2024-11-17 05:03:46 +0800 CST
使用 Git 制作升级包
2024-11-19 02:19:48 +0800 CST
git使用笔记
2024-11-18 18:17:44 +0800 CST
Vue 中如何处理父子组件通信?
2024-11-17 04:35:13 +0800 CST
JavaScript设计模式:发布订阅模式
2024-11-18 01:52:39 +0800 CST
PHP 的生成器,用过的都说好!
2024-11-18 04:43:02 +0800 CST
H5端向App端通信(Uniapp 必会)
2025-02-20 10:32:26 +0800 CST
如何在Vue3中定义一个组件?
2024-11-17 04:15:09 +0800 CST
介绍Vue3的Tree Shaking是什么?
2024-11-18 20:37:41 +0800 CST
html一份退出酒场的告知书
2024-11-18 18:14:45 +0800 CST
JavaScript中设置器和获取器
2024-11-17 19:54:27 +0800 CST
15 个 JavaScript 性能优化技巧
2024-11-19 07:52:10 +0800 CST
Rust 高性能 XML 读写库
2024-11-19 07:50:32 +0800 CST
什么是Vue实例(Vue Instance)?
2024-11-19 06:04:20 +0800 CST
html折叠登陆表单
2024-11-18 19:51:14 +0800 CST
go错误处理
2024-11-18 18:17:38 +0800 CST
Python 获取网络时间和本地时间
2024-11-18 21:53:35 +0800 CST
H5保险购买与投诉意见
2024-11-19 03:48:35 +0800 CST
Vue3中的v-slot指令有什么改变?
2024-11-18 07:32:50 +0800 CST
Nginx 防止IP伪造,绕过IP限制
2025-01-15 09:44:42 +0800 CST
程序员茄子在线接单