如何使用 MVC 设计我的网站作为 Web 服务?

发布于 2024-10-25 08:56:13 字数 206 浏览 1 评论 0原文

我想知道设计我的网站的更好方法是什么,该网站应该以 MVC(模型-视图-控制器)结构输出用于浏览器的 HTML 和用于移动设备的 JSON。

我的想法是简单地添加 if/else 语句来确定控制器中的操作中输出 HTML 或 JSON,但我觉得有更好(或更灵活)的方法来做到这一点。

有没有人可以教我这件事或告诉我应该使用哪些关键字进行搜索?

谢谢!

I'm wondering what's the better way to design my website which should output HTML for browser and JSON for mobile device in MVC(Model-View-Controller) structure.

My idea is to simply add if/else statement to determine outputting HTML or JSON in actions in controller, but i feel there is a better(or more flexible) way to do this.

Is there anyone could teach me about this or tell me what keywords I should use to search?

Thanks!

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

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

发布评论

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

评论(3

恬淡成诗 2024-11-01 08:56:13

您基本上是在寻找抽象视图层。我的方法是定义一个视图接口,它只接受数据并返回一个字符串响应。像这样的事情:

interface View {
    function render($viewName, $data);
}

现在您可以创建这样的实现:

class PhpFileView implements View {
    private $baseDir;
    private $format;

    public function __construct($baseDir) {
        $this->baseDir = $baseDir;
        $this->format = 'html';
    }

    public function setFormat($format) {
        $this->format = $format;
    }

    public function render($viewName, $data) {
        ob_start();
        require "{$this->baseDir}/$viewName.{$this->format}.php";
        return ob_get_clean();
    }
}

现在您可以为控制器提供 PhpFileView 类的实例并执行如下操作:

class MyController {
    protected $view;

    public function __construct($config) {
        $this->view = new PhpFileView($config['view_dir']);
    }

    public function indexAction($params) {
        $this->view->setFormat($params['format']);
        return $this->view->render('my/index', array(
            'some_var' => 'some value',
        ));
    }
}

$config = array('view_dir' => 'app/views');
$controller = new MyController($config);
// format could be dynamically set to 'xml', yaml, anything
$params = array_merge($_REQUEST, array('format' => 'html'));
echo $controller->indexAction($params);

这为您提供了很大的灵活性,因为您可以在 中设置您想要的任何格式$params['format'] 然后用于包含 $viewdir/$viewName.$format.php。在我们的示例中,它将呈现 app/views/my/index.html.php

在该视图文件中,您可以访问 $data 变量。所以你可以做类似

这种系统的巨大优点是,您可以轻松添加对备用视图引擎的支持,例如 Twig (你应该看看它)。

You're basically looking for an abstracted view layer. The way I'd go about this is to define a view interface that simply takes data and returns a string response. Something like this:

interface View {
    function render($viewName, $data);
}

Now you can create an implementation of this:

class PhpFileView implements View {
    private $baseDir;
    private $format;

    public function __construct($baseDir) {
        $this->baseDir = $baseDir;
        $this->format = 'html';
    }

    public function setFormat($format) {
        $this->format = $format;
    }

    public function render($viewName, $data) {
        ob_start();
        require "{$this->baseDir}/$viewName.{$this->format}.php";
        return ob_get_clean();
    }
}

Now you can give your controller an instance of the PhpFileView class and do something like this:

class MyController {
    protected $view;

    public function __construct($config) {
        $this->view = new PhpFileView($config['view_dir']);
    }

    public function indexAction($params) {
        $this->view->setFormat($params['format']);
        return $this->view->render('my/index', array(
            'some_var' => 'some value',
        ));
    }
}

$config = array('view_dir' => 'app/views');
$controller = new MyController($config);
// format could be dynamically set to 'xml', yaml, anything
$params = array_merge($_REQUEST, array('format' => 'html'));
echo $controller->indexAction($params);

This gives you a lot of flexibility as you can set any format you want in $params['format'] which is then used to include $viewdir/$viewName.$format.php. In our example it would render app/views/my/index.html.php.

Inside that view file you have access to the $data variable. So you could do something like <?php echo $data['some_var']; ?>.

The great advantage of such a system is that you could easily add support for alternate view engines such as Twig (you should take a look at it).

肩上的翅膀 2024-11-01 08:56:13

更好的想法是对 HTML 和 JSON 使用单独的视图。

在正确实现的 MVC 框架中,您可以设置用于呈现输出的视图类(或模板)。这可以是 JSON、HTML、XML 或其他任何形式。

a better idea would be to use separate views for HTML and JSON.

In correctly implemented MVC frameworks you can set the view class (or template) to be used to render the output. This can be JSON, HTML, XML or whatever.

风蛊 2024-11-01 08:56:13
//From ur controller.
public handleResponse($responseData)
{
   $responseInf = Util::getResponseObject($headers,$responseData);
   $responseInf->flushOutput();
} 


class Util
{
   static function getResponseObject($headers,$responseData)
   {
       $responseInf = false;
       if($headers specific mobile)
         $responseInf = new JSON_Output($responseData);
       else
         $responseInf = new HTML_Output($responseData);

       return $responseInf;
   }
}

Interface ResponseInf
{
   public function flushOutput();
}

class JSON_Output implements ResponseInf
{
    private $responseData = false;

    function ___construct($data)
      $this->responseData = $data;

    public function flushOutput()
    {
       //Convert $this->responseData to JSON object
       //flush json object from here.
    }
}

class HTML_Output implements ResponseInf
{
    private $responseData = false;

    function ___construct($data)
      $this->responseData = $data;

    public function flushOutput()
    {
       //Convert $this->responseData to HTML string 
       //flush html from here.
    }
}
//From ur controller.
public handleResponse($responseData)
{
   $responseInf = Util::getResponseObject($headers,$responseData);
   $responseInf->flushOutput();
} 


class Util
{
   static function getResponseObject($headers,$responseData)
   {
       $responseInf = false;
       if($headers specific mobile)
         $responseInf = new JSON_Output($responseData);
       else
         $responseInf = new HTML_Output($responseData);

       return $responseInf;
   }
}

Interface ResponseInf
{
   public function flushOutput();
}

class JSON_Output implements ResponseInf
{
    private $responseData = false;

    function ___construct($data)
      $this->responseData = $data;

    public function flushOutput()
    {
       //Convert $this->responseData to JSON object
       //flush json object from here.
    }
}

class HTML_Output implements ResponseInf
{
    private $responseData = false;

    function ___construct($data)
      $this->responseData = $data;

    public function flushOutput()
    {
       //Convert $this->responseData to HTML string 
       //flush html from here.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文