How to Work with Peachpie Samples

We regularly try to produce samples for everyone to try out how to work with Peachpie compiler. These samples are usually common usecases where we show how to get started with a Peachpie-powered application quickly. In this article, we would like to give a detailed tutorial on how to get the samples working and what you can achieve with them.

Prerequisites

Before you can start working on any samples, you first need to:

  1. Download .NET Core 1.0
  2. Download or fork our samples
  3. Optionally download Visual Studio Code. You can also work with other IDEs that  support .NET Core (the full Visual Studio, MonoDevelop (we cannot guarantee the quality here) or VS for Mac)

Note – .NET Core 1.1:

Peachpie version 0.5.0 supports .NET Core 1.1. However, as of now, you also need to have .NET Core 1.0, which will be used during the compilation. In order to use .NET Core 1.1, you will need to modify project.json. Therefore, we only recommend this option for advanced users.

1. Downloading .NET Core

We support all platforms where .NET Core works. The official platforms and installers are available here.

Choose whatever applies to your system; in our case, we will use the installer for 64bit Windows:

netcore10downloadpage

2. Running the sample

The Peachpie samples are intended to demonstrate one particular construct or example of how to work with the compiler. You can either work with the samples on the command line or from the comfort of the Visual Studio Code IDE.

For simplicity’s sake, let’s start with the command line/terminal.

First, try to run the simplest sample, just as it is. Open the command line/terminal in the directory `peachpie-samples/console-application` and type the following commands:

  • `dotnet restore`
  • `dotnet run`

The first command downloads the necessary dependencies and updates the project.json.lock file. To find out more about this process, check out: https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore.


C:\Users\jmise\Projects\peachpie-samples\console-application> dotnet restore
log : Restoring packages for C:\Users\jmise\Projects\peachpie-samples\console-application\project.json...
log : Restoring packages for tool 'Peachpie.Compiler.Tools' in C:\Users\jmise\Projects\peachpie-samples\console-application\project.json...
log : Writing lock file to disk. Path: C:\Users\jmise\Projects\peachpie-samples\console-application\project.lock.json
log : C:\Users\jmise\Projects\peachpie-samples\console-application\project.json
log : Restore completed in 3917ms.

The second command executes the compilation of the PHP file and runs the resulting program.


C:\Users\jmise\Projects\peachpie-samples\console-application> dotnet run
Project console-application (.NETCoreApp,Version=v1.0) will be compiled because project is not safe for incremental compilation. Use --build-profile flag for more information.
Compiling console-application for .NETCoreApp,Version=v1.0
Peachpie PHP Compiler.

Compilation succeeded.
 0 Warning(s)
 0 Error(s)

Time elapsed 00:00:02.9096366
 (The compilation time can be improved. Run "dotnet build --build-profile" for more information)

Hello .NET World!

Peachpie utilizes the compiler that was downloaded as a part of the `dotnet restore` command. What is compiled and how is specified in the project.json file, which we will discuss in more detail below.

3. Modifying the sample

There are two kinds of modifications we will look at:

  1. The easiest one is to change the PHP code. Try making any chance to the index.php file. Please keep in mind, however, that Peachpie is still a work in progress. Some PHP constructs and functions are not supported by the compiler yet. Check out our roadmap to see a full list of supported constructs.
  2. We can also alter the project.json file. Here we can change some of the files that can be included in the program or specify which dependencies on NuGet packages the project will have. If you are interested in how this process works, check out the complete project.json documentation here.

4. Working with Visual Studio Code

When you have a larger project, you probably want to work in the more comfortable setting of an IDE. We have a practical extension for Visual Studio Code, which is a free, yet quite powerful and efficient development environment that fully supports .NET Core, PHP syntax highlighting and .NET debugging – all of which you have at your fingertips thanks to compiling with Peachpie.

Check out this article, where we show how to work on a C#/PHP interoperability sample within VSCode. Alternatively, you can also watch the video below:

Follow our progress on GitHub and stay tuned for more updates on Twitter or Facebook.

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Loading Facebook Comments ...

Leave a Reply