webiik/ssr
Composer 安装命令:
composer require webiik/ssr
包简介
Server-side rendering of javascript UI components from PHP.
README 文档
README
SSR
Server-side rendering of javascript UI components from PHP. Out of the box, it supports React, however you can add any JS UI library.
Prerequisites
Supports
JS engines:
UI libraries:
- React
- Custom UI library.
Step-by-step Example
This example uses Webpack to bundle JS. You can use your favorite JS bundler.
-
Create the
MyTestfolder with the following file structure.. .. ├── package.json ├── webpack.config.js ├── meow.jsx ├── index.js └── index.php
-
Inside
MyTestfolder install all necessary packages.npm install react npm install react-dom npm install @webiik/render-js-components composer require webiik/ssr
-
Create a component.
Edit
meow.jsxto:import * as React from 'react'; export const Meow = (props) => { return (<h1>Meow {props.name}!</h1>); }
-
Register the component.
Edit
index.jsto:import {registerReactComponent} from '@webiik/render-js-components'; import {Meow} from 'meow'; registerReactComponent({Meow});
-
Configure Webpack.
Edit
webpack.config.jsto:const webpack = require('webpack'); const path = require('path'); module.exports = { entry: { 'index': 'index.js' }, output: { filename: '[name].js', path: path.resolve(__dirname + '/build/') }, resolve: { extensions: ['.js', '.jsx'] } };
-
Bundle
index.jstobuild/index.js. Remember,build/index.jsMUST contain all code dependencies required to render the component with javascript.webpack -p --colors --progress
-
Render the component from PHP.
Edit
index.phpto:// Render the component on server $ssr = new \Webiik\Ssr\Ssr(); $engine = new \Webiik\Ssr\Engines\NodeJs(); $engine->setTmpDir(__DIR__); $ssr->useEngine($engine); $html = $ssr->render('build/index.js', 'Meow', ['name' => 'Dolly'], [ 'ssr' => true, ]); // Load JS libs on client echo '<script src="build/index.js"></script>'; // Print server-side rendered component on client echo $html;
Cache
You can use cache to store rendered components.
// To enable cache you MUST set cache dir and add 'cache' key to $renderOptions. // To prevent cache conflicts cache key MUST be unique. $ssr->setCacheDir(__DIR__); $html = $ssr->render('index.js', 'Meow', ['name' => 'Dolly'], [ 'ssr' => true, 'cache' => 'Meow', 'expires' => 1, // 1 = one hour, 0 = never expires ]);
Custom UI library
SSR supports React out of the box, however, you can add support for your favorite UI library.
-
JS - Create a component registrar. Use registerReactComponent.tsx as template. The purpose of component registrar is to register function that renders component on a server and client.
-
PHP - Use method
setFwJsMask()to tell SSR how to call the component registrar from step 1.$ssr->setFwJsMask('vue', 'window.WebiikVue.%1$s("%2$s", "%3$s", "%4$s")');
-
PHP - Tell SSR that you want to use the component registrar from step 2.
$ssr->setDefaultFramework('vue');
or
$html = $ssr->render('index.js', 'Meow', ['name' => 'Dolly'], [ 'fw' => 'vue', ]);
If you need it. You can use more UI libraries at once.
Custom JS engine
The engine is a PHP class that processes JS and returns the result as a string. The engine MUST implement EngineInterface. See the code of current engines to learn more.
- PHP - Create your engine.
- PHP - Tell SSR to use your engine.
$ssr->useEngine(new YourCustomEngine());
Resources
webiik/ssr 适用场景与选型建议
webiik/ssr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「JS」 「react」 「vue」 「SSR」 「server side rendering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webiik/ssr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webiik/ssr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webiik/ssr 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Async Reactive Postgres Driver for PHP (Non-blocking)
A component that allows creating responsive HTML tables or lists from data object
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
PHP client for the Google Closure Compiler API in one file.
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-08