Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sebastian De Deyne:
Server side rendering JavaScript from PHP
Mar 13, 2018 @ 15:21:24

Sebastian De Deyne has a tutorial posted to his site showing you how to use two packages to render Javascript on the server side via PHP.

Server side rendering is a hot topic when it comes to client side applications. Unfortunately, it's not an easy thing to do, especially if you're not building things in a Node.js environment.

I published two libraries to enable server side rendering JavaScript from PHP: spatie/server-side-rendering and spatie/laravel-server-side-rendering for Laravel apps.

Let's review some server side rendering concepts, benefits and tradeoffs, and build a server renderer in PHP from first principles.

The tutorial starts with a look at what "server-side rendering" means in this case, how it works with the browser and some of the benefits. It then talks about the rendering process and some of the tradeoffs between it and just using Javascript on the frontend. From there the post gets to the examples of the actual rendering process. If creates a simple Vue.js application that outputs a "Hello, world!" template. It then includes the code to use the packages (that, in turn, use the V8JS package) to render the Javascript and output the result.

tagged: serverside render javascript tutorial package spatie v8js

Link: https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php

Frank de Jonge:
Rendering ReactJS templates server-side
May 21, 2015 @ 14:17:50

Frank de Jonge has posted a tutorial to his site showing how you can render React.js templates server-side in PHP. He makes use of the V8JS extension to execute Javascript inside of PHP and echo out the rendered result.

The last couple of months I've been working with ReactJS quite extensively. It's been a very rewarding and insightful journey. There is, however, one part that kept coming back to me: server-side rendering. How on earth am I going to use ReactJS when I want to render my templates on the server? So, I sat down and looked at the possibilities.

He suggests two options, running a small Node application or using the V8JS extension, and opts for trying the second option to meet his needs. He talks about the "why" of rendering server-side JS and gives a brief introduction to V8JS and the workflow he'll follow to use it. He helps you get this library via Composer to make working with it easier and provides an example of how to use it. After trying out this method, he then goes back to option #1, the small Node application (what he ended up choosing). He walks through the setup of this application, showing how to set it up inside a Lumen application and using Express to output the generated templates and data. He then hooks this into the PHP application via a simple HTTP client grabbing the results and pushing them back out to the page.

tagged: reactjs template serverside nodejs v8js extension http lumen

Link: http://blog.frankdejonge.nl/rendering-reactjs-templates-server-side/

DZone.com:
Running JavaScript inside PHP code
Mar 01, 2012 @ 15:56:45

On DZone.com Giorgio Sironi has a new post about an interesting new PHP extension that lets you run javascript inside PHP - v8js (VERY alpha right now).

v8js is a new PHP extension able to run JavaScript code inside V8, Google's JavaScript interpreter that powers for example Chrome and NodeJS. This extension is highly alpha - and its API would probably change in the months ahead. Since documentation is lacking, I invite you to repeat the discovering process I follow in this post in case you find some differences in a new version of v8js.

He gives you the (PECL-based) commands to get the extension and it's needed dependencies installed and enabled. He uses PHP's own Reflection features to look at the extension and find its methods including: "executeString", "getPendingException" and "getExtensions". To show it in action, he implements an old standby to test new languages - the FizzBuzz example - in Javascript, executed inside the PHP. He also includes a quick example of how to load in an external Javascript file and execute the results.

tagged: javascript extension execute v8js google interpreter

Link:


Trending Topics: