管理不同的输出格式或设备类型

发布于 2024-11-19 22:54:00 字数 2280 浏览 0 评论 0原文

我必须为移动设备显示不同的视图,并且我想提供一个简单的 JSON-API。 我为 Kohana 框架编写了一个小模块,它根据某些情况加载不同的视图,这在这种情况下应该对我有帮助: https://github.com/ClaudioAlbertin/Kohana-View-Factory

但是,我对此解决方案不太满意,因为我无法为不同的设备类型设置不同的资产。另外,当我使用 JSON 视图输出 JSON 时,它仍然包含在所有 HTML 模板中。 现在,我正在寻找更好的解决方案。 如何在 MVC 应用程序中处理不同的输出格式或设备类型?

我有一个想法:只需将控制器分成两个控制器:一个数据控制器和一个输出控制器。

  • 数据控制器在模型的帮助下获取和设置数据, 所有验证等。它从模型获取数据并将其写入数据对象 随后将其传递给视图。
  • 输出控制器加载视图并从数据控制器向它们提供数据对象。每种格式或设备类型都有一个输出控制器:移动设备的输出控制器可以加载移动视图并添加所有移动版本的样式表和脚本。 JSON 输出控制器可以加载没有所有 html 模板内容的视图并将数据转换为 JSON。

举个小例子:

<?php

class Controller_Data_User extends Controller_Data // Controller_Data defines a data-object $this->data
{

    public function action_index()
    {
        $this->request->redirect('user/list');
    }

    public function action_list()
    {
        $this->data->users = ORM::factory('user')->find_all();
    }

    public function action_show($id)
    {
        $user = new Model_User((int) $id);

        if (!$user->loaded()) {
            throw new HTTP_Exception_404('User not found.');
        }

        $this->data->user = $user;
    }

}

class Controller_Output_Desktop extends Controller_Output_HTML // Controller_Output_HTML loads a HTML-template
{

    public function action_list($data)
    {
        $view = new View('user/list.desktop');
        $view->set($data->as_array());

        $this->template->body = $view;
    }

    public function action_show($data)
    {
        $view = new View('user/show.desktop');
        $view->set($data->as_array());

        $this->template->body = $view;
    }

}

class Controller_Output_JSON extends Controller_Output // Controller_Output doesn't load a template
{

    public function action_list($data)
    {
        $view = new View('user/list.json');
        $view->users = json_encode($data->users->as_array());

        $this->template = $view;
    }

    public function action_show($data)
    {
        $view = new View('user/show.json');
        $view->user = json_encode($data->user);

        $this->template = $view;
    }

}

你觉得怎么样?

I have to display different views for mobile devices and I want to provide a simple JSON-API.
I wrote a little module for the Kohana Framework which loads different views depending on some circumstances, which should help me in this case: https://github.com/ClaudioAlbertin/Kohana-View-Factory

However, I'm not very happy with this solution because I can't set different assets for different device-types. Also, when I'd output JSON with a JSON-view, it's still wrapped in all the HTML-templates.
Now, I'm looking for a better solution. How do you handle different output formats or device-types in your MVC-applications?

I had an idea: just split the controller into two controllers: a data-controller and an output-controller.

  • The data-controller gets and sets data with help of the models, does
    all the validating etc. It gets the data from the models and write it to a data-object
    which is later passed to the view.
  • The output-controller loads the views and give them the data-object from the data-controller. There is an output-controller for each format or device-type: an output-controller for mobile-devices could load the mobile-views and add all the mobile-versions of stylesheets and scripts. A JSON-output-controller could load a view without all the html-template stuff and convert the data into JSON.

A little example:

<?php

class Controller_Data_User extends Controller_Data // Controller_Data defines a data-object $this->data
{

    public function action_index()
    {
        $this->request->redirect('user/list');
    }

    public function action_list()
    {
        $this->data->users = ORM::factory('user')->find_all();
    }

    public function action_show($id)
    {
        $user = new Model_User((int) $id);

        if (!$user->loaded()) {
            throw new HTTP_Exception_404('User not found.');
        }

        $this->data->user = $user;
    }

}

class Controller_Output_Desktop extends Controller_Output_HTML // Controller_Output_HTML loads a HTML-template
{

    public function action_list($data)
    {
        $view = new View('user/list.desktop');
        $view->set($data->as_array());

        $this->template->body = $view;
    }

    public function action_show($data)
    {
        $view = new View('user/show.desktop');
        $view->set($data->as_array());

        $this->template->body = $view;
    }

}

class Controller_Output_JSON extends Controller_Output // Controller_Output doesn't load a template
{

    public function action_list($data)
    {
        $view = new View('user/list.json');
        $view->users = json_encode($data->users->as_array());

        $this->template = $view;
    }

    public function action_show($data)
    {
        $view = new View('user/show.json');
        $view->user = json_encode($data->user);

        $this->template = $view;
    }

}

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-11-26 22:54:00

嗯......从第一个角度来看,它看起来很奇怪,并且在某种程度上像分形 - 我们正在突破我们的 MVC 之一的 MVC - C。

但是为什么这个应用程序返回如此不同的结果,基于入口点(或设备)?

控制器的任务只是获取数据并选择视图——为什么我们需要独立的逻辑来根据入口点(设备)选择某些内容?

我认为首先应该回答这些问题。某个地方可能有问题。

此外,理想情况下控制器应仅选择一种视图,并且不要根据当前输出对数据进行“编码”或其他操作。我认为所有这些都应该以某种“布局”或其他方式进行。由于数据总是相同的,甚至不同的视图也应该是相同的——只是某些方面发生了变化。

Hmm... From the 1st view it loooks strange, and somehow like fractal -- we are breaking on MVC one of our MVC -- C.

But why is this app returns so different results, based on point-of-entry (or device)?

The task of the controller is only to get the data and choose the view -- why do we need standalone logic for choosing something based on point-of-entry (device)?

I think these questions should be answered first. Somewhere could be some problem.

Also the cotroller should select only one view ideally, and dont' do "encode" or else with data, based on current output. I think all this should be in some kind of "layouts" or else. As data always the same and even different views should be the same -- only some aspects changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文