使用 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 不再只属于后端!