tystr / react-js-bundle
用于将reactjs集成到symfony的包。通过v8js php扩展为同构应用提供服务器端渲染。
v0.2.0
2016-03-10 07:37 UTC
Requires
- guzzlehttp/guzzle: ^6.1
- reactjs/react-php-v8js: ^1.0
- symfony/config: ~2.7
- symfony/dependency-injection: ~2.7
- symfony/http-kernel: ~2.7
- twig/twig: ^1.23
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-09-24 19:40:43 UTC
README
将React集成到Symfony的包。通过v8js PHP扩展提供服务器端渲染,用于构建同构应用。
安装
使用composer安装tystr/react-js-bundle
# composer.phar require tystr/react-js-bundle:dev-master:~0.1
配置
将包注册到您的应用中
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Tystr\ReactJsBundle\TystrReactJsBundle(), // ... ); }
配置react.js和组件javascript文件的路径
# app/config.yml tystr_react_js: react_path: path/to/react.js components_path: path/to/components.js
默认情况下,使用v8js PHP扩展渲染react组件。如果您希望使用外部服务器渲染react组件,您可以配置外部渲染方法
# app/config.yml tystr_react_js: react_path: path/to/react.js components_path: path/to/components.js render_method: external render_url: https://:3000
这将导致向render_url
值发起一个GET
请求,组件和数据(在这种情况下为{name: Tyler}
)作为查询参数附加到url上
GET https://:3000?component=MyComponent&data=%7B%22name%22%3A%22Tyler%22%7D
用法
{{ react_component('MyComponent', 'my-component') }}
这将服务器端渲染react组件MyComponent
,并将其放置在id为my-component
的div中。
要向组件传递数据,将哈希作为第三个参数传递
{{ react_component('MyComponent', 'my-component', {'name': 'Tyler'}) }}
这使得this.props.name
在MyComponent
中可用。
要使用react_component
函数挂载所有在服务器端渲染的组件,使用react_mount_components
twig函数
<script> {{ react_mount_components() }} </script>
要挂载单个react组件(只要它已经通过react_component
渲染),使用react_mount_component
函数
<script> {{ react_mount_component('MyComponent') }} </script>
尝试挂载尚未渲染的组件的标记将导致异常Tystr\ReactJsBundle\Exception\ComponentNotRenderedException
。