The. NET core ecosystem is becoming more and more powerful as the power of the open source community continues to grow, and sometimes interesting projects such as Peachpie, which make PHP's code migration to. Net core projects, become possible.
It's easier to understand the features from the start by creating a simple starter program.
First install the Peachpie template:
dotnet new -i Peachpie.Templates::*
Then create the project:
dotnet new web -lang PHP -o helloPHP
Then switch the directory to the server folder to run the program:
cd Serverdotnet run
Finally access the HTTP://LOCALHOST:5004/via the browser:
The peachpie mechanism is simple, compiling PHP files into the DLL files of the. NET platform, which can be called by other programs as normal. NET code libraries.
It consists of three parts:
- Compiler (Peach.exe or PEACHPIE.NET.SDK) to compile the PHP file into a. NET Assembly
- Runtime (Peachpie.Runtime.dll), the functionality required for the provider to run
- Code base (Peachpie.Library.dll), functions that contain PHP, classes, and other implementations
Go back to the beginning of the entry code to explore, you can find that the key point is in the website directory in the Website.msbuildproj file:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Library</OutputType> <TargetFramework>netstandard2.0</TargetFramework> <AssemblyName>helloPHP</AssemblyName> </PropertyGroup> <ItemGroup> <Compile Include="**/*.php" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Peachpie.Compiler.Tools" Version="0.9.0-*" /> <PackageReference Include="Peachpie.NET.Sdk" Version="0.9.0-*" PrivateAssets="Build" /> </ItemGroup></Project>
With both Peachpie.Compiler.Tools and PEACHPIE.NET.SDK, all PHP files, including index.php files, will eventually be compiled into a DLL file.
It is also very simple to use this DLL file in the Configure method of the Startup class:
app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "helloPHP"));
The PHP site that runs in ASP.
The Peachpie project is still in the development phase, but does not affect its ability to do some amazing work, for example, its official website is built on WordPress, but has been compiled by Peachpie to run in ASP. You will never expect this website to be written in PHP by the following diagram:
More detailed information about this can be found in its blog.
If you're interested in migrating PHP code to the. NET platform, you can access its repository on GitHub, which is great if it can be a building block.
. Net Core Development Log--peachpie