douchongbang6011 2015-12-14 08:25
浏览 49

从后端服务渲染UI是一个很好的设计吗?

I read some MVC framework for web application like play-framework, yii etc. They want to let server side to render the UI and send it to the browser side. Please see below yii code as an example,

I created a php class as below:

<?php

namespace app\controllers;

use yii\web\Controller;

class SiteController extends Controller
{
    // ...existing code...

    public function actionSay($message = 'Hello')
    {
        return $this->render('say', ['message' => $message]);
    }
}

Then I create a view php file:

<?php
use yii\helpers\Html;
?>
<?= Html::encode($message) ?>

When the render method is called in the action of the controller class, it will render the view and return that to the browser.

I see there are a few frameworks which are using that kind of design pattern. I wander whether it is a good idea from a design point of view. It seems that they combine UI and service together. Whenever we change something on UI, we have to do the corresponding update on server code.

I'd like to separate UI and backend service to different layers. For example, the service can listen on a restful address port over http protocol. And the client side will request/post on these restful interfaces. In this way, the server side doesn't know anything about who is UI/client. It can be browser, mobile or any kind of client.

Which design is a better from design point of view? I just want to get more idea on them. Any feedback is very welcome.

  • 写回答

1条回答 默认 最新

  • duangua5308 2015-12-14 08:35
    关注

    You've asked a very subjective question, so it's possible you might get flagged. Still, I'll offer up my view and if it's helpful, great!

    You're right that separating UI from service matters, but you can separate UI and service just fine without having to render the UI on the client.

    If you're using PHP, you would create a RESTful API as you said, which is responsible for taking URLs like /content/143 and returning some JSON. You could then create a rendering front-end that receives requests direct from users, hits your API to fetch the content, then converts that into rendered HTML for the user. I use Guzzle for this and it works great, although you should implement caching in the renderer if you anticipate any serious load.

    If you want to put UI rendering on the client, that's fine too: again you would make your API in PHP (I'm only saying that because of your PHP tag), but then create the front-end using a toolkit like React where everything is rendered on the client.

    There are advantages to both options, so it's really down to skill availability and your precise use case.

    No matter what you choose, using some sort of output templating system is basically a must. Twig is great for this if you want a pure PHP approach. If you try React you'll soon run into something neat called JSX, which is XML that gets compiled down to JavaScript – the syntax is strange at first glance, but then you realise how clever it is.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c