Php – Controllers and Views – MVC in Zend Framework

model-view-controllerPHPzend-framework

I am using MVC in PHP based Zend Framework. This is more of a design question. I have a controller that has a couple of actions. These actions are accessed via AJAX from the controller's view. The controller's actions, perform Business logic by accessing data from functions inside a model, and construct or echo HTML. This HTML is spit back to view in the AJAX response. My understanding of controllers is they are not supposed to contain any HTML at all. But given the AJAX in the views, I feel I don't have a choice except to generate HTML on the fly in the controller. Is this a good design? How can I improve it?

Best Answer

There are two action helpers for doing exactly this. you can re-use your actions for multiple contexts with the ajaxContext or contextSwitch action helpers. The context switch is generally the more useful in my experience, and it can even automatically serialize the data you assign to the view in your action for json responses so there is no need for a view script.

you initialise the context switch like this:

class MyController extends Zend_Controller_Action
{
    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('index', 'json')
                      ->initContext();
    }
    public function indexAction()
    {
        $this->view->items = My_Model::fetchAll();
    }
}

The above will add a context of json to the context switch, and when the action is called with the request parameter 'format' set, it will automatically serialize the content, in this case giving a json array of the items returned by My_Model::fetchAll();

The format parameter can either be passed in the url "/my/index/format/json" or with a get query "/my/index?format=json"

The real magic is that the context switch also sets the appropriate headers for the response type, such as content-type.

You can even specify your own contexts, and the headers to send. Read more about the context switch here

Related Topic