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

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

使用 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开发 开源技术 跨平台

推荐文章

Plyr.js 播放器介绍
2024-11-18 12:39:35 +0800 CST
pip安装到指定目录上
2024-11-17 16:17:25 +0800 CST
全栈利器 H3 框架来了!
2025-07-07 17:48:01 +0800 CST
使用xshell上传和下载文件
2024-11-18 12:55:11 +0800 CST
Vue中的`key`属性有什么作用?
2024-11-17 11:49:45 +0800 CST
PHP 8.4 中的新数组函数
2024-11-19 08:33:52 +0800 CST
如何实现虚拟滚动
2024-11-18 20:50:47 +0800 CST
前端项目中图片的使用规范
2024-11-19 09:30:04 +0800 CST
Vue3的虚拟DOM是如何提高性能的?
2024-11-18 22:12:20 +0800 CST
如何在Vue中处理动态路由?
2024-11-19 06:09:50 +0800 CST
ElasticSearch集群搭建指南
2024-11-19 02:31:21 +0800 CST
Vue3中如何处理SEO优化?
2024-11-17 08:01:47 +0800 CST
H5抖音商城小黄车购物系统
2024-11-19 08:04:29 +0800 CST
20个超实用的CSS动画库
2024-11-18 07:23:12 +0800 CST
在 Docker 中部署 Vue 开发环境
2024-11-18 15:04:41 +0800 CST
程序员茄子在线接单