Over the course of the 6+ years we’ve been developing the PHP compiler to .NET, many practical apps have been tried on PeachPie, both by the core developer team and by the community. In this article, we’ll take a look at some of the coolest projects built with PeachPie.
In case you’re new here, let’s briefly recap what PeachPie is and answer the iconic “But Why” question. PeachPie is a PHP compiler and runtime under .NET — a complete re-implementation of PHP in C#, capable of compiling PHP into CIL bytecode and thus fully interoperable with any language from the .NET family.
The idea behind PeachPie is primarily to allow two of the most popular web development technologies to co-exist, but there are other benefits to running PHP on .NET, e.g. performance enhancements, better security and arguably more professional tooling by Microsoft. The project has been under development since 2016 and is a part of the .NET Foundation.
Easily the most popular real-world app built with PeachPie is WpDotNet, i.e. WordPress running on .NET. This app is created by us, the developers of PeachPie, and is available as an SDK or full-blown project to play with. The truly mindblowing part of this project is that it’s distributed as a NuGet package and is injected into your existing solution by adding a package reference and then injecting WordPress as middleware within your Configure
startup method, like so:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// ...
app.UseWordPress();
// ...
}
}
The line app.UseWordPress();
has fascinated the community for years, prompting even Microsoft legend Scott Hanselman to write a lengthy blog post.
Another one of our own projects including a whole host of ready-made projects is apps.peachpie.io. This site contains a bunch of handy PHP libraries already compiled and ready to be used on .NET. Examples include the Twig templating system, Responsive File Manager or DomPDF.
You can also browse through all the WordPress themes and plugins we’ve tested and compiled — there are currently over 5300 themes and plugins you can use for WpDotNet without any issues.
Even though the implementation is a little outdated by now, MediaWiki (the software that powers Wikipedia) is actually one of the largest and most complex apps we’ve ever managed to run on PeachPie.
You can use this project to run your own Wiki on .NET, although we admit that we haven’t tested this in a while. So if you do give it a shot and you end up running into problems, please do let us know.
Perhaps the most ambitious community project ever attempted is to run Laravel on .NET. Unfortunately, the community member who was attempting to make this happen hasn’t given it a shot in 4 years, although we were extremely close to getting an error-free compilation of possibly the second most popular PHP app.
PeachPie’s development has made massive strides since then, so we assume that we’re likely not far from being able to run Laravel at this point. If you’re interested in making this happen, we’d love it if you gave it a shot and let us know what issues you run into.
Probably the most sophisticated and popular discussion forum app, Flarum, would be a truly valuable addition to the .NET ecosystem. A community member is currently trying to achieve this quest — we have an open issue on this topic. Feel free to add to it!
Another massive PHP app that likely already works, but hasn’t properly been worked on since 2019, is Magento. This e-commerce platform, which is now part of Adobe, has received quite a bit of attention from the community and from us as well, as you can see by the amount of closed issues on this topic.
If you’d like to see Magento on .NET, give it a try and let us know how far you can get.
Yes, many. We frequently get the question of whether PeachPie is production-ready and capable of supporting major commercial projects, and the answer is clearly yes. Major Fortune 500 brands, including Volkswagen AG, for example, use PeachPie to power their mission-critical web apps.
We have clients from the media sector, electronics and insurance industry currently under commercial contracts to integrate their PHP apps with .NET via PeachPie. If you have a usecase for PeachPie and would like to understand whether we can support you commercially, don’t hesitate to get in touch.
PeachPie offers many practical use-cases for both PHP and .NET developers, but some use-cases are a bit more mind-blowing than others. In this article, we’ll discuss how to create websites in PHP and run them on the client side using web assembly and Blazor.
In this post, we combine several technologies: PHP as a scripting language, Blazor as .NET’s framework to write web apps in C# and HTML, web assembly (WASM) as the means with which scripts can be executed within the browser on the client side, and of course PeachPie – the PHP compiler and runtime for .NET.
Our goal here is to allow developers to write scripts in PHP and execute them right in the browser. Why? Here’s why:
In order to get started with PHP on the client side, we’ve created a library (Peachpie.Blazor
) and SDK (Peachpie.Blazor.Sdk
) to work with PeachPie and Blazor.
The first thing you need to do is install the Peachpie.Templates by running the following command
dotnet new -i "Peachpie.Templates::*"
Next, you can choose from one of two pre-configured templates:
Now you’re ready to create the project:
dotnet new project-name
When you’re done, you can add Razor pages to BlazorApp.Cient or even create PHP scripts by adding them to PHPScripts (this, however, is optional).
Finally, launch the Blazor.Server with the command
dotnet run --project BlazorApp\Server
and you’re almost done. When you’re ready, navigate to https://localhost:5001 and check out the result!
Currently, this project is still in its infancy, but it already supports a fairly extensive list of features. The API and functionality might still change to improve the advantages, but here’s what should already work out of the box:
Another very neat feature of this setup is the possibility to debug PHP code that runs in the browser directly in Visual Studio. To try this, run the Blazor.Server in DEBUG
mode and set the breakpoint to any line in your PHP code. The Peachpie.Blazor.Sdk then sets up the environment to enable PHP debugging.
Not only can you debug, but since PhpComponent
and PhpScriptProvider
have many stages, we provide helpful logs describing the behavior of the components. Logs are displayed in the browser console by default. If you want to prohibit logging, just add the following to Program.cs in the Blazor.Client project:
builder.Logging.SetMinimumLevel(LogLevel.None);
Currently, this project is rather experimental, but there are many improvements we could make. For example, we could make rendering with the PhpScriptProvider efficient enough in order to remove PhpComponent and make the usage even more transparent.
Alternatively, we could even use this technology to move entire WordPress sites to the client side – in order to do so, we have to figure out a way of adding support for databases.
If you have any issues or improvement suggestions or would like to help out with the development of this concept, feel free to contribute to the repo:
https://github.com/peachpiecompiler/peachpie-blazor
There are a few tools out there that allow you to create an executable console application in PHP, but they tend to be a little heavy and limited to a certain OS. With PeachPie, console apps can be created straightforwardly, without having to distribute the sources, and it can be debugged right in Visual Studio.
(more…)Almost every post, article or thread on Reddit about PeachPie is inevitably met with someone posting the famed Ryan Reynolds GIF asking “but why?”, so instead of explaining each time, we decided to put our entire motivation behind this project on paper so that we can link to it.
(more…)We’ve been a little less active with blog posts and announcements lately, but it’s not for a lack of activity around the PeachPie platform. Let’s take a look at the current state of PeachPie and how compatible we are with the plethora of PHP applications out there.
(more…)As we make the final preparations before publishing version 1.0, lots of new additions have been made ahead of the latest release of PeachPie. Let’s take a look at what’s new.
(more…)It’s been a fruitful year, as we take a look back at 2018 to see what we achieved and forward to 2019 to set our sights on bigger and better things.
(more…)We’ve had an extension for the lightweight VS Code IDE for quite some time, but now was as good a time as ever to create one for the full Visual Studio.
PeachPie is now a mature project and so it was only about time for it to have a proper documentation as well. That is why we are happy to introduce docs.peachpie.io.
No reasonable WordPress site is complete without some kind of caching plugin. While PeachPie should inherently speed up the runtime of PHP apps, it also opens up the door to another fantastic possibility: the ASP.NET Core response cache.
The web cache is a general issue we will face regardless of language, web server or platform. In the PHP environment specifically, virtually any framework or CMS includes their own implementation or provides a tutorial on how to configure the cache. In this article, we suggest a rather amazing alternative.
PeachPie is a compiler and runtime of the PHP language under .NET and .NET Core. By compiling PHP to CIL bytecode, PeachPie bridges the gap between these two technologies and essentially allows developers to take advantage of to the rich PHP ecosystem combined with the many fantastic tools the .NET world has to offer. One of these tools is the powerful ASP.NET Core response caching middleware, which is what we’ll focus on in this article.
With PeachPie, we find ourselves in the .NET Core environment; a PHP site is compiled and runs as a native .NET application, which allows us to combine the solution with the .NET platform. Moreover, the ASP.NET Core response cache sits directly on the pipeline of the ultimate lightweight web server, which is known to produce blazing fast speeds. This cache is fully configurable thanks to a dependency injection and highly performant. And finally, the cache mechanism is compiled, which means that in the case of a ‘cache hit’, not a single line of PHP code is executed.
If you already know enough, feel free to skip right to the results; here is a link to a GitHub project that compiles all of WordPress and handles the requests in your ASP.NET Core app using this compiled version of WordPress.
Take note of:
[csharp]
app.UseMiddleware<ResponseCachingMiddleware>()
[/csharp]
Thanks to the combination of PeachPie and ASP.NET Core, this will switch on the server-side in-memory response cache in your PHP web.
Each application has its specific behavior and the cache needs to be refreshed intelligently. The standard Response Cache setting goes by the response headers configured by the website’s code; if the PHP code correctly sets the expiration and similar parameters and there is no need to explicitly delete the cache, everything will work out of the box.
Applications such as WordPress, however, don’t do this and even more so, the cache needs to be dumped when, for example, a comment is added to an article or a blog post was edited. It is also not advisable to cache anything (or only some sites) for a logged in user, seeing as some of the content will be specific to that user. This can be achieved by implementing one’s own IResponseCachingPolicyProvider
, as seen here:
The instance of this object is injected into the ResponseCachingMiddleware
construct. The object defines the cache’s behavior – whether to save a specific Http Request and whether to subsequently store the Http Response in the cache. In our case, the interconnection with PHP is more interesting, however.
Our goal is to subsequently call our IResponseCachingPolicyProvider
and tell it to dump the cache. In the case of WordPress, we designed and implemented a C# add-on that defines the IWpPlugin
interface, among others. This enables us to add so called hooks during each request to WordPress and thus connect to the ‘callback’ API of WordPress. This procedure will be very similar regardless of which PHP project we are using.
Using WordPress as an example, we are adding an event handler into the WordPress filter “save_post”, as seen here:
[csharp]
app.AddFilter("save_post", updated)
[/csharp]
WordPress will then call us when this event will be triggered and we can thus store the current time. All following requests to the cache merely return the cached content created before this time.
The main advantage of this new mechanism rests in its simplicity and versatility. Our solution is identical and universal across any PHP website without having to change the PHP code or modify its logic. Nevertheless, measuring PHP code in this particular area makes little to no sense. No PHP code is being executed and the benchmarks are thus identical with the aforementioned TechEmpower benchmarks.
The process is as follows: a request reaches the server – the ResponseCache sits first in the request pipeline – it finds the cached content and instantly returns it – end of request. However, in the future we are planning on preparing a comparison with the popular WP-Super-Cache plugin, which our built-in ASP.NET Core response cache renders obsolete (when running WordPress on .NET). This plugin has several modes – the one that can be compared with ours in terms of speed, however, requires a modification of the Apache configuration file; thanks to this, Apache routes the request directly to the cached file on the disk.
All in all, thanks to the ASP.NET Core Response Cache, we can serve a majority of WordPress requests with a minimal effort in about 4 ms.
You can find our WordPress SDK to study and analyze here.