php开发桌面应用程序_使用PHP开发跨平台桌面应用程序的3种方法
php开发桌面应用程序
PHP as a cross-platform desktop app development language? Blasphemy! Nonetheless, it’s possible.
PHP作为跨平台的桌面应用程序开发语言? *! 尽管如此,还是有可能的。
A few years ago, everything those interested in bringing PHP to the desktop had had was the now long abandoned GTK PHP. Since then, new players have appeared, though let’s first answer the “why”.
几年前,那些有兴趣将PHP引入桌面的人所拥有的,就是已经废弃了很长时间的GTK PHP 。 从那以后,新玩家出现了,尽管我们首先回答“为什么”。
为什么? (Why?)
Why would anyone develop cross platform PHP apps for the desktop? Why not opt for something that can actually tie into the low level APIs of the operating system, like Adobe AIR? Why not go with something outdated and bloated but reliable, like Java? Why not make it a Chrome app and if you need native support, use Native Client? Hell, if you want a scripting language, why not just go with Python? Everything goes, as long as we avoid having to bundle a server with the whole shebang, right?
为什么有人会为台式机开发跨平台PHP应用程序? 为什么不选择实际上可以与操作系统的低级API绑定的内容,例如Adobe AIR? 为什么不选择Java之类的过时,肿但可靠的东西呢? 为什么不将其配置为Chrome应用?如果您需要本机支持,请使用本机客户端? 天哪,如果您要使用脚本语言,为什么不只使用Python? 只要我们避免将服务器与整个服务器捆绑在一起,一切都会顺利进行,对吧?
Off the top of my head, I can think of several far fetched scenarios:
我脑海中浮现出一些牵强的场景:
- You need a good middle ground between easy syntax and good structure, which is PHP, and you can’t be bothered to learn new languages like ActionScript 您需要在简单的语法和良好的结构之间找到良好的中间立场,这就是PHP,并且您也不必费心学习ActionScript之类的新语言。
- You’re running IT in a company with highly computer illiterate people, and the only way to force them into using a good browser for your company app is to embed it into the app you deliver. It’s still a web app, but opens in a headless Chrome! 您在一家计算机知识渊博的公司中运行IT,迫使他们为公司应用程序使用好的浏览器的唯一方法是将其嵌入到您提供的应用程序中。 它仍然是一个网络应用程序,但是可以在无头Chrome中打开!
- You want to avoid paying hosting costs for your own personal application, and you like to carry it with you on a USB stick. You just plug it in, run it, and your app is there – using the same SQLite DB from before. If you need to sync online, you send the whole DB export to Dropbox or some such service at the click of a button, thus making sure you’re literally the only one who can access your “web app” even without your computer. 您希望避免为自己的个人应用程序支付托管费用,并且希望将其随身携带在USB记忆棒上。 您只需插入它,运行它,然后您的应用程序就在这里–使用以前的相同SQLite DB。 如果您需要在线同步,只需单击一下按钮,即可将整个数据库导出发送到Dropbox或某些此类服务,从而确保您实际上是即使没有计算机也可以访问“ Web应用程序”的人。
- You don’t need low level OS API access – you just want to make a browser based game, or a helper app, or something similarly simple. PHP is perfectly fine for that, and you already know the language. 您不需要低级的OS API访问权限–您只想制作一个基于浏览器的游戏,一个助手应用程序或类似的简单应用。 PHP完全可以做到这一点,并且您已经知道该语言。
These scenarios might look like grasping at straws, and indeed, I really can’t think of a REAL, practical reason to want to do it that doesn’t have a viable alternative. Still, it’s nice to know it’s possible. Let’s see how.
这些场景可能看起来像是在吸管,但实际上,我真的想不出一个真正可行的理由来做,但没有可行的选择。 不过,很高兴知道这是可能的。 让我们看看如何。
1.夜雨 (1. Nightrain)
Nightrain is a pre-packaged set of PHP-hosting prerequisites powered by PHP 5.5.x at the moment. It’s a packager written in Python that uses PHP’s internal server to host your app, thus avoiding Apache and Nginx and minimizing configuration shenanigans. However, this also means some more advanced aspects are unavailable, and you can only really use it for very rudimentary apps.
Nightrain是目前由PHP 5.5.x支持的一组预打包PHP托管先决条件。 这是一个用Python编写的打包程序,它使用PHP的内部服务器托管您的应用程序,从而避免了Apache和Nginx并最大程度地减少了配置隐患。 但是,这也意味着某些更高级的方面不可用 ,并且您只能将其真正用于非常初级的应用程序。
Another big con is that on Windows, a command prompt is launched first, and then the “app”. The command windows must stay open if you want to use the app, and this might be more than a little confusing to the technically illiterate people of scenario 2) above.
另一个大缺点是,在Windows上,首先启动命令提示符,然后启动“ app”。 如果要使用该应用程序,则命令窗口必须保持打开状态,这可能会使上述情况2)的技术水平不高的人感到困惑。
What’s more, you can only run one nightrain app by default, because it actually launches a server on port 8000 and then makes the headless browser that opens “secretly” visit localhost:8000
. If you want to launch several different nightrain apps, you need to change the port in settings.ini
. This also means that simply visiting localhost:8000
in your host machine’s browser will show you the same app.
此外,默认情况下,您只能运行一个nightrain应用程序,因为它实际上在端口8000上启动了一个服务器,然后使无头浏览器“秘密”打开访问localhost:8000
。 如果要启动几个不同的Nightrain应用程序,则需要在settings.ini
更改端口。 这也意味着,只要在主机浏览器中访问localhost:8000
就会显示相同的应用程序。
Nightrain is compatible with most PHP apps/frameworks out of the box, as long as you change the database to SQLite, which is what’s used, and tweak the bundled php.ini
for some missing extensions, if any. MySQL is not bundled and installing it alongside the regular stack is no simple matter. It’s very simple to make the app send the SQLite data upstream to a server you use for a centralized database anyway, so using only SQLite on the system where the app is running is somewhat logical.
只要将数据库更改为SQLite(即使用的数据库),并调整捆绑的php.ini
中的某些缺少的扩展名(如果有),Nightrain即可与大多数PHP应用程序/框架兼容。 没有捆绑 MySQL,将其与常规堆栈一起安装绝非易事。 无论如何,使应用程序将上游SQLite数据发送到用于集中式数据库的服务器非常简单,因此在运行该应用程序的系统上仅使用SQLite有点合乎逻辑。
By far the biggest drawback of the app is that it uses WX widgets to power the headless browser, and on Windows, this seems to come down to IE7. Changing it seems possible, by means of WXPython as mentioned in the issue linked above, but hasn’t yet been attempted. One can only hope the browser object will be updated to something more usable some time soon – until then, and until all the other critical drawbacks are fixed, I can’t even begin to imagine a use for Nightrain.
到目前为止,该应用程序的最大缺点是它使用WX小部件来驱动无头浏览器,在Windows上,这似乎可以归结为IE7 。 通过上面链接的问题中提到的WXPython来更改它似乎是可能的,但是尚未尝试过。 只能希望浏览器对象不久后会更新为更有用的功能-直到那时,直到解决所有其他关键缺陷,我什至无法开始想象Nightrain的用途。
2. WXPHP (2. WXPHP)
wxPHP stands for “wxWidgets for PHP” and is a PHP extension that wraps the wxWidgets library, which allows to write multi-platform desktop applications that make use of the native graphical components available to the different platforms. – Wikipedia
wxPHP代表“ wxWidgets for PHP”,并且是一个PHP扩展,它包装了wxWidgets库,该库允许编写利用跨平台使用的本机图形组件的多平台桌面应用程序。 – *
You install wxPHP as a separate program, which then gives you support for execution of .wxphp
files by simply doubleclicking on them.
您将wxPHP安装为单独的程序,然后只需双击它们即可为执行.wxphp
文件提供支持。
This means your applications are mere files, and you can distribute them everywhere with ease. You can organize your code into files and classes as usual and distribute folders. The main .wxphp
file can then include these other resources.
这意味着您的应用程序仅仅是文件,您可以轻松地将它们分发到任何地方。 您可以像往常一样将代码组织到文件和类中,并分发文件夹。 然后,主.wxphp
文件可以包含这些其他资源。
The installation comes with several examples, including one which initializes WebView and loads the wxPHP website in a wx frame. One thing to note is that with wxPHP you aren’t developing websites as you would on the web. In other words, you don’t develop offline websites, but string together various wx widgets. As such, the library has a bit of a learning curve, and you’ll be lacking the HTML5 features you might be used to, or the simplicity of web development. There is some Proof of Concept of the internal PHP server running and serving requests, but that’s experimental and complex, and once again exposes the localhost
, just like Nightrain.
该安装附带几个示例,其中一个示例初始化WebView并将wxPHP网站加载到wx框架中。 需要注意的一件事是,使用wxPHP并不会像在Web上那样开发网站。 换句话说,您不是开发离线网站,而是将各种wx小部件串联在一起。 因此,该库有一些学习曲线,您将缺少可能习惯HTML5功能或Web开发的简单性。 内部PHP服务器运行并处理请求有一些概念证明 ,但这是实验性的和复杂的,并且像Nightrain一样再次公开了localhost
。
wxPHP also comes with an adorable form building tool which will help you automatically generate the PHP code you need for your wxPHP apps by means of a wysiwyg editor.
wxPHP还附带了一个可爱的表单构建工具,该工具将通过wysiwyg编辑器帮助您自动生成wxPHP应用程序所需PHP代码。
Before you dismiss wx as trivial, people have developed more than basic apps in it. For example, here’s a PHP Editor with remote debugging and a plugin API.
在您将wx视为琐碎的事之前,人们已经开发出了比基本应用更多的东西。 例如,这是一个具有远程调试功能和插件API的PHP编辑器 。
If you’re serious about PHP desktop development, wxPHP is by far the better option when compared to Nightrain, even though Nightrain lets you write good old HTML for GUI.
如果您对PHP桌面开发很认真,那么与Nightrain相比,wxPHP是更好的选择,即使Nightrain允许您为GUI编写良好的旧HTML。
One of the biggest advantages of wx here is the fact that once installed, all .wxphp
files can be run at the click of the mouse. No additional installs, no awkward console windows. For technically illiterate people, that’s a godsend – you can easily distribute the app inside your company via a simple email, and the update procedure is as simple as overwriting a file.
wx的最大优点之一是,安装后,所有.wxphp
文件都可以通过单击鼠标运行。 没有其他安装,没有笨拙的控制台窗口。 对于不懂技术的人来说,这真是天赐良机–您可以通过简单的电子邮件轻松地在公司内部分发应用程序,更新过程就像覆盖文件一样简单。
3. TideSDK (3. TideSDK)
TideSDK has a somewhat different approach than the above two. You install an SDK to be able to develop applications, and each platform has certain prerequisites. TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.
TideSDK与上述两种方法有些不同。 您安装一个SDK就能开发应用程序,并且每个平台都有某些先决条件。 TideSDK实际上是重命名的Titanium Desktop项目。 Titanium仍然专注于移动设备,放弃了台式机版本,该版本已被开源并将其称为TideSDK的一些人接管。
Once installed as per the Getting Started guide, and once we have the TideSDK Developer app (a helper application which will guide us in bundling our application into a distributable package), we can get started developing. Apps you build with Tide (via the helper app, or via the command line) will be both distributable as purely executable, or can be distributed as installable packages which get the whole “app” treatment, including an installation procedure embedded, making them uninstallable via Add/Remove Programs on Windows or your package managers on other operating systems.
按照入门指南进行安装后,一旦有了TideSDK Developer应用程序(一个辅助应用程序,它将指导我们将应用程序捆绑到可分发的程序包中),便可以开始开发。 使用Tide构建的应用程序(通过助手应用程序或通过命令行)将既可以作为纯可执行文件分发,也可以作为可安装包分发,从而获得完整的“应用程序”处理,包括嵌入式安装过程,使其可卸载通过Windows上的“添加/删除程序”或其他操作系统上的程序包管理器。
Applications resources are used in conjunction with a WebKit client and a familiar and extensive API. The API is privileged, providing filesystem access that allows you to read and manage files. APIs are also provided to create and interact with a local database. Network API allows to create clients and servers or to interface with HTTP at a much lower level. It is also possible to open socket connections to other services.
应用程序资源与WebKit客户端和熟悉且广泛的API结合使用。 该API具有特权,可提供文件系统访问权限,使您可以读取和管理文件。 还提供了API以创建本地数据库并与之交互。 网络API允许创建客户端和服务器,或者在更低的级别上与HTTP接口。 也可以打开与其他服务的套接字连接。
Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like Python, Ruby and PHP as well. The engine behind the rendering is WebKit which means it’ll be somewhat slow to start, but it’ll support the latest web technologies.
通常,TideSDK使用HTML,CSS和JS来呈现应用程序,但它也支持脚本语言,例如Python,Ruby和PHP。 渲染背后的引擎是WebKit,这意味着启动起来会有些慢,但是它将支持最新的Web技术。
The heart of TideSDK is an object bridge compiled into the WebKit component. The bridge allows other scripting languages – python, php or ruby – to run on the HTML page using script tags in the DOM, just like JavaScript. You can also directly call .py, .rb or .php files from within your application.
TideSDK的核心是编译到WebKit组件中的对象桥。 该桥允许其他脚本语言(如python,php或ruby)使用DOM中的脚本标签在HTML页面上运行,就像JavaScript一样。 您也可以在应用程序中直接调用.py,.rb或.php文件。
PHP is activated by adding a module statement to the manifest file, like so:
通过向清单文件添加模块语句来**PHP,如下所示:
#appname:HelloWorld
#appid:com.tidesdk.helloworld
#publisher:Software in the Public Interest (SPI) Inc
#image:default_app_logo.png
#url:http//tidesdk.org
#guid:845e9c3c-c9ff-4ad4-afdf-9638092f044f
#desc:Sample Hello World application
#type:desktop
runtime:1.3.1-beta
app:1.3.1-beta
codec:1.3.1-beta
database:1.3.1-beta
filesystem:1.3.1-beta
media:1.3.1-beta
monkey:1.3.1-beta
network:1.3.1-beta
platform:1.3.1-beta
process:1.3.1-beta
ui:1.3.1-beta
worker:1.3.1-beta
php:1.3.1-beta
Note that using the script modules for scripting languages will incur significant performance penalties on the installation and runtime of your app(s).
请注意,将脚本模块用于脚本语言将对应用程序的安装和运行时造成重大的性能损失。
Interestingly, TideSDK features an object bridge which lets you, when using PHP in your apps, convert data seamlessly from JS to PHP and back. You can read more here, but a detailed TideSDK tutorial is coming soon.
有趣的是,TideSDK具有一个对象桥,当您在应用程序中使用PHP时,它使您可以将数据无缝地从JS转换为PHP,然后再转换回PHP。 您可以在此处了解更多信息 ,但是即将推出详细的TideSDK教程。
There are several major downsides to using TideSDK for PHP desktop app development:
使用TideSDK进行PHP桌面应用程序开发有几个主要缺点:
- The PHP development workflow is severely underdocumented and highly susceptible to bugs, but almost impossible to debug. PHP开发工作流文档严重不足,极易出现错误,但几乎无法调试。
-
The bundled PHP version is terribly outdated – version 5.3.X at the time of this writing. While it’s relatively easy to replace it with an up-to-date one through the
/modules
folder in the SDK’s installation directory, it’s an additional nuisance and lacks many modern PHP features which might come in handy in desktop app development, not to mention the built-in server which also might get an esoteric use case here.捆绑PHP版本已经过时了-在撰写本文时,版本为5.3.X。 尽管通过SDK安装目录中的
/modules
文件夹将其替换为最新版本相对容易,但这是一个额外的麻烦,并且缺少许多现代PHP功能,这些功能可能会在桌面应用程序开发中派上用场,更不用说了内置服务器,在这里也可能会得到深奥的用例。 -
There is a learning curve. The DOM API is different from what you may be used to in web development. To echo something on screen, you would need to call
$document->write()
rather thanecho
. It’s a minor difference, but it isn’t well documented and can trip you up.有一个学习曲线。 DOM API与您在Web开发中可能使用的API不同。 要在屏幕上回显某些内容,您需要调用
$document->write()
而不是echo
。 差异不大,但没有充分的文档证明,可能会使您绊倒。 - By far the biggest downside is the compilation. The package you get by building an app is bound to the platform you’ve built it on. To build the app for multiple environments, you need to HAVE those multiple environments. The Windows/Linux disparity is easily solved with virtual machines (though easier to solve if your host is Windows and you have Linux VMs than the other way around), but good luck compiling it for OS X unless you’ve got an OS X device, too. 到目前为止,最大的缺点是编译。 通过构建应用程序获得的程序包已绑定到您基于其构建的平台。 要为多个环境构建应用程序,您需要具有多个环境。 Windows / Linux的差异可以通过虚拟机轻松解决(尽管如果主机是Windows且具有Linux VM,则比其他方法更容易解决),但是除非您拥有OS X设备,否则好运为OS X编译它也一样
TideSDK is a neat option, but it’s far from usable. It’ll do great for HTML/CSS/JS delivery, but when it comes to PHP, I believe wxPHP is still your best bet.
TideSDK是一个很好的选择,但远非可用。 对于HTML / CSS / JS交付,它将做得很好,但是对于PHP,我相信wxPHP仍然是您最好的选择。
其他选择 (Other options)
There are other options available too, but they’re underwhelming to say the least.
也有其他可用的选项,但至少可以说,它们让人感到不知所措。
-
PHPDesktop is similar to Nightrain, but is Windows only. On the other hand, it comes bundled with the Mongoose server and can thus execute parallel requests. It also runs Chrome, which means it’s slow to render/open but supports the latest web technologies. Its Windows-exclusivity disqualifies it from this list of multi-platform solutions, though.
PHPDesktop与Nightrain类似,但仅Windows。 另一方面,它与Mongoose服务器捆绑在一起,因此可以执行并行请求。 它还运行Chrome,这意味着渲染/打开速度很慢,但支持最新的网络技术。 但是,它的Windows独占性使其无法从此多平台解决方案列表中获得资格。
-
Webinder and PHP GTK are both severely outdated and probably shouldn’t be used. In addition to that, Webinder is Windows-only.
结论 (Conclusion)
While the reasons for building PHP apps for the desktop vary from use case to use case and often aren’t easy to justify, I believe it’s good to know the option is there, should you ever need it.
尽管为每个案例构建用于桌面PHP应用程序的原因各不相同,并且通常不容易证明其合理性,但我相信很高兴知道是否存在该选项(如果您需要)。
Have you developed desktop apps in PHP? If so, please, please let me know why and which tech you’ve used – I would absolutely love to have you write about your experiences. Know of any other options? Let me know in the comments!
您是否使用PHP开发了桌面应用程序? 如果是这样,请告诉我您使用的原因和技术,我非常希望您能写下自己的经历。 知道其他选择吗? 在评论中让我知道!
翻译自: https://www.sitepoint.com/3-ways-develop-cross-platform-desktop-apps-php/
php开发桌面应用程序
下一篇: vue项目打包为桌面应用程序