How to implement angular server side rendering in laravel?

I created a single page application with angular 5. I use this application with laravel backend. In the laravel project I have an angular folder, this folder store all the angular files. I built the angular application outside the angular folder, in a public folder in the root laravel's project folder. I set the web.php file, so if I go to homestead.test url the index.html will loaded from the public folder. After I set up the serverside rendering with stories universal rendering, I built the client application and the server bundle. After I did this, and go to the homestead.test in the browser a I saw that my application doesn't look like before. With normal ng build (I'm hungarian by the way, so I used hungarian words): 在这里输入图像描述 After i did the universan rendering builds (ng build --prod --extract-css, ng build --prod --extract-css --app 1 --output-hashing=false, I used the --extract-css, because I used scss in the angular and when i didn't used it i got a lot of warnings): 在这里输入图像描述 And when I looked at the page source, it looked like the original angular source, but in the express server the source shows the real content. So my question is how to set up laravel to act like the express server? The ng build --prod --extract-css create a lot of js file, the assets folder and some other files, but the ng build only create some of them, without the assets folder. So how can I import all the assets folder and the js files in laravel? The stories universal rendering create a server.js file too from Node server.ts. I don't know this file is necessary in laravel.

I also got some addicitonal questions: Is it the best way to connect laravel to an angular application? If yes, can I host it with real web host? Is there any free web host to test my application? If I succesfully display the angular app in laravel like I did with express server, than refresh the page in any url will works? Because now I got an error message.

I created this application as a thesis app for my university and I've neved host app in the web, I only used local hosts.


The idea of Angular Universal is to run the javascript on the server side to generate the page on the server side. Of course that is easy with Node (Express JS) and hence that is there already.

Regarding Angular Universal on PHP, you can follow this Github issue. What I understand is, at least today, there is no support. But it's true that many people want to have the support and probably the support will be there in future.

For the time being, you have the following options:

  • You may keep doing thing the way you do now and you can host the app in any hosting platform. Although, you should probably improve your build mechanism to remove the manual intervention of copying angular files within Laraval. Your build mechanism should do all this copying things. The downside of the approach is that you won't have the benefits of server side rendering that Angular Universal provides. But if you don't care about those benefits like page loading speed and SEO, you are good.
  • The other option is to host your Angular part in the Node (Express JS) and route all API calls to Laraval. Thus you can get the benefit of Angular Universal. It means that you seend to run at lease two processes - one for the API and the other for serving the UI.
  • Both the options are used depending on the requirement. For a larger microservices based architecture the second approach is preferred as it makes the lifecycle of the UI part and the API part independent.

    However, for smaller projects where the user base is less and you don't really care about SEO, the first option makes sense.

    链接地址: http://www.djcxy.com/p/31398.html

    上一篇: 在App Engine上使用Angular Universal进行服务器渲染的Angular 5应用程序

    下一篇: 如何在laravel中实现角度服务器端渲染?