简体   繁体   English

控制器和视图-Zend Framework中的MVC

[英]Controllers and Views - MVC in Zend Framework

I am using MVC in PHP based Zend Framework. 我在基于PHP的Zend Framework中使用MVC。 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. 这些动作可通过AJAX从控制器的视图访问。 The controller's actions, perform Business logic by accessing data from functions inside a model, and construct or echo HTML. 控制器的动作,通过访问模型内​​部函数中的数据来执行业务逻辑,以及构造或回显HTML。 This HTML is spit back to view in the AJAX response. 将此HTML吐回以在AJAX响应中查看。 My understanding of controllers is they are not supposed to contain any HTML at all. 我对控制器的理解是,它们根本不应该包含任何HTML。 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. 但是鉴于视图中的AJAX,我觉得除了在控制器中动态生成HTML外,我别无选择。 Is this a good design? 这是一个好的设计吗? How can I improve it? 我该如何改善?

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. 您可以通过ajaxContext或contextSwitch操作助手将操作用于多个上下文。 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. 根据我的经验,上下文切换通常更有用,它甚至可以自动序列化在操作中为JSON响应分配给视图的数据,因此不需要视图脚本。

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(); 上面的代码将json的上下文添加到上下文开关,并且在使用请求参数'format'设置了调用动作时,它将自动序列化内容,在这种情况下,将给出My_Model返回的项的json数组: fetchAll();

The format parameter can either be passed in the url "/my/index/format/json" or with a get query "/my/index?format=json" format参数可以在url“ / my / index / format / json”中传递,也可以在get查询中输入“ / 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 在此处阅读有关上下文切换的更多信息

First of all, the business logic should be in the Model, not the Controller. 首先,业务逻辑应该在模型中,而不是在控制器中。

Secondly, My ajax requests commonly have this sort of format: 其次,我的ajax请求通常具有以下格式:

if ($ajax = $this->getRequest()->isXMLHttpRequest()) {
  $this->_helper->layout->disableLayout();   
};

// then later - if its responding with json:

  if ($ajax)
  {
    $this->_helper->viewRenderer->setNoRender(true);     
    $this->getResponse()->setHeader('Content-Type', 'text/json'); 
    echo $this->view->json($some_return_values);
    return;
  }

Also - don't forget you can use $this->view->render('controller/action.phtml'); 另外-不要忘记您可以使用$this->view->render('controller/action.phtml'); to capture a rendered phtml into a string to return via the controller. 将渲染的phtml捕获为字符串以通过控制器返回。 This will allow you to keep presentation in the view. 这将使您可以在视图中保持演示。

Always try to leave any presentational logic inside a view. 始终尝试在视图中保留任何表示逻辑。 I think a correct design approach for your application would be something like : 我认为适合您的应用程序的正确设计方法应该是:

AjaxController.php : AjaxController.php:

public function AjaxAction() {
    //do some logic
    //set content to a variable
}

ajax.phtml : ajax.phtml:

<p><?php //display the content of the variable setted in the controller ?></p>

You can later edit the view or the action separately, perhaps even rehuse the view for some similar ajax generated content, etc. Always try to separate things this way, that is the way MVC pattern is designed to work with. 您以后可以单独编辑视图或操作,甚至可以为某些相似的Ajax生成的内容重用视图,等等。请始终尝试以这种方式将事物分开,这就是MVC模式设计用于的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM