Render a Razor Partial View on a PHP page

We discussed rendering a PHP file in a Razor View last week, but this week we’ll push the boundaries of interoperability between PHP and ASP.NET even further. What if we enriched a PHP website with Razor Partial Views coded in a C# project (those with the .cshtml extension)?

Introduction

PeachPie is a compiler and runtime for the PHP language on the .NET platform. An existing (or new) PHP app is compiled to a .DLL file (or even packed into a NuGet) and consequently runs on the standard .NET Core runtime; moreover, it can be referenced from a C# project or vice versa – you can also reference a C# project, NuGet or other .NET DLLs from this project.

Run a PHP site on ASP.NET Core

If you check out the samples at https://github.com/iolevel/peachpie-samples/tree/master/mvc, you can find a project called “php-library” containing some PHP files. This could be a small or very large PHP project. Its project file specifies that all *.php files will be compiled using the PeachPie compiler. The other project, “render-partial-view-within-php”, is a regular C# MVC containing a Razor Partial View. Additionally, it references the PHP project, so when compiled, php-library.dll is copied together with the C# MVC app.

In order to handle requests to .php files compiled into php-library in the C# MVC app, we must configure the ASP.NET Core pipeline in Startup.cs as follows:

app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "php-library"));

That’s it. Now start the project and a request to ‘index.php’ will be served.

Render a Razor Partial View from PHP

Now that the site is up and running, we can extend a PHP page by rendering a Razor Partial View inside it. The sample C# MVC app already contains a shared partial view called “_User.cshtml”. Lets focus on the php-library project now;

index.php:

Peachpie\AspNetCore\Mvc\HttpContextExtension::Partial(string $viewName, object $model) is a helper function that does the trick – it finds the partial view “_User”, renders it and retrieves its output. It is then “echoed” using PHP’s <?= syntax.

php-library.msbuildproj:

<PackageReference Include="Peachpie.AspNetCore.Mvc" Version="0.9.0-CI01034" />

The PHP project contains an additional reference that implements the helper function above. This helper function does all the dirty work, such as getting the necessary Razor services, looking up the view, constructing some ViewContext so that things work out, etc. As a result, we have a single-line syntax for rendering a (pre-compiled) Razor Partial View on a PHP page.

View Model

This API supports passing a model instance as well. Notice the second argument new User. The beauty of PeachPie and of this sample is that the type of this Model can be either in PHP or C#.

Hopefully you liked our mini series on PHP/Razor interoperability possibilities. Let us know if this was useful or what other tutorials you would like to see in the future.

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Posted on August 7, 2018, in category Information, Tutorial, tags: , , , , , , , , , ,