如何使用 Laravel + React.js + Redux 进行服务器端渲染?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36371681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do server side rendering with Laravel + React.js + Redux?
提问by Vivek Sancheti
We are setting an app using Laravel in backend and React.JS + Redux in front end. So looking for solution for server side rendering (for SEO).
我们正在后端使用 Laravel 和前端使用 React.JS + Redux 设置应用程序。所以正在寻找服务器端渲染的解决方案(用于 SEO)。
Found this solution: https://github.com/tz5514/Laravel-Redux-Isomorphicbut it seems like in backend it using express.js to render.
找到了这个解决方案:https: //github.com/tz5514/Laravel-Redux-Isomorphic但它似乎在后端使用 express.js 进行渲染。
So I am searching for any better server side rendering solution.
所以我正在寻找任何更好的服务器端渲染解决方案。
回答by Vivek Sancheti
React is javascript, so it needs a javascript engine in order to run, so one way to see a laravel project is to think of it as the 'backend' api server, which is completely separated from the 'frontend' server which uses node to run the javascript part of the application.
React 是 javascript,因此它需要一个 javascript 引擎才能运行,因此查看 laravel 项目的一种方法是将其视为“后端”api 服务器,它与使用 node 的“前端”服务器完全分离运行应用程序的 javascript 部分。
In other words, you have two apps, the one with laravel that exposes an api which could be used by any other service in the infrastructure, and the reactjs app which is a nodejs app that consumes that api, and handles the magic of react + redux.
换句话说,您有两个应用程序,一个带有 laravel,它公开了一个可以被基础设施中的任何其他服务使用的 api,另一个是 reactjs 应用程序,它是一个使用该 api 的 nodejs 应用程序,并处理 react +还原。
The way your site is actually accessed from the outside world is thru the express/nodejs app, which calls the laravel api for data persistence and other business logic.
从外部世界实际访问您的站点的方式是通过 express/nodejs 应用程序,该应用程序调用 laravel api 以实现数据持久性和其他业务逻辑。
The laravel app handles sessions, oauth, the database, file access to the hardrive and so on, while the nodejs app handles the actual website routing, the templates, the css and javascript part.
laravel 应用程序处理会话、oauth、数据库、对硬盘的文件访问等,而 nodejs 应用程序处理实际的网站路由、模板、css 和 javascript 部分。
I got this answer from HashNode
我从HashNode得到了这个答案
回答by devsourav
you can use the spatie/laravel-server-side-renderingpackage, it is great for both React & Vue. Here you can find YT tutorial for this package usage as well - https://youtu.be/uO42Kpa4pvI
你可以使用spatie/laravel-server-side-rendering包,它非常适合 React 和 Vue。在这里你也可以找到这个包使用的 YT 教程 - https://youtu.be/uO42Kpa4pvI
回答by Naufal Rabbani
React seems suitable with Node Js (only). But, You can try to solve your problem with use this Laravel Plugin. react-laravel
React 似乎适合 Node Js(仅限)。但是,您可以尝试使用此 Laravel 插件来解决您的问题。 反应-laravel