Much has been said about the interoperability options PeachPie opens up between PHP and .NET code. One of the most practical use cases is combining Razor and PHP Partial Views, which is what today’s article will be about.
Introduction
In summary, our goal is to create something like: @Html.Php(“content.php”).
In other words, we would like to have a Razor view or Partial View (.cshtml) and display the output of a PHP script inside. We can imagine these PHP scripts as Razor Partial Views, but written in PHP.
Let’s take a look at a very practical use case, where we integrate a PHP application into a website’s MVC – the PHP code already defines the components and HTML pieces and we would like to use this code in an ASP.NET Core MVC website without having to rewrite it.
We’ll start off by getting our app to run on PeachPie. Our existing PHP code is transformed into a .NET Core project and all of its content is compiled to a .NET Core Standard .dll
file. From this point forward, everything we had in PHP can be directly utilized in the world of C# and .NET in general.
Peachpie.AspNetCore.Mvc
Starting with PeachPie version 0.9.0-CI01028
, we included the NuGet package Peachpie.AspNetCore.Mvc
, which includes helper extension methods, thanks to which today’s blog is even possible.
Configuration
In order to be able to render PHP scripts in Razor View, several steps need to be taken:
First of all, you need to compile your PHP project to .NET using PeachPie; e.g. as we did with WordPress or MediaWiki. Note that OutputType
must be library
and TargetFramework
is netstandard2.0
, because we are building a library that will be referenced by your C# MVC app.
[code language=”xml”]
<PackageReference Include="Peachpie.AspNetCore.Mvc" Version="1.0.0-*" />
<ProjectReference Include="../myPhpApp/myPhpApp.msbuildproj" />
[/code]
In the last step, we must tell the runtime that it should look for the compiled PHP scripts in myPhpApp.dll
. We must do this once when the application is running; the most elegant way is to insert the code into the Startup
object, specifically into the ConfigureServices
method in the ASP.NET Core configuration.
[code language=”csharp”]
Context.AddScriptReference( Assembly.Load("myPhpApp") );
[/code]
Rendering a PHP View within a Razor View
Now we can come back to the short snippet in the introduction: @Html.Php(“content.php”).
The method Php()
is an extension method defined in Peachpie.AspNetCore.Mvc.HtmlHelperExtension
. Our Razor (partial) view will look as follows:
[code language=”csharp”]
@using Peachpie.AspNetCore.Mvc;
<h1>Razor View</h1>
@Html.Php("test.php")
[/code]
The Php
method actually performs multiple steps:
- It finds the compiled “test.php” in our DLL.
- It creates or uses t he existing PHP Context, which is an object representing a PHP request associated with the current HttpContext object.
- It turns on the output buffering (
ob_start
) on the PHP context. - It performes an include of the PHP script.
- It turns off the output buffering (
ob_end_clean
). - It outputs the result of the PHP script into the
TextWriter
object within the current Razor View.
You can grab the whole sample from [HERE].
Model
What about MVC Model objects? If you want to pass some data to the PHP script, you can do so by specifying the second parameter of the Php("test.php", model)
method. You can pass the current .NET Model
or any other object. To use it, simply refer to $this
variable in the PHP code. If you are not comfortable with using $this
, you can take advantage of the PeachPie API and pass any C# variable into a global PHP variable. Simply call something like
[csharp]
HttpContext.GetOrCreateContext().Globals["mydata"] = (PhpValue)"Hi from C#!";
[/csharp]
This code will create a PHP global variable $mydata
. GetOrCreateContext()
is an extension method that gets you the PHP Context
out of the ASP.NET Core HttpContext
.
Conclusion
There are many practical use cases of the PHP/C# interoperability PeachPie enables. Rendering PHP scripts (like a PHP View) within a Razor View, however, is one of the more amazing ones. It allows for a completely seamless integration of PHP Views into Razor and also for a subsequent gradual migration from PHP to .NET.
Let us know what you think about this possibility!