dev-master
9999999-dev
The Requires
- ext-v8js >=0.1.3
The Development Requires
by Yosh de Vos
Wallogit.com
2017 © Pedro Peláez
This library integrates react.js in PHP using the v8js extension to allow server-side rendering. It also contains a Twig extension to embed react component rendering inside Twig templates., (*1)
You need the V8Js PHP extension to run this library., (*2)
To install this extension follow the official installation guides: * On Linux * On MacOS * On Windows, (*3)
You can also use one of my Docker images: * php-fpm-v8js * php-fpm-v8js-dev, (*4)
composer require yoshz/reactjs-php-bridge
bower install --save react
Using the renderer directly:, (*5)
$reactjs = new \ReactJsBridge\Renderer(array(
'react_path' => __DIR__ . /bower_components/react',
'app_paths' => array(
__DIR__ . '/app'
)
));
?>
<html>
<head>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
</head>
<body>
<main>
<?= $reactjs->getComponentMarkup('HelloWorld') ?>
</main>
// initialize client-side javascript
<script><?= $reactjs->getClientJs() ?></script>
</body>
</html>
Or using the Twig extension:, (*6)
$reactjs = new \ReactJsBridge\Renderer(array(
'react_path' => __DIR__ . /bower_components/react',
'app_paths' => array(
__DIR__ . '/components'
)
));
$ext = new \ReactJsBridge\TwigExtension($reactjs);
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new \Twig_Environment($loader);
$twig->addExtension($ext);
echo $twig->loadTemplate('helloworld.html.twig')->render();
And the Twig file templates/helloworld.html.twig:, (*7)
<html>
<head>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
</head>
<body>
<main>
{{ reactjs_component('HelloWorld') }}
</main>
{{ reactjs_clientjs() }}
</body>
</html>
This library is inspired by the reactjs/react-php-v8js library., (*8)