douqi5079
douqi5079
2017-12-04 23:54
浏览 51
已采纳

使用php通过WebSocket在单页Web应用程序中进行“页面更改”

I am trying to make a single page web app that runs over a websocket for all server request. After the user logs in, I want the page to change the html to display a dashboard (first page).

I have a template for the dashboard already build from a previous version. I need to find a way to input database info into template and send the page over the websocket as a string for the javascript to display.

I don't know how to fill the template using php classes without hard coding it into the class, then turning it into a string so I can send it. The dashboard is very complex and has dynamically built menus for each users per their settings and permissions.

Am I looking at this whole problem wrong? I have some experience with MVC from ruby on rails/node.js, but unfortunately this app is stuck using php. Can I use a framework with the socket? Should I have the JS load the template through https and send the data over the socket? (The JS guy would not like that).

I am running Ubuntu, apache2, php7, Ratchet WebSocket(wss), composer, and MySQL all on AWS.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • droe9376
    droe9376 2017-12-05 00:12
    已采纳

    I would definitely let the frontend get the required data. You could use php as a backend to test the authentication, parse the request, get the data from the database and send it to the frontend. The frontend then would need to update the application using dom manipulation (think jQuery).
    Or, php could send a generated view that would be shown in a div in the frontend.

    In this setup, I would not recommend websockets, but just plain AJAX.

    From what I gather from your question, you might be better of using a real SPA framework like AngularJS. It it just better in using the data you will get from the backend. You could then choose to use a PHP backend using AJAX or a Websocket backend like nodejs.

    点赞 评论

相关推荐