开发者

how to design my website to serve as a web-service using MVC?

开发者 https://www.devze.com 2023-02-19 11:24 出处:网络
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.

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!


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).


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.


//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.
    }
}
0

精彩评论

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