开发者

Managing different output formats or device-types

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 Fram开发者_如何学运维ework 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜